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>
|
2020-08-19 11:52:17 +00:00
|
|
|
|
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
|
|
|
{
|
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-12-25 04:01:46 +00:00
|
|
|
inline std::enable_if_t<!std::is_floating_point_v<T>, bool> isNaN(T)
|
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>
|
2017-12-25 04:01:46 +00:00
|
|
|
inline std::enable_if_t<std::is_floating_point_v<T>, bool> isFinite(T x)
|
2017-06-15 09:12:32 +00:00
|
|
|
{
|
|
|
|
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)
|
2017-06-15 09:12:32 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
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>
|
2020-08-19 11:52:17 +00:00
|
|
|
std::enable_if_t<is_integer_v<T>, T> NaNOrZero()
|
2017-03-11 01:29:45 +00:00
|
|
|
{
|
2020-08-19 11:52:17 +00:00
|
|
|
return T{0};
|
2017-03-11 01:29:45 +00:00
|
|
|
}
|
2017-06-15 09:12:32 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2020-08-19 11:52:17 +00:00
|
|
|
std::enable_if_t<std::is_class_v<T> && !is_integer_v<T>, T> NaNOrZero()
|
2017-06-15 09:12:32 +00:00
|
|
|
{
|
|
|
|
return T{};
|
|
|
|
}
|