summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorgraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-08-15 02:43:08 +0000
committergraehl <graehl@ec762483-ff6d-05da-a07a-a48fb63a330f>2010-08-15 02:43:08 +0000
commit66d9f91ce7ad792138d68a7f29ab159e7af6a132 (patch)
tree8ec4a54172d16cdc5111c56a16e07186167a21b0
parent66c7b26f43ae057b88a9c94d11b5819dd94b0940 (diff)
nan
git-svn-id: https://ws10smt.googlecode.com/svn/trunk@550 ec762483-ff6d-05da-a07a-a48fb63a330f
-rwxr-xr-xutils/nan.h42
1 files changed, 42 insertions, 0 deletions
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 <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