summaryrefslogtreecommitdiff
path: root/utils/nan.h
blob: 257364d56757fe8bca9f433d53eaecd2a3cae48d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef NAN_H
#define NAN_H
//TODO: switch to C99 isnan isfinite isinf etc. (faster)

#include <limits>

template <bool> struct nan_static_assert;
template <> struct nan_static_assert<true> { };

// is_iec559 i.e. only IEEE 754 float has x != x <=> x is nan
template<typename T>
inline bool is_nan(T x) {
//    static_cast<void>(sizeof(nan_static_assert<std::numeric_limits<T>::has_quiet_NaN>));
    return std::numeric_limits<T>::has_quiet_NaN && (x != x);
}

template <typename T>
inline bool is_inf(T x) {
//    static_cast<void>(sizeof(nan_static_assert<std::numeric_limits<T>::has_infinity>));
    return x == std::numeric_limits<T>::infinity() || x == -std::numeric_limits<T>::infinity();
}

template <typename T>
inline bool is_pos_inf(T x) {
//    static_cast<void>(sizeof(nan_static_assert<std::numeric_limits<T>::has_infinity>));
    return x == std::numeric_limits<T>::infinity();
}

template <typename T>
inline bool is_neg_inf(T x) {
//    static_cast<void>(sizeof(nan_static_assert<std::numeric_limits<T>::has_infinity>));
    return x == -std::numeric_limits<T>::infinity();
}

//c99 isfinite macro shoudl be much faster
template <typename T>
inline bool is_finite(T x) {
  return !is_nan(x) && !is_inf(x);
}


#endif