BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 #ifdef BALL_HAS_FLOAT_H
21 # include <float.h>
22 #endif
23 
24 #ifndef BALL_COMMON_CONSTANTS_H
25 # include <BALL/COMMON/constants.h>
26 #endif
27 
28 #ifndef BALL_COMMON_GLOBAL_H
29 # include <BALL/COMMON/global.h>
30 #endif
31 
32 #ifndef BALL_COMMON_MACROS_H
33 # include <BALL/COMMON/macros.h>
34 #endif
35 
36 namespace BALL
37 {
38 
39  namespace Maths
40  {
41 
47 
52  template <typename T>
53  inline
54  T abs(const T& t)
55  {
56  return BALL_ABS(t);
57  }
58 
63  template <typename T>
64  inline
65  T frac(const T& t)
66  {
67  long tmp = (long)t;
68  return (t - (T)tmp);
69  }
70 
71 #ifndef max
72 
77  template <typename T>
78  inline
79  T max(const T& a, const T& b)
80  {
81  return BALL_MAX(a, b);
82  }
83 
90  template <typename T>
91  inline
92  T max(const T& a, const T& b, const T &ct)
93  {
94  return BALL_MAX3(a, b, ct);
95  }
96 #endif
97 
98 #ifndef min
99 
104  template <typename T>
105  inline
106  T min(const T& a, const T& b)
107  {
108  return BALL_MIN(a, b);
109  }
110 
117  template <typename T>
118  inline
119  T min(const T& a, const T& b, const T &ct)
120  {
121  return BALL_MIN3(a, b, ct);
122  }
123 #endif
124 
129  template <typename T>
130  inline
131  T round(const T& t)
132  {
133  return (T)(t > 0 ? long(t + 0.5) : long(t - 0.5));
134  }
135 
140  template <typename T>
141  inline
142  T sgn(const T& t)
143  {
144  return BALL_SGN(t);
145  }
146 
151  template <typename T>
152  inline
153  bool isFinite(const T& t)
154  {
155 #ifdef BALL_COMPILER_MSVC
156  return ::_finite(t);
157 #else
158  return finite(t);
159 #endif
160  }
161 
166  template <typename T>
167  inline
168  bool isNan(const T& t)
169  {
170  #ifdef BALL_COMPILER_MSVC
171  return (_isnan(t) != 0);
172  #elif defined(BALL_OS_DARWIN)
173  return ( __inline_isnand(t) != 0);
174  #else
175  return (isnan(t) != 0);
176  #endif
177  }
178 
183  template <typename T>
184  inline
185  bool isInfinite(const T& t)
186  {
187  return (!Maths::isFinite(t) && !Maths::isNan(t));
188  }
189 
194  template <typename T>
195  inline
196  bool isZero(const T& t)
197  {
198  return (abs(t) < Constants::EPSILON);
199  }
200 
205  template <typename T>
206  inline
207  bool isNotZero(const T& t)
208  {
209  return (abs(t) >= Constants::EPSILON);
210  }
211 
217  template <typename T1, typename T2>
218  inline
219  bool isEqual(const T1& a, const T2& b)
220  {
221  return (abs(a - b) < Constants::EPSILON);
222  }
223 
229  template <typename T1, typename T2>
230  inline
231  bool isNotEqual(const T1& a, const T2& b)
232  {
233  return (abs(a - b) >= Constants::EPSILON);
234  }
235 
241  template <typename T1, typename T2>
242  inline
243  bool isLess(const T1& a, const T2& b)
244 
245  {
246  return ((a - b) <= -Constants::EPSILON);
247  }
248 
254  template <typename T1, typename T2>
255  inline
256  bool isLessOrEqual(const T1& a, const T2& b)
257  {
258  return ((a - b) < Constants::EPSILON);
259  }
260 
266  template <typename T1, typename T2>
267  inline
268  bool isGreaterOrEqual(const T1& a, const T2& b)
269  {
270  return ((a - b) > -Constants::EPSILON);
271  }
272 
278  template <typename T1, typename T2>
279  inline
280  bool isGreater(const T1& a, const T2& b)
281  {
282  return (a - b >= Constants::EPSILON);
283  }
284 
289  template <typename T>
290  inline
291  long floor(const T& t)
292  {
293  return (long)(Maths::isGreater(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t - 1));
294  }
295 
300  template <typename T>
301  inline
302  long ceiling(const T& t)
303  {
304  return (long)(Maths::isLess(t, 0) ? t: (Maths::isEqual(t, (T)(long)t) ? t : t + 1));
305  }
306 
312  template <typename T1, typename T2>
313  inline
314  Index compare(const T1& a, const T2& b)
315  {
316  return (Maths::isLess(a, b) ? -1 : Maths::isEqual(a, b) ? 0 : 1);
317  }
318 
325  template <typename T>
326  inline
327  bool isNear(const T& a, const T& b, const T& max_diff)
328  {
329  return (abs((double)a - (double)b) < abs((double)max_diff));
330  }
331 
332 
334  inline double rint(double x)
335  {
336  if (x < 0.0) return (double)(int)(x - 0.5);
337  else return (double)(int)(x + 0.5);
338  }
339 
341 
342  } // namespace Maths
343 } // namespace BALL
344 
345 #endif // BALL_MATHS_COMMON_H
#define BALL_MAX(a, b)
Definition: macros.h:23
#define BALL_ABS(x)
Definition: macros.h:27
bool isLessOrEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:256
bool isEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:219
#define BALL_MAX3(x, y, z)
Definition: macros.h:24
long floor(const T &t)
Definition: MATHS/common.h:291
#define BALL_MIN(a, b)
Definition: macros.h:25
bool isNear(const T &a, const T &b, const T &max_diff)
Definition: MATHS/common.h:327
long ceiling(const T &t)
Definition: MATHS/common.h:302
bool isNotZero(const T &t)
Definition: MATHS/common.h:207
T frac(const T &t)
Definition: MATHS/common.h:65
bool isZero(const T &t)
Definition: MATHS/common.h:196
bool isGreaterOrEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:268
bool isFinite(const T &t)
Definition: MATHS/common.h:153
BALL_EXTERN_VARIABLE double EPSILON
Definition: constants.h:43
T abs(const T &t)
Definition: MATHS/common.h:54
double rint(double x)
round to integral value in floating-point format
Definition: MATHS/common.h:334
bool isNan(const T &t)
Definition: MATHS/common.h:168
T max(const T &a, const T &b)
Definition: MATHS/common.h:79
T round(const T &t)
Definition: MATHS/common.h:131
bool isGreater(const T1 &a, const T2 &b)
Definition: MATHS/common.h:280
bool isInfinite(const T &t)
Definition: MATHS/common.h:185
bool isNotEqual(const T1 &a, const T2 &b)
Definition: MATHS/common.h:231
bool isLess(const T1 &a, const T2 &b)
Definition: MATHS/common.h:243
#define BALL_SGN(x)
Definition: macros.h:28
#define BALL_MIN3(x, y, z)
Definition: macros.h:26
T sgn(const T &t)
Definition: MATHS/common.h:142
Index compare(const T1 &a, const T2 &b)
Definition: MATHS/common.h:314
T min(const T &a, const T &b)
Definition: MATHS/common.h:106