]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGMisc.hxx
59c61edc75e285783e562d1485412fa797a82f7f
[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   static T rad2deg(const T& val)
33   { return val*180/pi(); }
34   static T deg2rad(const T& val)
35   { return val*pi()/180; }
36
37 #ifndef NDEBUG
38   /// Returns true if v is a NaN value
39   /// Use with care: allways code that you do not need to use that!
40   static bool isNaN(const T& v)
41   {
42 #ifdef HAVE_ISNAN
43     return isnan(v);
44 #elif defined HAVE_STD_ISNAN
45     return std::isnan(v);
46 #else
47     // Use that every compare involving a NaN returns false
48     // But be careful, some usual compiler switches like for example
49     // -fast-math from gcc might optimize that expression to v != v which
50     // behaves exactly like the opposite ...
51     return !(v == v);
52 #endif
53   }
54 #endif
55 };
56
57 typedef SGMisc<float> SGMiscf;
58 typedef SGMisc<double> SGMiscd;
59
60 #endif