KrisLibrary  1.0.0
math.h
Go to the documentation of this file.
1 #ifndef MATH_MATH_H
2 #define MATH_MATH_H
3 
4 #if defined(__APPLE__) || defined(MACOSX)
5 #include "/usr/include/math.h"
6 #else
7 #include <math.h>
8 #endif
9 
10 #include <float.h>
11 #include <limits>
12 #include <KrisLibrary/utils.h> //for Max/Min definition
13 #include "infnan.h"
14 
19 #if defined (__SVR4) && defined (__sun)
20 #include <ieeefp.h>
21 
22 static inline int isinf(double x) { return !finite(x) && x==x; }
23 static inline int isinff(double x) { return isinf(x); }
24 static inline int finitef(double x) { return finite(x); }
25 
26 static inline float acosf (float x) { return acos(x); }
27 static inline float asinf (float x) { return asin(x); }
28 static inline float atanf (float x) { return atan(x); }
29 static inline float atan2f (float x, float y) { return atan2(x, y); }
30 static inline float ceilf (float x) { return ceil(x); }
31 static inline float cosf (float x) { return cos(x); }
32 static inline float coshf (float x) { return cosh(x); }
33 static inline float expf (float x) { return exp(x); }
34 static inline float fabsf (float x) { return fabs(x); }
35 static inline float fmodf (float x, float y) { return fmod(x, y); }
36 static inline float floorf (float x) { return floor (x); }
37 static inline float logf (float x) { return log (x); }
38 static inline float sinf (float x) { return sin (x); }
39 static inline float sinhf (float x) { return sinh (x); }
40 static inline float sqrtf (float x) { return sqrt (x); }
41 static inline float powf (float x, float y) { return pow (x,y); }
42 static inline float tanf (float x) { return tan (x); }
43 static inline float tanhf (float x) { return tanh(x); }
44 #endif
45 
65 #define MATH_DOUBLE 1
66 
70 namespace Math {
71 
74 
75 const static double dZero = 0.0;
76 const static double dOne = 1.0;
77 const static double dTwo = 2.0;
78 const static double dHalf = 0.5;
79 const static double dE = 2.71828182845904523536028747135266;
80 const static double dLog2e = 1.4426950408889634073599246810018921; //log_2 e
81 const static double dLog10e = 0.4342944819032518276511289189166051; //log_10 e
82 const static double dLn2 = 0.6931471805599453094172321214581766; //log_e 2
83 const static double dLn10 = 2.3025850929940456840179914546843642; //log_e 10
84 const static double dPi = 3.1415926535897932384626433832795;
85 const static double dTwoPi = 6.2831853071795864769252867665590;
86 const static double dPi_2 = 1.5707963267948966192313216916397;
87 const static double dPi_4 = 0.7853981633974483096156608458198757;
88 const static double dSqrt2 = 1.4142135623730950488016887242096981;
89 const static double dSqrt1_2 = 0.7071067811865475244008443621048490;
90 const static double dEpsilon = 1e-8;
91 const static double dDtoRConst = 0.017453292519943295769236907684886;
92 const static double dRtoDConst = 57.295779513082320876798154814105;
93 
94 const static float fZero = 0.0f;
95 const static float fOne = 1.0f;
96 const static float fTwo = 2.0f;
97 const static float fHalf = 0.5f;
98 const static float fE = 2.718281828f;
99 const static float fLog2e = 1.4426950408f; //log_2 e
100 const static float fLog10e = 0.4342944819f; //log_10 e
101 const static float fLn2 = 0.69314718056f; //log_e 2
102 const static float fLn10 = 2.302585092994f; //log_e 10
103 const static float fPi = 3.141592654f;
104 const static float fTwoPi = 6.283185307f;
105 const static float fPi_2 = 1.5707963267f;
106 const static float fPi_4 = 0.7853981634f;
107 const static float fSqrt2 = 1.4142135624f;
108 const static float fSqrt1_2 = 0.70710678119f;
109 const static float fEpsilon = 1e-5f;
110 const static float fDtoRConst = 0.0174532925f;
111 const static float fRtoDConst = 57.29577951f;
112 
113 #ifdef MATH_DOUBLE
114 
115 typedef double Real;
116 
117 const static Real Zero = dZero;
118 const static Real One = dOne;
119 const static Real Two = dTwo;
120 const static Real Half = dHalf;
121 const static Real E = dE;
122 const static Real Log2e = dLog2e; //log_2 e
123 const static Real Log10e = dLog10e; //log_10 e
124 const static Real Ln2 = dLn2; //log_e 2
125 const static Real Ln10 = dLn10; //log_e 10
126 const static Real Pi = dPi;
127 const static Real TwoPi = dTwoPi;
128 const static Real Pi_2 = dPi_2;
129 const static Real Pi_4 = dPi_4;
130 const static Real Sqrt2 = dSqrt2;
131 const static Real Sqrt1_2 = dSqrt1_2;
132 const static Real Epsilon = dEpsilon;
133 const static Real DtoRConst = dDtoRConst;
134 const static Real RtoDConst = dRtoDConst;
135 const static Real Inf = dInf;
136 
137 #else
138 
139 typedef float Real;
140 
141 const static Real Zero = fZero;
142 const static Real One = fOne;
143 const static Real Two = fTwo;
144 const static Real Half = fHalf;
145 const static Real E = fE;
146 const static Real Log2e = fLog2e; //log_2 e
147 const static Real Log10e = fLog10e; //log_10 e
148 const static Real Ln2 = fLn2; //log_e 2
149 const static Real Ln10 = fLn10; //log_e 10
150 const static Real Pi = fPi;
151 const static Real TwoPi = fTwoPi;
152 const static Real Pi_2 = fPi_2;
153 const static Real Pi_4 = fPi_4;
154 const static Real Sqrt2 = fSqrt2;
155 const static Real Sqrt1_2 = fSqrt1_2;
156 const static Real Epsilon = fEpsilon;
157 const static Real DtoRConst = fDtoRConst;
158 const static Real RtoDConst = fRtoDConst;
159 const static Real Inf = fInf;
160 
161 #endif //MATH_DOUBLE
162 
163 //math.h aliases
164 inline double Abs(double x) { return fabs(x); }
165 inline double Sqr(double x) { return x*x; }
166 inline double Sqrt(double x) { return sqrt(x); }
167 inline double Exp(double x) { return exp(x); }
168 inline double Log(double x) { return log(x); }
169 inline double Pow(double x, double y) { return pow(x,y); }
170 inline double Sin(double x) { return sin(x); }
171 inline double Cos(double x) { return cos(x); }
172 inline double Tan(double x) { return tan(x); }
173 inline double Sinh(double x) { return sinh(x); }
174 inline double Cosh(double x) { return cosh(x); }
175 inline double Tanh(double x) { return tanh(x); }
176 inline double Asin(double x) { return asin(x); }
177 inline double Acos(double x) { return acos(x); }
178 inline double Atan(double x) { return atan(x); }
179 inline double Atan2(double y, double x) { return atan2(y,x); }
180 inline double Floor(double x) { return floor(x); }
181 inline double Ceil(double x) { return ceil(x); }
182 inline double Mod(double x, double y) { return fmod(x,y); }
183 
184 inline float Abs(float x) { return fabsf(x); }
185 inline float Sqr(float x) { return x*x; }
186 inline float Sqrt(float x) { return sqrtf(x); }
187 inline float Exp(float x) { return expf(x); }
188 inline float Log(float x) { return logf(x); }
189 inline float Pow(float x, float y) { return powf(x,y); }
190 inline float Sin(float x) { return sinf(x); }
191 inline float Cos(float x) { return cosf(x); }
192 inline float Tan(float x) { return tanf(x); }
193 inline float Sinh(float x) { return sinhf(x); }
194 inline float Cosh(float x) { return coshf(x); }
195 inline float Tanh(float x) { return tanhf(x); }
196 inline float Asin(float x) { return asinf(x); }
197 inline float Acos(float x) { return acosf(x); }
198 inline float Atan(float x) { return atanf(x); }
199 inline float Atan2(float y, float x) { return atan2f(y,x); }
200 inline float Floor(float x) { return floorf(x); }
201 inline float Ceil(float x) { return ceilf(x); }
202 inline float Mod(float x, float y) { return fmodf(x,y); }
203 
205 inline double Inv(double x) { return dOne/x; }
207 inline double Sign(double x) { return (x>0 ? dOne : (x<0 ? -dOne : 0)); }
209 inline double Clamp(double x, double a, double b) { return (x<a? a : (x>b? b : x)); }
210 inline double Trunc(double x) { return (x>=0 ? Floor(x) : -Floor(-x)); }
211 inline double Frac(double x) { return x - Trunc(x); }
212 
213 inline float Inv(float x) { return fOne/x; }
214 inline float Sign(float x) { return (x>0 ? fOne : (x<0 ? -fOne : 0)); }
215 inline float Clamp(float x, float a, float b) { return (x<a? a : (x>b? b : x)); }
216 inline float Trunc(float x) { return (x>=0 ? Floor(x) : -Floor(-x)); }
217 inline float Frac(float x) { return x - Trunc(x); }
218 
220 inline double dot(double a, double b) { return a*b; }
222 inline bool FuzzyEquals(double a, double b, double eps=dEpsilon) { return Abs(a-b) <= eps; }
224 inline bool FuzzyZero(double a, double eps=dEpsilon) { return Abs(a) <= eps; }
226 inline double PseudoInv(double x, double eps=dZero) { return (FuzzyZero(x,eps)?dZero:dOne/x); }
227 inline float dot(float a, float b) { return a*b; }
228 inline bool FuzzyEquals(float a, float b, float eps=fEpsilon) { return Abs(a-b) <= eps; }
229 inline bool FuzzyZero(float a, float eps=fEpsilon) { return Abs(a) <= eps; }
230 inline float PseudoInv(float x, float eps=fZero) { return (FuzzyZero(x,eps)?fZero:fOne/x); }
231 
233 template <class T> inline Real Delta(T i, T j) { return (i==j? One : Zero); }
234 template <class T> inline Real Delta(T x) { return (x==0? One : Zero); }
235 
237 inline double DtoR(double f) { return f*dDtoRConst; }
239 inline double RtoD(double f) { return f*dRtoDConst; }
240 inline float DtoR(float f) { return f*fDtoRConst; }
241 inline float RtoD(float f) { return f*fRtoDConst; }
242 
244 } //namespace Math
245 
246 #endif
Common math typedefs, constants, functions.
Cross-platform infinity and not-a-number routines.
Real Delta(T i, T j)
Kronecker delta.
Definition: math.h:233
Utilities commonly used throughout a program.
double RtoD(double f)
Radian to degree conversion.
Definition: math.h:239
double Clamp(double x, double a, double b)
Returns a if x < a, b if x > b, otherwise x.
Definition: math.h:209
Contains all definitions in the Math package.
Definition: WorkspaceBound.h:12
double DtoR(double f)
Degree to radian conversion.
Definition: math.h:237
double Sign(double x)
Returns {-1,0,1} if x is {<0,0,>0}.
Definition: math.h:207