ClickHouse/dbms/src/Common/NaNUtils.h

58 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>
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{};
}
2018-07-20 19:05:07 +00:00
2018-07-25 19:38:21 +00:00
#if 1 /// __int128
2018-07-20 19:05:07 +00:00
template <typename T>
2018-08-15 15:29:57 +00:00
std::enable_if_t<std::is_same_v<T, __int128> && !std::numeric_limits<T>::is_integer, __int128> NaNOrZero()
2018-07-20 19:05:07 +00:00
{
return __int128(0);
}
2018-07-25 19:38:21 +00:00
#endif