Scopira  20080306
math.h
1 
2 /*
3  * Copyright (c) 2002-2003 National Research Council
4  *
5  * All rights reserved.
6  *
7  * This material is confidential and proprietary information of
8  * National Research Council Canada ("Confidential Information").
9  * This Confidential Information may only be used and reproduced
10  * in accordance with the terms of the license agreement.
11  *
12  */
13 
14 #ifndef __INCLUDED_SCOPIRA_BASEKIT_MATH_H__
15 #define __INCLUDED_SCOPIRA_BASEKIT_MATH_H__
16 
17 #include <scopira/tool/platform.h>
18 
19 #ifdef PLATFORM_win32
20 #ifndef _USE_MATH_DEFINES
21 #define _USE_MATH_DEFINES
22 #endif
23 #include <float.h>
24 #endif
25 
26 #include <assert.h>
27 #include <math.h>
28 
29 #ifdef PLATFORM_osx
30 #include <cmath>
31 #endif
32 
33 #include <scopira/tool/platform.h>
34 
35 // should these be in this file?
36 #ifndef TINYNUM
37 #define TINYNUM (1.0e-20)
38 #endif
39 
40 #ifndef SMALLNUM
41 #define SMALLNUM (1.0e-5)
42 #endif
43 
44 #define IMIN(a,b) ((a) < (b) ? (a) : (b))
45 #define FMAX(a,b) ((a) > (b) ? (a) : (b))
46 #define SQR(a) ((a) == 0.0 ? 0.0 : a*a)
47 #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
48 #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
49 
50 #ifndef M_E
51 #define M_E 2.71828182845904523536
52 #endif
53 #ifndef M_LOG2E
54 #define M_LOG2E 1.44269504088896340736
55 #endif
56 #ifndef M_LOG10E
57 #define M_LOG10E 0.434294481903251827651
58 #endif
59 #ifndef M_LN2
60 #define M_LN2 0.693147180559945309417
61 #endif
62 #ifndef M_LN10
63 #define M_LN10 2.30258509299404568402
64 #endif
65 #ifndef M_PI
66 #define M_PI 3.14159265358979323846
67 #endif
68 #ifndef M_PI_2
69 #define M_PI_2 1.57079632679489661923
70 #endif
71 #ifndef M_PI_4
72 #define M_PI_4 0.785398163397448309616
73 #endif
74 #ifndef M_1_PI
75 #define M_1_PI 0.318309886183790671538
76 #endif
77 #ifndef M_2_PI
78 #define M_2_PI 0.636619772367581343076
79 #endif
80 #ifndef M_2_SQRTPI
81 #define M_2_SQRTPI 1.12837916709551257390
82 #endif
83 #ifndef M_SQRT2
84 #define M_SQRT2 1.41421356237309504880
85 #endif
86 #ifndef M_SQRT1_2
87 #define M_SQRT1_2 0.707106781186547524401
88 #endif
89 
90 namespace scopira
91 {
92  namespace basekit
93  {
99  const double tol_c = 0.0000001;
105  const double tinynum_c = 1.0e-20;
111  const double smallnum_c = 1.0e-5;
112 
117  template <class T>
118  inline T sqr(T v) { return v*v; }
124  template <class T>
125  inline bool is_equal(T v1, T v2) { T tmp(v1 - v2); return tmp<tol_c && tmp> (-tol_c); }
131  template <class T>
132  inline bool is_zero(T v) { return v<tol_c && v> (-tol_c); }
133 #ifdef PLATFORM_win32
134  inline bool is_nan(double v) { return _isnan(v) != 0; }
135  inline bool is_nan(float v) { return _isnan(v) != 0; }//incase is*() is a macro, which it often is
136 #elif defined(PLATFORM_osx)
137  // mac lacks (utleast under certain builds) isnan... supposed v != v -> true for nan-v is standard
138  //inline bool is_nan(double v) { return v != v; }
139  //inline bool is_nan(float v) { return v != v; }
140  inline bool is_nan(double v) { return std::isnan(v); }
141  inline bool is_nan(float v) { return std::isnan(v); }//incase is*() is a macro, which it often is
142 #else
143 
148  inline bool is_nan(double v) { return isnan(v) != 0; }
154  inline bool is_nan(float v) { return isnan(v) != 0; }//incase is*() is a macro, which it often is
155 #endif
156 #ifdef PLATFORM_win32
158  inline int is_inf(double v) { return !_finite(v) && !_isnan(v); }
159  inline int is_inf(float v) { return !_finite(v) && !_isnan(v); }
160 #else
161 #ifdef PLATFORM_irix
162  // surely irix has an implmentation of these?
163  inline int is_inf(double v) { assert(false); return 0; }
164  inline int is_inf(float v) { assert(false); return 0; }
165 #elif defined(PLATFORM_osx)
166  inline int is_inf(double v) { return std::isinf(v); }
167  inline int is_inf(float v) { return std::isinf(v); }
168 #else
169 
174  inline int is_inf(double v) { return isinf(v); }
180  inline int is_inf(float v) { return isinf(v); }//incase is*() is a macro, which it often is
181 #endif
182 #endif
183 
190  template <class T>
191  inline T sign(T v) { if (v>0) return 1; if (v<0) return -1; return 0; }
192 
193  //http://more.sourceforge.net/doc/html/math_8h-source.html
203  int factorial(int n, int m = 0);
212  int binomial(int n, int i);
213  }
214 }
215 
216 #endif
217 
const double tinynum_c
Definition: math.h:105
int is_inf(double v)
see man isinf for the meanin of the return
Definition: math.h:174
Definition: archiveflow.h:20
T sqr(T v)
Definition: math.h:118
bool is_nan(double v)
Definition: math.h:148
const double tol_c
Definition: math.h:99
const double smallnum_c
Definition: math.h:111
T sign(T v)
Definition: math.h:191
bool is_equal(T v1, T v2)
Definition: math.h:125
int binomial(int n, int i)
bool is_zero(T v)
Definition: math.h:132
int factorial(int n, int m=0)