BALL  1.5.0
MATHS/common.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 // $Id: common.h,v 1.29.16.1 2007/03/25 21:23:45 oliver Exp $
5 //
6 
7 #ifndef BALL_MATHS_COMMON_H
8 #define BALL_MATHS_COMMON_H
9 
10 #ifndef BALL_CONFIG_CONFIG_H
11 # include <BALL/CONFIG/config.h>
12 #endif
13 
14 #include <cmath>
15 
16 #ifdef BALL_HAS_IEEEFP_H
17 # include <ieeefp.h>
18 #endif
19 
20 #ifndef BALL_COMMON_CONSTANTS_H
21 # include <BALL/COMMON/constants.h>
22 #endif
23 
24 #ifndef BALL_COMMON_GLOBAL_H
25 # include <BALL/COMMON/global.h>
26 #endif
27 
28 #ifndef BALL_COMMON_MACROS_H
29 # include <BALL/COMMON/macros.h>
30 #endif
31 
32 namespace BALL
33 {
34 
35  namespace Maths
36  {
37 
43 
48  template <typename T>
49  inline
50  T abs(const T& t)
51  {
52  return t >= 0 ? t : -t;
53  }
54 
59  template <typename T>
60  inline
61  T frac(const T& t)
62  {
63  long tmp = (long)t;
64  return (t - (T)tmp);
65  }
66 
67 #ifndef max
68 
73  template <typename T>
74  inline
75  T max(const T& a, const T& b)
76  {
77  return a < b ? b : a;
78  }
79 
86  template <typename T>
87  inline
88  T max(const T& a, const T& b, const T &ct)
89  {
90  return a > b ? max(a, ct) : max(b, ct);
91  }
92 #endif
93 
94 #ifndef min
95 
100  template <typename T>
101  inline
102  T min(const T& a, const T& b)
103  {
104  return a > b ? b : a;
105  }
106 
113  template <typename T>
114  inline
115  T min(const T& a, const T& b, const T &ct)
116  {
117  return a < b ? min(a, ct) : min(b, ct);
118  }
119 #endif
120 
125  template <typename T>
126  inline
127  T round(const T& t)
128  {
129  return (T)(t > 0 ? long(t + 0.5) : long(t - 0.5));
130  }
131 
136  template <typename T>
137  inline
138  T sgn(const T& t)
139  {
140  return ((t < 0) ? -1 : (t == 0) ? 0 : 1);
141  }
142 
147  template <typename T>
148  inline
149  bool isFinite(const T& t)
150  {
151  return std::isfinite(t);
152  }
153 
158  template <typename T>
159  inline
160  bool isNan(const T& t)
161  {
162  #ifdef BALL_COMPILER_MSVC
163  return (_isnan(t) != 0);
164  #elif defined(BALL_OS_DARWIN)
165  return ( __inline_isnand(t) != 0);
166  #else
167  using std::isnan;
168  return (isnan(t) != 0);
169  #endif
170  }
171 
176  template <typename T>
177  inline
178  bool isInfinite(const T& t)
179  {
180  return (!Maths::isFinite(t) && !Maths::isNan(t));
181  }
182 
187  template <typename T>
188  inline
189  bool isZero(const T& t)
190  {
191  return (abs(t) < Constants::EPSILON);
192  }
193 
198  template <typename T>
199  inline
200  bool isNotZero(const T& t)
201  {
202  return (abs(t) >= Constants::EPSILON);
203  }
204 
210  template <typename T1, typename T2>
211  inline
212  bool isEqual(const T1& a, const T2& b)
213  {
214  return (abs(a - b) < Constants::EPSILON);
215  }
216 
222  template <typename T1, typename T2>
223  inline
224  bool isNotEqual(const T1& a, const T2& b)
225  {
226  return (abs(a - b) >= Constants::EPSILON);
227  }
228 
234  template <typename T1, typename T2>
235  inline
236  bool isLess(const T1& a, const T2& b)
237 
238  {
239  return ((a - b) <= -Constants::EPSILON);
240  }
241 
247  template <typename T1, typename T2>
248  inline
249  bool isLessOrEqual(const T1& a, const T2& b)
250  {
251  return ((a - b) < Constants::EPSILON);
252  }
253 
259  template <typename T1, typename T2>
260  inline
261  bool isGreaterOrEqual(const T1& a, const T2& b)
262  {
263  return ((a - b) > -Constants::EPSILON);
264  }
265 
271  template <typename T1, typename T2>
272  inline
273  bool isGreater(const T1& a, const T2& b)
274  {
275  return (a - b >= Constants::EPSILON);
276  }
277 
282  template <typename T>
283  inline
284  long floor(const T& t)
285  {
286  return (long)(Maths::isGreater(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t - 1));
287  }
288 
293  template <typename T>
294  inline
295  long ceiling(const T& t)
296  {
297  return (long)(Maths::isLess(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t + 1));
298  }
299 
305  template <typename T1, typename T2>
306  inline
307  Index compare(const T1& a, const T2& b)
308  {
309  return (Maths::isLess(a, b) ? -1 : Maths::isEqual(a, b) ? 0 : 1);
310  }
311 
318  template <typename T>
319  inline
320  bool isNear(const T& a, const T& b, const T& max_diff)
321  {
322  return (abs((double)a - (double)b) < abs((double)max_diff));
323  }
324 
325 
327  inline double rint(double x)
328  {
329  if (x < 0.0) return (double)(int)(x - 0.5);
330  else return (double)(int)(x + 0.5);
331  }
332 
334 
335  } // namespace Maths
336 } // namespace BALL
337 
338 #endif // BALL_MATHS_COMMON_H
BALL_EXTERN_VARIABLE double EPSILON
Definition: constants.h:43
bool isLess(const T1 &a, const T2 &b)
Definition: MATHS/common.h:236
T max(const T &a, const T &b)
Definition: MATHS/common.h:75
T sgn(const T &t)
Definition: MATHS/common.h:138
bool isNotZero(const T &t)
Definition: MATHS/common.h:200
bool isInfinite(const T &t)
Definition: MATHS/common.h:178
bool isEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:212
bool isGreater(const T1 &a, const T2 &b)
Definition: MATHS/common.h:273
Index compare(const T1 &a, const T2 &b)
Definition: MATHS/common.h:307
bool isFinite(const T &t)
Definition: MATHS/common.h:149
long floor(const T &t)
Definition: MATHS/common.h:284
bool isGreaterOrEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:261
T abs(const T &t)
Definition: MATHS/common.h:50
T min(const T &a, const T &b)
Definition: MATHS/common.h:102
bool isNear(const T &a, const T &b, const T &max_diff)
Definition: MATHS/common.h:320
bool isNan(const T &t)
Definition: MATHS/common.h:160
Definition: constants.h:12
bool isLessOrEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:249
long ceiling(const T &t)
Definition: MATHS/common.h:295
bool isZero(const T &t)
Definition: MATHS/common.h:189
double rint(double x)
round to integral value in floating-point format
Definition: MATHS/common.h:327
T frac(const T &t)
Definition: MATHS/common.h:61
T round(const T &t)
Definition: MATHS/common.h:127
bool isNotEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:224