Math.h

Go to the documentation of this file.
00001 // @(#)root/mathcore:$Id: Math.h 21114 2007-11-29 17:16:45Z moneta $
00002 // Author: L. Moneta Tue Nov 14 15:44:38 2006
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2006  LCG ROOT Math Team, CERN/PH-SFT                *
00007  *                                                                    *
00008  *                                                                    *
00009  **********************************************************************/
00010 
00011 // mathematical constants like Pi
00012 
00013 #ifndef ROOT_Math_Math
00014 #define ROOT_Math_Math
00015 
00016 #ifdef _WIN32
00017 #define _USE_MATH_DEFINES 
00018 #define HAVE_NO_LOG1P
00019 #define HAVE_NO_EXPM1
00020 #endif
00021 
00022 #include <cmath>
00023 
00024 #if defined(__sun)
00025 //solaris definition of cmath does not include math.h which has the definitions of numerical constants
00026 #include <math.h>
00027 #endif
00028 
00029 
00030 #ifdef HAVE_NO_EXPM1
00031 // needed to implement expm1
00032 #include <limits>
00033 #endif
00034 
00035 
00036 #ifndef M_PI
00037 
00038 #define M_PI       3.14159265358979323846264338328      // Pi 
00039 #endif
00040 
00041 #ifndef M_PI_2
00042 #define M_PI_2     1.57079632679489661923132169164      // Pi/2 
00043 #endif
00044 
00045 #ifndef M_PI_4
00046 #define M_PI_4     0.78539816339744830961566084582      // Pi/4 
00047 #endif
00048 
00049 namespace ROOT { 
00050 
00051    namespace Math { 
00052 
00053 /** 
00054     Mathematical constants 
00055 */ 
00056 inline double Pi() { return M_PI; } 
00057 
00058 /** 
00059     declarations for functions which are not implemented by some compilers
00060 */
00061 
00062 /// log(1+x) with error cancelatio when x is small
00063 inline double log1p( double x) { 
00064 #ifndef HAVE_NO_LOG1P
00065    return ::log1p(x); 
00066 #else 
00067    // if log1p is not in c math library
00068   volatile double y;
00069   y = 1 + x;
00070   return std::log(y) - ((y-1)-x)/y ;  /* cancels errors with IEEE arithmetic */
00071 #endif
00072 }      
00073 /// exp(x) -1 with error cancellation when x is small
00074 inline double expm1( double x) { 
00075 #ifndef HAVE_NO_EXPM1
00076    return ::expm1(x); 
00077 #else 
00078    // compute using taylor expansion until difference is less than epsilon
00079    // use for values smaller than 0.5 (for larger (exp(x)-1 is fine
00080    if (std::abs(x) < 0.5)
00081    {
00082        // taylor series S = x + (1/2!) x^2 + (1/3!) x^3 + ... 
00083 
00084       double i = 1.0;
00085       double sum = x;
00086       double term = x / 1.0;
00087       do {
00088          i++ ;
00089          term *= x/i;
00090          sum += term;
00091       }
00092       while (std::abs(term) > std::abs(sum) * std::numeric_limits<double>::epsilon() ) ;
00093       
00094       return sum ;
00095    }
00096    else
00097    {
00098       return std::exp(x) - 1;
00099    }
00100 #endif
00101 }
00102 
00103       
00104    } // end namespace Math
00105 
00106 } // end namespace ROOT
00107 
00108 
00109 #endif /* ROOT_Math_Math */

Generated on Tue Jul 5 14:25:31 2011 for ROOT_528-00b_version by  doxygen 1.5.1