]> git.mxchange.org Git - simgear.git/blob - simgear/math/SGMisc.hxx
Add dist and distSqr functions
[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 #include <cmath>
22
23 template<typename T>
24 class SGMisc {
25 public:
26   static T pi() { return T(3.1415926535897932384626433832795029L); }
27   static T min(const T& a, const T& b)
28   { return a < b ? a : b; }
29   static T min(const T& a, const T& b, const T& c)
30   { return min(min(a, b), c); }
31   static T min(const T& a, const T& b, const T& c, const T& d)
32   { return min(min(min(a, b), c), d); }
33   static T max(const T& a, const T& b)
34   { return a > b ? a : b; }
35   static T max(const T& a, const T& b, const T& c)
36   { return max(max(a, b), c); }
37   static T max(const T& a, const T& b, const T& c, const T& d)
38   { return max(max(max(a, b), c), d); }
39   static int sign(const T& a)
40   {
41     if (a < -SGLimits<T>::min())
42       return -1;
43     else if (SGLimits<T>::min() < a)
44       return 1;
45     else
46       return 0;
47   }
48
49   static T rad2deg(const T& val)
50   { return val*180/pi(); }
51   static T deg2rad(const T& val)
52   { return val*pi()/180; }
53
54 #ifndef NDEBUG
55   /// Returns true if v is a NaN value
56   /// Use with care: allways code that you do not need to use that!
57   static bool isNaN(const T& v)
58   {
59 #ifdef HAVE_ISNAN
60     return isnan(v);
61 #elif defined HAVE_STD_ISNAN
62     return std::isnan(v);
63 #else
64     // Use that every compare involving a NaN returns false
65     // But be careful, some usual compiler switches like for example
66     // -fast-math from gcc might optimize that expression to v != v which
67     // behaves exactly like the opposite ...
68     return !(v == v);
69 #endif
70   }
71 #endif
72 };
73
74 typedef SGMisc<float> SGMiscf;
75 typedef SGMisc<double> SGMiscd;
76
77 #endif