Main Page   Groups   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Concepts

vnl_math.h

Go to the documentation of this file.
00001 #ifndef vnl_math_h_
00002 #define vnl_math_h_
00003 // This is vxl/vnl/vnl_math.h
00004 
00005 //: \file
00006 //  \brief Namespace with standard math functions
00007 //  \author Andrew W. Fitzgibbon, Oxford RRG, July 13, 1996
00008 //    The vnl_math namespace provides a standard set of the simple mathematical
00009 //    functions (min, max, sqr, sgn, rnd, abs), and some predefined constants
00010 //    such as pi and e, which are not defined by the ANSI C++ standard.
00011 //
00012 //    There are complex versions defined in vnl_complex.h
00013 //
00014 //    That's right, M_PI is nonstandard!
00015 //
00016 //    Aside from e, pi and their associates the class also defines eps,
00017 //    the IEEE double machine precision.  This is the smallest number
00018 //    eps such that 1+eps != 1.
00019 //
00020 //    The operations are overloaded for int, float and double arguments,
00021 //    which in combination with inlining can make them  more efficient than
00022 //    their counterparts in the standard C library.
00023 //
00024 
00025 //      Modifications
00026 //     210598 AWF Removed conditional VCL_IMPLEMENT_STATIC_CONSTS, sometimes gcc needs them.
00027 //     LSB (Modifications) 23/1/01 Documentation tidied
00028 
00029 #include <vcl_cmath.h>
00030 #include "dll.h"
00031 
00032 //: Type-accessible infinities for use in templates.
00033 template <class T> T vnl_huge_val(T);
00034 double   vnl_huge_val(double);
00035 float    vnl_huge_val(float);
00036 long int vnl_huge_val(long int);
00037 int      vnl_huge_val(int);
00038 short    vnl_huge_val(short);
00039 char     vnl_huge_val(char);
00040 
00041 //: real numerical constants
00042 class vnl_math {
00043 public:
00044   //: pi, e and all that
00045   static VNL_DLL_DATA double const e               VCL_STATIC_CONST_INIT_FLOAT(2.7182818284590452354);
00046   static VNL_DLL_DATA double const log2e           VCL_STATIC_CONST_INIT_FLOAT(1.4426950408889634074);
00047   static VNL_DLL_DATA double const log10e          VCL_STATIC_CONST_INIT_FLOAT(0.43429448190325182765);
00048   static VNL_DLL_DATA double const ln2             VCL_STATIC_CONST_INIT_FLOAT(0.69314718055994530942);
00049   static VNL_DLL_DATA double const ln10            VCL_STATIC_CONST_INIT_FLOAT(2.30258509299404568402);
00050   static VNL_DLL_DATA double const pi              VCL_STATIC_CONST_INIT_FLOAT(3.14159265358979323846);
00051   static VNL_DLL_DATA double const pi_over_2       VCL_STATIC_CONST_INIT_FLOAT(1.57079632679489661923);
00052   static VNL_DLL_DATA double const pi_over_4       VCL_STATIC_CONST_INIT_FLOAT(0.78539816339744830962);
00053   static VNL_DLL_DATA double const one_over_pi     VCL_STATIC_CONST_INIT_FLOAT(0.31830988618379067154);
00054   static VNL_DLL_DATA double const two_over_pi     VCL_STATIC_CONST_INIT_FLOAT(0.63661977236758134308);
00055   static VNL_DLL_DATA double const two_over_sqrtpi VCL_STATIC_CONST_INIT_FLOAT(1.12837916709551257390);
00056   static VNL_DLL_DATA double const sqrt2           VCL_STATIC_CONST_INIT_FLOAT(1.41421356237309504880);
00057   static VNL_DLL_DATA double const sqrt1_2         VCL_STATIC_CONST_INIT_FLOAT(0.70710678118654752440);
00058 
00059   //: IEEE double machine precision
00060   static VNL_DLL_DATA double const eps             VCL_STATIC_CONST_INIT_FLOAT(2.2204460492503131e-16);
00061   static VNL_DLL_DATA double const sqrteps         VCL_STATIC_CONST_INIT_FLOAT(1.490116119384766e-08);
00062 
00063   //: MAX* constants.
00064   // Supplied until compilers accept the templated numeric_traits.
00065   // These are lowercase to avoid conflict with OS-defined macros.
00066   static VNL_DLL_DATA int const      maxint;
00067   static VNL_DLL_DATA long int const maxlong;
00068   static VNL_DLL_DATA double const   maxdouble;
00069   static VNL_DLL_DATA float const    maxfloat;
00070 };
00071 
00072 // isnan
00073 template <class T> inline bool vnl_math_isnan(T ) { return false; }
00074 bool vnl_math_isnan(float);
00075 bool vnl_math_isnan(double);
00076 bool vnl_math_isnan(long double);
00077 
00078 // isinf
00079 template <class T> inline bool vnl_math_isinf(T ) { return false; }
00080 bool vnl_math_isinf(float);
00081 bool vnl_math_isinf(double);
00082 bool vnl_math_isinf(long double);
00083 
00084 // isfinite
00085 template <class T> inline bool vnl_math_isfinite(T ) { return false; }
00086 bool vnl_math_isfinite(float);
00087 bool vnl_math_isfinite(double);
00088 bool vnl_math_isfinite(long double);
00089 
00090 // rnd (rounding; 0.5 rounds up)
00091 inline long vnl_math_rnd(float x) { return (x>=0.0)?(int)(x + 0.5):(int)(x - 0.5); }
00092 inline int  vnl_math_rnd(double x) { return (x>=0.0)?(int)(x + 0.5):(int)(x - 0.5); }
00093 
00094 // abs
00095 inline int      vnl_math_abs(int x) { return x < 0 ? -x : x; }
00096 inline unsigned vnl_math_abs(unsigned x) { return x; } // to avoid SunPro4.2 float/double conflict
00097 inline long     vnl_math_abs(long x) { return x < 0 ? -x : x; }
00098 inline unsigned long vnl_math_abs(unsigned long x) { return x; }
00099 inline float    vnl_math_abs(float x) { return x < 0.0 ? -x : x; }
00100 inline double   vnl_math_abs(double x) { return x < 0.0 ? -x : x; }
00101 inline long double vnl_math_abs(long double x) { return x < 0 ? -x : x; }
00102 
00103 // max
00104 inline int      vnl_math_max(int x, int y) { return (x > y) ? x : y; }
00105 inline unsigned vnl_math_max(unsigned x, unsigned y) { return (x > y) ? x : y; }
00106 inline long     vnl_math_max(long x, long y) { return (x > y) ? x : y; }
00107 inline float    vnl_math_max(float x, float y) { return (x < y) ? y : x; }
00108 inline double   vnl_math_max(double x, double y) { return (x < y) ? y : x; }
00109 
00110 // min
00111 inline int      vnl_math_min(int x, int y) { return (x < y) ? x : y; }
00112 inline unsigned vnl_math_min(unsigned x, unsigned y) { return (x < y) ? x : y; }
00113 inline long     vnl_math_min(long x, long y) { return (x < y) ? x : y; }
00114 inline float    vnl_math_min(float x, float y) { return (x > y) ? y : x; }
00115 inline double   vnl_math_min(double x, double y) { return (x > y) ? y : x; }
00116 
00117 // sqr (square)
00118 inline int                 vnl_math_sqr(int x) { return x*x; }
00119 inline unsigned            vnl_math_sqr(unsigned x) { return x*x; }
00120 inline long                vnl_math_sqr(long x) { return x*x; }
00121 inline float               vnl_math_sqr(float x) { return x*x; }
00122 inline double              vnl_math_sqr(double x) { return x*x; }
00123 
00124 // sgn (sign in -1, 0, +1)
00125 inline int vnl_math_sgn(int x) { return x?((x>0)?1:-1):0; }
00126 inline int vnl_math_sgn(long x) { return x?((x>0)?1:-1):0; }
00127 inline int vnl_math_sgn(float x) { return (x != 0)?((x>0)?1:-1):0; }
00128 inline int vnl_math_sgn(double x) { return (x != 0)?((x>0)?1:-1):0; }
00129 
00130 // sng0 (sign inn -1, +1 only, useful for reals)
00131 inline int vnl_math_sgn0(int x) { return (x>=0)?1:-1; }
00132 inline int vnl_math_sgn0(long x) { return (x>=0)?1:-1; }
00133 inline int vnl_math_sgn0(float x) { return (x>=0)?1:-1; }
00134 inline int vnl_math_sgn0(double x) { return (x>=0)?1:-1; }
00135 
00136 // squared_magnitude
00137 inline int      vnl_math_squared_magnitude(int x) { return x*x; }
00138 inline unsigned vnl_math_squared_magnitude(unsigned x) { return x*x; }
00139 inline long     vnl_math_squared_magnitude(long x) { return x*x; }
00140 inline unsigned long vnl_math_squared_magnitude(unsigned long x) { return x*x; }
00141 inline float    vnl_math_squared_magnitude(float x) { return x*x; }
00142 inline double   vnl_math_squared_magnitude(double x) { return x*x; }
00143 inline long double vnl_math_squared_magnitude(long double x) { return x*x; }
00144 
00145 // squareroot
00146 inline float  vnl_math_sqrt(float x) { return float( vcl_sqrt(double(x))); }
00147 inline double vnl_math_sqrt(double x) { return       vcl_sqrt(double(x)) ; }
00148 
00149 // cuberoot
00150 inline float  vnl_math_cuberoot(float a) { return float((a<0) ? -vcl_exp(vcl_log(-a)/3) : vcl_exp(vcl_log(a)/3)); }
00151 inline double vnl_math_cuberoot(double a) { return (a<0) ? -vcl_exp(vcl_log(-a)/3) : vcl_exp(vcl_log(a)/3); }
00152 
00153 // hypotenuse
00154 inline double vnl_math_hypot(int x, int y) { return vcl_sqrt(double(x*x + y*y)); }
00155 inline float  vnl_math_hypot(float x, float y) { return float( vcl_sqrt(double(x*x + y*y)) ); }
00156 inline double vnl_math_hypot(double x, double y) { return vcl_sqrt(x*x + y*y); }
00157 
00158 #endif // vnl_math_h_

Generated at Fri May 21 01:15:48 2004 for ITK by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2000