ClickHouse/dbms/include/DB/Common/NaNUtils.h

33 lines
658 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>
inline bool isNaN(T x)
{
return std::is_floating_point<T>::value ? std::isnan(x) : false;
}
template <typename T>
inline bool isFinite(T x)
{
return std::is_floating_point<T>::value ? std::isfinite(x) : 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();
}
template <typename T>
typename std::enable_if<!std::is_floating_point<T>::value, T>::type NaNOrZero()
{
return 0;
}