ClickHouse/src/Common/NaNUtils.h

52 lines
1013 B
C++
Raw Normal View History

2017-03-11 01:29:45 +00:00
#pragma once
#include <cmath>
#include <limits>
#include <type_traits>
2020-09-15 09:55:57 +00:00
#include <common/extended_types.h>
2017-03-11 01:29:45 +00:00
/// 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>
std::enable_if_t<is_integer_v<T>, T> NaNOrZero()
2017-03-11 01:29:45 +00:00
{
return T{0};
2017-03-11 01:29:45 +00:00
}
template <typename T>
std::enable_if_t<std::is_class_v<T> && !is_integer_v<T>, T> NaNOrZero()
{
return T{};
}