]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGMisc.hxx
Modified Files:
[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 General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16 //
17
18 #ifndef SGMisc_H
19 #define SGMisc_H
20
21 template<typename T>
22 class SGMisc {
23 public:
24   static T pi() { return T(3.1415926535897932384626433832795029L); }
25   static T min(const T& a, const T& b)
26   { return a < b ? a : b; }
27   static T min(const T& a, const T& b, const T& c)
28   { return min(min(a, b), c); }
29   static T min(const T& a, const T& b, const T& c, const T& d)
30   { return min(min(min(a, b), c), d); }
31   static T max(const T& a, const T& b)
32   { return a > b ? a : b; }
33   static T max(const T& a, const T& b, const T& c)
34   { return max(max(a, b), c); }
35   static T max(const T& a, const T& b, const T& c, const T& d)
36   { return max(max(max(a, b), c), d); }
37   static int sign(const T& a)
38   {
39     if (a < -SGLimits<T>::min())
40       return -1;
41     else if (SGLimits<T>::min() < a)
42       return 1;
43     else
44       return 0;
45   }
46
47   static T rad2deg(const T& val)
48   { return val*180/pi(); }
49   static T deg2rad(const T& val)
50   { return val*pi()/180; }
51
52   static T round(const T& v)
53   { return floor(v + T(0.5)); }
54   static int roundToInt(const T& v)
55   { return int(round(v)); }
56
57 #ifndef NDEBUG
58   /// Returns true if v is a NaN value
59   /// Use with care: allways code that you do not need to use that!
60   static bool isNaN(const T& v)
61   {
62 #ifdef HAVE_ISNAN
63     return isnan(v);
64 #elif defined HAVE_STD_ISNAN
65     return std::isnan(v);
66 #else
67     // Use that every compare involving a NaN returns false
68     // But be careful, some usual compiler switches like for example
69     // -fast-math from gcc might optimize that expression to v != v which
70     // behaves exactly like the opposite ...
71     return !(v == v);
72 #endif
73   }
74 #endif
75 };
76
77 #endif