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-06-15 09:12:32 +00:00
|
|
|
inline typename std::enable_if<std::is_floating_point<T>::value, bool>::type isNaN(T x)
|
2017-03-11 01:29:45 +00:00
|
|
|
{
|
2017-06-15 09:12:32 +00:00
|
|
|
return std::isnan(x);
|
2017-03-11 01:29:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 10:13:45 +00:00
|
|
|
template <typename T>
|
2017-06-15 09:12:32 +00:00
|
|
|
inline typename std::enable_if<!std::is_floating_point<T>::value, bool>::type isNaN(T x)
|
2017-03-12 10:13:45 +00:00
|
|
|
{
|
2017-06-15 09:12:32 +00:00
|
|
|
return false;
|
2017-03-12 10:13:45 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 09:12:32 +00:00
|
|
|
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()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return std::numeric_limits<T>::quiet_NaN();
|
2017-03-11 01:29:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2017-06-15 09:12:32 +00:00
|
|
|
typename std::enable_if<std::numeric_limits<T>::is_integer, T>::type NaNOrZero()
|
2017-03-11 01:29:45 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
return 0;
|
2017-03-11 01:29:45 +00:00
|
|
|
}
|
2017-06-15 09:12:32 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
typename std::enable_if<std::is_class<T>::value, T>::type NaNOrZero()
|
|
|
|
{
|
|
|
|
return T{};
|
|
|
|
}
|