]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGMisc.hxx
b8ba7e8d9c764b45d6d0daa4d9206105d24b6be6
[simgear.git] / simgear / math / SGMisc.hxx
1 // Copyright (C) 2006  Mathias Froehlich - Mathias.Froehlich@web.de
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Library General Public
5 // License as published by the Free Software Foundation; either
6 // version 2 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Library General Public License for more details.
12 //
13 // You should have received a copy of the GNU Library General Public
14 // License along with this library; if not, write to the
15 // Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 // Boston, MA  02111-1307, USA.
17 //
18
19 #ifndef SGMisc_H
20 #define SGMisc_H
21
22 #include <cmath>
23
24 template<typename T>
25 class SGMisc {
26 public:
27   static T pi() { return T(3.1415926535897932384626433832795029L); }
28   static T min(const T& a, const T& b)
29   { return a < b ? a : b; }
30   static T min(const T& a, const T& b, const T& c)
31   { return min(min(a, b), c); }
32   static T min(const T& a, const T& b, const T& c, const T& d)
33   { return min(min(min(a, b), c), d); }
34   static T max(const T& a, const T& b)
35   { return a > b ? a : b; }
36   static T max(const T& a, const T& b, const T& c)
37   { return max(max(a, b), c); }
38   static T max(const T& a, const T& b, const T& c, const T& d)
39   { return max(max(max(a, b), c), d); }
40   static int sign(const T& a)
41   {
42     if (a < -SGLimits<T>::min())
43       return -1;
44     else if (SGLimits<T>::min() < a)
45       return 1;
46     else
47       return 0;
48   }
49
50   static T rad2deg(const T& val)
51   { return val*180/pi(); }
52   static T deg2rad(const T& val)
53   { return val*pi()/180; }
54
55 #ifndef NDEBUG
56   /// Returns true if v is a NaN value
57   /// Use with care: allways code that you do not need to use that!
58   static bool isNaN(const T& v)
59   {
60 #ifdef HAVE_ISNAN
61     return isnan(v);
62 #elif defined HAVE_STD_ISNAN
63     return std::isnan(v);
64 #else
65     // Use that every compare involving a NaN returns false
66     // But be careful, some usual compiler switches like for example
67     // -fast-math from gcc might optimize that expression to v != v which
68     // behaves exactly like the opposite ...
69     return !(v == v);
70 #endif
71   }
72 #endif
73 };
74
75 typedef SGMisc<float> SGMiscf;
76 typedef SGMisc<double> SGMiscd;
77
78 #endif