Scopira 20080306

math.h

00001 
00002 /*
00003  *  Copyright (c) 2002-2003    National Research Council
00004  *
00005  *  All rights reserved.
00006  *
00007  *  This material is confidential and proprietary information of
00008  *  National Research Council Canada ("Confidential Information").
00009  *  This Confidential Information may only be used and reproduced
00010  *  in accordance with the terms of the license agreement.
00011  *
00012  */
00013 
00014 #ifndef __INCLUDED_SCOPIRA_BASEKIT_MATH_H__
00015 #define __INCLUDED_SCOPIRA_BASEKIT_MATH_H__
00016 
00017 #include <scopira/tool/platform.h>
00018 
00019 #ifdef PLATFORM_win32
00020 #ifndef _USE_MATH_DEFINES
00021 #define _USE_MATH_DEFINES
00022 #endif
00023 #include <float.h>
00024 #endif
00025 
00026 #include <assert.h>
00027 #include <math.h>
00028 
00029 #ifdef PLATFORM_osx
00030 #include <cmath>
00031 #endif
00032 
00033 #include <scopira/tool/platform.h>
00034 
00035 // should these be in this file?
00036 #ifndef TINYNUM
00037 #define TINYNUM (1.0e-20)
00038 #endif
00039 
00040 #ifndef SMALLNUM
00041 #define SMALLNUM (1.0e-5)
00042 #endif
00043 
00044 #define IMIN(a,b) ((a) < (b) ? (a) : (b))
00045 #define FMAX(a,b) ((a) > (b) ? (a) : (b))
00046 #define SQR(a) ((a) == 0.0 ? 0.0 : a*a)
00047 #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
00048 #define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
00049 
00050 #ifndef M_E
00051 #define M_E 2.71828182845904523536
00052 #endif
00053 #ifndef M_LOG2E
00054 #define M_LOG2E 1.44269504088896340736
00055 #endif
00056 #ifndef M_LOG10E
00057 #define M_LOG10E 0.434294481903251827651
00058 #endif
00059 #ifndef M_LN2
00060 #define M_LN2 0.693147180559945309417
00061 #endif
00062 #ifndef M_LN10
00063 #define M_LN10 2.30258509299404568402
00064 #endif
00065 #ifndef M_PI
00066 #define M_PI 3.14159265358979323846
00067 #endif
00068 #ifndef M_PI_2
00069 #define M_PI_2 1.57079632679489661923
00070 #endif
00071 #ifndef M_PI_4
00072 #define M_PI_4 0.785398163397448309616
00073 #endif
00074 #ifndef M_1_PI
00075 #define M_1_PI 0.318309886183790671538
00076 #endif
00077 #ifndef M_2_PI
00078 #define M_2_PI 0.636619772367581343076
00079 #endif
00080 #ifndef M_2_SQRTPI
00081 #define M_2_SQRTPI 1.12837916709551257390
00082 #endif
00083 #ifndef M_SQRT2
00084 #define M_SQRT2 1.41421356237309504880
00085 #endif
00086 #ifndef M_SQRT1_2
00087 #define M_SQRT1_2 0.707106781186547524401
00088 #endif
00089 
00090 namespace scopira
00091 {
00092   namespace basekit
00093   {
00099     const double tol_c = 0.0000001;
00105     const double tinynum_c = 1.0e-20;
00111     const double smallnum_c = 1.0e-5;
00112 
00117     template <class T>
00118       inline T sqr(T v) { return v*v; }
00124     template <class T>
00125       inline bool is_equal(T v1, T v2) { T tmp(v1 - v2); return tmp<tol_c && tmp> (-tol_c); }
00131     template <class T>
00132       inline bool is_zero(T v) { return v<tol_c && v> (-tol_c); }
00133 #ifdef PLATFORM_win32
00134     inline bool is_nan(double v) { return _isnan(v) != 0; }
00135     inline bool is_nan(float v) { return _isnan(v) != 0; }//incase is*() is a macro, which it often is
00136 #elif defined(PLATFORM_osx)
00137     // mac lacks (utleast under certain builds) isnan... supposed v != v -> true for nan-v is standard
00138     //inline bool is_nan(double v) { return v != v; }
00139     //inline bool is_nan(float v) { return v != v; }
00140     inline bool is_nan(double v) { return std::isnan(v); }
00141     inline bool is_nan(float v) { return std::isnan(v); }//incase is*() is a macro, which it often is
00142 #else
00143 
00148     inline bool is_nan(double v) { return isnan(v) != 0; }
00154     inline bool is_nan(float v) { return isnan(v) != 0; }//incase is*() is a macro, which it often is
00155 #endif
00156 
00157 #ifdef PLATFORM_win32
00158     inline int is_inf(double v) { return !_finite(v) && !_isnan(v); }
00159     inline int is_inf(float v) { return !_finite(v) && !_isnan(v); }
00160 #else
00161 #ifdef PLATFORM_irix
00162     // surely irix has an implmentation of these?
00163     inline int is_inf(double v) { assert(false); return 0; }
00164     inline int is_inf(float v) { assert(false); return 0; }
00165 #elif defined(PLATFORM_osx)
00166     inline int is_inf(double v) { return std::isinf(v); }
00167     inline int is_inf(float v) { return std::isinf(v); }
00168 #else
00169 
00174     inline int is_inf(double v) { return isinf(v); }
00180     inline int is_inf(float v) { return isinf(v); }//incase is*() is a macro, which it often is
00181 #endif
00182 #endif
00183 
00190     template <class T>
00191       inline T sign(T v) { if (v>0) return 1; if (v<0) return -1; return 0; }
00192       
00193     //http://more.sourceforge.net/doc/html/math_8h-source.html
00203     int factorial(int n, int m = 0);
00212     int binomial(int n, int i);
00213   }
00214 }
00215 
00216 #endif
00217