]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGMisc.hxx
b6671f8434fbfedb20a9b253a1c63a3af7b6cfcb
[simgear.git] / simgear / math / SGMisc.hxx
1 #ifndef SGMisc_H
2 #define SGMisc_H
3
4 #include <cmath>
5
6 template<typename T>
7 class SGMisc {
8 public:
9   static T pi() { return T(3.1415926535897932384626433832795029L); }
10   static T min(const T& a, const T& b)
11   { return a < b ? a : b; }
12   static T min(const T& a, const T& b, const T& c)
13   { return min(min(a, b), c); }
14   static T min(const T& a, const T& b, const T& c, const T& d)
15   { return min(min(min(a, b), c), d); }
16   static T max(const T& a, const T& b)
17   { return a > b ? a : b; }
18   static T max(const T& a, const T& b, const T& c)
19   { return max(max(a, b), c); }
20   static T max(const T& a, const T& b, const T& c, const T& d)
21   { return max(max(max(a, b), c), d); }
22   static int sign(const T& a)
23   {
24     if (a < -SGLimits<T>::min())
25       return -1;
26     else if (SGLimits<T>::min() < a)
27       return 1;
28     else
29       return 0;
30   }
31
32 #ifndef NDEBUG
33   /// Returns true if v is a NaN value
34   /// Use with care: allways code that you do not need to use that!
35   static bool isNaN(const T& v)
36   {
37 #ifdef HAVE_ISNAN
38     return isnan(v);
39 #elif defined HAVE_STD_ISNAN
40     return std::isnan(v);
41 #else
42     // Use that every compare involving a NaN returns false
43     // But be careful, some usual compiler switches like for example
44     // -fast-math from gcc might optimize that expression to v != v which
45     // behaves exactly like the opposite ...
46     return !(v == v);
47 #endif
48   }
49 #endif
50 };
51
52 typedef SGMisc<float> SGMiscf;
53 typedef SGMisc<double> SGMiscd;
54
55 #endif