From 09bbe07fedcdf74f9d0bb28510fe5f3bf0748f6a Mon Sep 17 00:00:00 2001 From: graehl Date: Sun, 15 Aug 2010 02:43:08 +0000 Subject: nan git-svn-id: https://ws10smt.googlecode.com/svn/trunk@550 ec762483-ff6d-05da-a07a-a48fb63a330f --- utils/nan.h | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100755 utils/nan.h diff --git a/utils/nan.h b/utils/nan.h new file mode 100755 index 00000000..257364d5 --- /dev/null +++ b/utils/nan.h @@ -0,0 +1,42 @@ +#ifndef NAN_H +#define NAN_H +//TODO: switch to C99 isnan isfinite isinf etc. (faster) + +#include + +template struct nan_static_assert; +template <> struct nan_static_assert { }; + +// is_iec559 i.e. only IEEE 754 float has x != x <=> x is nan +template +inline bool is_nan(T x) { +// static_cast(sizeof(nan_static_assert::has_quiet_NaN>)); + return std::numeric_limits::has_quiet_NaN && (x != x); +} + +template +inline bool is_inf(T x) { +// static_cast(sizeof(nan_static_assert::has_infinity>)); + return x == std::numeric_limits::infinity() || x == -std::numeric_limits::infinity(); +} + +template +inline bool is_pos_inf(T x) { +// static_cast(sizeof(nan_static_assert::has_infinity>)); + return x == std::numeric_limits::infinity(); +} + +template +inline bool is_neg_inf(T x) { +// static_cast(sizeof(nan_static_assert::has_infinity>)); + return x == -std::numeric_limits::infinity(); +} + +//c99 isfinite macro shoudl be much faster +template +inline bool is_finite(T x) { + return !is_nan(x) && !is_inf(x); +} + + +#endif -- cgit v1.2.3