ClickHouse/dbms/src/Common/NaNUtils.h

50 lines
1.1 KiB
C++
Raw Normal View History

2017-03-11 01:29:45 +00:00
#pragma once
#include <cmath>
#include <limits>
#include <type_traits>
/// To be sure, that this function is zero-cost for non-floating point types.
template <typename T>
inline typename std::enable_if<std::is_floating_point<T>::value, bool>::type isNaN(T x)
2017-03-11 01:29:45 +00:00
{
return std::isnan(x);
2017-03-11 01:29:45 +00:00
}
template <typename T>
inline typename std::enable_if<!std::is_floating_point<T>::value, bool>::type isNaN(T x)
{
return false;
}
template <typename T>
inline typename std::enable_if<std::is_floating_point<T>::value, bool>::type isFinite(T x)
{
return std::isfinite(x);
}
template <typename T>
inline typename std::enable_if<!std::is_floating_point<T>::value, bool>::type isFinite(T x)
{
return true;
}
2017-03-11 01:29:45 +00:00
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type NaNOrZero()
{
return std::numeric_limits<T>::quiet_NaN();
2017-03-11 01:29:45 +00:00
}
template <typename T>
typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type NaNOrZero()
2017-03-11 01:29:45 +00:00
{
return 0;
2017-03-11 01:29:45 +00:00
}
template <typename T>
typename std::enable_if<std::is_class<T>::value, T>::type NaNOrZero()
{
return T{};
}