Logo ROOT   6.08/07
Reference Guide
Math.h
Go to the documentation of this file.
1 // @(#)root/mathcore:$Id$
2 // Author: L. Moneta Tue Nov 14 15:44:38 2006
3 
4 /**********************************************************************
5  * *
6  * Copyright (c) 2006 LCG ROOT Math Team, CERN/PH-SFT *
7  * *
8  * *
9  **********************************************************************/
10 
11 // mathematical constants like Pi
12 
13 #ifndef ROOT_Math_Math
14 #define ROOT_Math_Math
15 
16 #ifdef _WIN32
17 #define _USE_MATH_DEFINES
18 #define HAVE_NO_LOG1P
19 #define HAVE_NO_EXPM1
20 #endif
21 
22 #include <cmath>
23 
24 #if defined(__sun)
25 //solaris definition of cmath does not include math.h which has the definitions of numerical constants
26 #include <math.h>
27 #endif
28 
29 
30 #ifdef HAVE_NO_EXPM1
31 // needed to implement expm1
32 #include <limits>
33 #endif
34 
35 
36 #ifndef M_PI
37 
38 #define M_PI 3.14159265358979323846264338328 // Pi
39 #endif
40 
41 #ifndef M_PI_2
42 #define M_PI_2 1.57079632679489661923132169164 // Pi/2
43 #endif
44 
45 #ifndef M_PI_4
46 #define M_PI_4 0.78539816339744830961566084582 // Pi/4
47 #endif
48 
49 /**
50  \namespace ROOT
51  Namespace for new ROOT classes and functions
52  */
53 
54 namespace ROOT {
55 
56  /**
57  \namespace Math
58  Namespace for new Math classes and functions.
59  See the \ref Math "Math Libraries" page for a detailed description.
60  */
61 
62 
63  namespace Math {
64 
65 /**
66  Mathematical constants
67 */
68 inline double Pi() { return M_PI; }
69 
70 /**
71  declarations for functions which are not implemented by some compilers
72 */
73 
74 /// log(1+x) with error cancelatio when x is small
75 inline double log1p( double x) {
76 #ifndef HAVE_NO_LOG1P
77  return ::log1p(x);
78 #else
79  // if log1p is not in c math library
80  volatile double y;
81  y = 1 + x;
82  return std::log(y) - ((y-1)-x)/y ; /* cancels errors with IEEE arithmetic */
83 #endif
84 }
85 /// exp(x) -1 with error cancellation when x is small
86 inline double expm1( double x) {
87 #ifndef HAVE_NO_EXPM1
88  return ::expm1(x);
89 #else
90  // compute using taylor expansion until difference is less than epsilon
91  // use for values smaller than 0.5 (for larger (exp(x)-1 is fine
92  if (std::abs(x) < 0.5)
93  {
94  // taylor series S = x + (1/2!) x^2 + (1/3!) x^3 + ...
95 
96  double i = 1.0;
97  double sum = x;
98  double term = x / 1.0;
99  do {
100  i++ ;
101  term *= x/i;
102  sum += term;
103  }
104  while (std::abs(term) > std::abs(sum) * std::numeric_limits<double>::epsilon() ) ;
105 
106  return sum ;
107  }
108  else
109  {
110  return std::exp(x) - 1;
111  }
112 #endif
113 }
114 
115 
116  } // end namespace Math
117 
118 } // end namespace ROOT
119 
120 
121 
122 
123 
124 #endif /* ROOT_Math_Math */
static long int sum(long int i)
Definition: Factory.cxx:1786
This namespace contains pre-defined functions to be used in conjuction with TExecutor::Map and TExecu...
Definition: StringConv.hxx:21
Double_t x[n]
Definition: legend1.C:17
double Pi()
Mathematical constants.
Definition: Math.h:68
double expm1(double x)
exp(x) -1 with error cancellation when x is small
Definition: Math.h:86
#define M_PI
Definition: Math.h:38
double log1p(double x)
declarations for functions which are not implemented by some compilers
Definition: Math.h:75
REAL epsilon
Definition: triangle.c:617
Double_t y[n]
Definition: legend1.C:17
Namespace for new Math classes and functions.
double exp(double)
double log(double)