ClickHouse/dbms/src/Common/NaNUtils.h

50 lines
973 B
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>
2017-12-25 04:01:46 +00:00
inline std::enable_if_t<std::is_floating_point_v<T>, bool> 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>
2017-12-25 04:01:46 +00:00
inline std::enable_if_t<!std::is_floating_point_v<T>, bool> isNaN(T)
{
return false;
}
template <typename T>
2017-12-25 04:01:46 +00:00
inline std::enable_if_t<std::is_floating_point_v<T>, bool> isFinite(T x)
{
return std::isfinite(x);
}
template <typename T>
2017-12-25 04:01:46 +00:00
inline std::enable_if_t<!std::is_floating_point_v<T>, bool> isFinite(T)
{
return true;
}
2017-03-11 01:29:45 +00:00
template <typename T>
2017-12-25 04:01:46 +00:00
std::enable_if_t<std::is_floating_point_v<T>, T> NaNOrZero()
2017-03-11 01:29:45 +00:00
{
return std::numeric_limits<T>::quiet_NaN();
2017-03-11 01:29:45 +00:00
}
template <typename T>
2017-12-25 04:01:46 +00:00
std::enable_if_t<std::numeric_limits<T>::is_integer, T> NaNOrZero()
2017-03-11 01:29:45 +00:00
{
return 0;
2017-03-11 01:29:45 +00:00
}
template <typename T>
2017-12-25 04:01:46 +00:00
std::enable_if_t<std::is_class_v<T>, T> NaNOrZero()
{
return T{};
}