31 #ifndef CORE_MATH_MISCMATH_H
32 #define CORE_MATH_MISCMATH_H 1
37 #include <boost/math/special_functions/fpclassify.hpp>
40 #include <boost/test/floating_point_comparison.hpp>
41 namespace btt=boost::test_tools;
44 #define M_PI 3.14159265358979323846
49 inline double acosh(
double x)
51 return (x<1) ? log(-1.0) : log(x+sqrt(x*x-1));
60 btt::close_at_tolerance<T> comp(btt::percent_tolerance(std::numeric_limits<T>::epsilon()));
70 inline unsigned long long Min(
unsigned long long d1,
unsigned long long d2)
75 inline unsigned long long Max(
unsigned long long d1,
unsigned long long d2)
81 inline int Min(
int d1,
int d2)
86 inline int Max(
int d1,
int d2)
92 inline unsigned int Min(
unsigned int d1,
unsigned int d2)
97 inline unsigned int Max(
unsigned int d1,
unsigned int d2)
103 inline long Min(
long d1,
long d2)
108 inline long Max(
long d1,
long d2)
115 inline long long Min(
long long d1,
long long d2)
120 inline long long Max(
long long d1,
long long d2)
126 inline float Max(
float d1,
float d2)
131 inline float Min(
float d1,
float d2)
137 inline double Max(
double d1,
double d2)
142 inline double Min(
double d1,
double d2)
148 inline unsigned long long Min(
unsigned long long d1,
unsigned long long d2,
unsigned long long d3)
150 unsigned long long m=d1<d2?d1:d2;
155 inline unsigned long long Mid(
unsigned long long a,
unsigned long long b,
unsigned long long c)
157 return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) : \
158 ((b < c) ? b : ((a > c) ? a : c)));
161 inline unsigned long long Max(
unsigned long long d1,
unsigned long long d2,
unsigned long long d3)
163 unsigned long long m=d1>d2?d1:d2;
169 inline double Min(
double d1,
double d2,
double d3)
171 double m=d1<d2?d1:d2;
176 inline double Mid(
double a,
double b,
double c)
178 return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
179 ((b < c) ? b : ((a > c) ? a : c)));
182 inline double Max(
double d1,
double d2,
double d3)
184 double m=d1>d2?d1:d2;
190 inline int Min(
int d1,
int d2,
int d3)
197 inline int Mid(
int a,
int b,
int c)
199 return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
200 ((b < c) ? b : ((a > c) ? a : c)));
203 inline int Max(
int d1,
int d2,
int d3)
211 inline unsigned int Min(
unsigned int d1,
215 unsigned int m=d1<d2?d1:d2;
220 inline unsigned int Mid(
unsigned int a,
224 return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
225 ((b < c) ? b : ((a > c) ? a : c)));
228 inline unsigned int Max(
unsigned int d1,
232 unsigned int m=d1>d2?d1:d2;
238 inline long Min(
long d1,
long d2,
long d3)
245 inline long Mid(
long a,
long b,
long c)
247 return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
248 ((b < c) ? b : ((a > c) ? a : c)));
252 inline long Max(
long d1,
long d2,
long d3)
260 inline long long Min(
long long d1,
long long d2,
long long d3)
262 long long m=d1<d2?d1:d2;
267 inline long long Mid(
long long a,
long long b,
long long c)
269 return ((a > b) ? ((a < c) ? a : ((b > c) ? b : c)) :
270 ((b < c) ? b : ((a > c) ? a : c)));
274 inline long long Max(
long long d1,
long long d2,
long long d3)
276 long long m=d1>d2?d1:d2;
284 inline double Pow(
double d,
double p)
289 inline double Pow(
double x,
unsigned int p)
301 inline double Pow(
double x,
int p)
334 return static_cast<int>(sqrt(static_cast<double>(i)));
349 return pow(d, 1.0/3.0);
354 return static_cast<float>(pow(static_cast<double>(d), 1.0/3.0));
357 inline double Sqr(
double x)
369 inline double Abs(
double d)
404 inline double Clamp(
double d,
double min,
double max)
406 return d<=min?min:d>=max?max:d;
411 return f<=min?min:f>=max?max:f;
416 return i<min?min:i>max?max:i;
424 return T(d2*weight+d1*(1.0-weight));
430 return T(d2*weight+d1*(1-weight));
435 return static_cast<int>(d+0.5);
440 return static_cast<int>(f+0.5);
447 int i=-
static_cast<int>(-d);
448 if(static_cast<double>(i) == d)
455 return (static_cast<int>(d));
463 int i=-
static_cast<int>(-f);
464 if(static_cast<float>(i) == f)
471 return (static_cast<int>(f));
480 inline int Floor(
double d,
int offset)
482 return (static_cast<int>((d + offset) - offset));
485 inline int Floor(
float f,
int offset)
487 return (static_cast<int>((f + offset) - offset));
494 int i=-
static_cast<int>(-d);
499 int i=
static_cast<int>(d);
500 if(static_cast<double>(i) == d)
511 int i=-
static_cast<int>(-f);
516 int i=
static_cast<int>(f);
517 if(static_cast<float>(i) == f)
528 inline int Ceil(
double d,
int offset)
530 return (static_cast<int>((d - offset) + offset));
533 inline int Ceil(
float f,
int offset)
535 return (static_cast<int>((f - offset) + offset));
540 return boost::math::isnan(val);
545 return boost::math::isfinite(val);
550 return boost::math::isinf(val);
556 return ((n & (n-1)) == 0);
561 return std::numeric_limits<SCIRun::size_type>::min() <= x &&
562 x <= std::numeric_limits<SCIRun::size_type>::max() &&
563 x == static_cast<SCIRun::size_type>(x);
574 unsigned int val = 4;
575 while (val < dim) val = val << 1;;
585 return Pow2(dim) >> 1;
593 unsigned int log = 0;
594 unsigned int val = 1;
595 while (val < dim) { val = val << 1; log++; };
603 inline double Cot(
double d)
610 return d*(
M_PI/180.);
615 return r*(180./
M_PI);
620 return d*
static_cast<float>(
M_PI/180.0);
625 return r*
static_cast<float>(180.0/
M_PI);
int Sign(double d)
Definition: MiscMath.h:386
int Ceil(double d)
Definition: MiscMath.h:490
bool IsNan(double val)
Definition: MiscMath.h:538
double Abs(double d)
Definition: MiscMath.h:369
bool IsPowerOf2(unsigned int n)
Definition: MiscMath.h:554
unsigned int Log2(const unsigned int dim)
Definition: MiscMath.h:591
double RtoD(double r)
Definition: MiscMath.h:613
double Clamp(double d, double min, double max)
Definition: MiscMath.h:404
double Sqr(double x)
Definition: MiscMath.h:357
double Cot(double d)
Definition: MiscMath.h:603
bool IsInfinite(double val)
Definition: MiscMath.h:548
double Pow(double d, double p)
Definition: MiscMath.h:284
bool nonzero(T d)
Definition: MiscMath.h:58
int Round(double d)
Definition: MiscMath.h:433
Definition: ParallelLinearAlgebraTests.cc:358
int Sqrt(int i)
Definition: MiscMath.h:332
void findFactorsNearRoot(const int value, int &factor1, int &factor2)
Definition: MiscMath.cc:51
unsigned long long Mid(unsigned long long a, unsigned long long b, unsigned long long c)
Definition: MiscMath.h:155
double Cbrt(double d)
Definition: MiscMath.h:347
double DtoR(double d)
Definition: MiscMath.h:608
#define M_PI
Definition: MiscMath.h:44
unsigned int Pow2(const unsigned int dim)
Definition: MiscMath.h:571
bool IsFinite(double val)
Definition: MiscMath.h:543
unsigned long long Max(unsigned long long d1, unsigned long long d2)
Definition: MiscMath.h:75
T Interpolate(T d1, T d2, double weight)
Definition: MiscMath.h:422
unsigned long long Min(unsigned long long d1, unsigned long long d2)
Faster versions of several math functions.
Definition: MiscMath.h:70
int n
Definition: eab.py:9
unsigned int LargestPowerOf2(const unsigned int dim)
Definition: MiscMath.h:582
int Floor(double d)
Definition: MiscMath.h:443
bool is_integral_value(double x)
Definition: MiscMath.h:559