Logo ROOT   6.13/01
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 namespace Math {
63 // Enable Vc/VecCore template instantiations to replace std math functions.
64 //
65 // Vc declares `std::sqrt(Vc-type)`. To use this for Vc-`SCALAR`s, the call
66 // to `sqrt()` must only be resolved at the template instantiation time, when
67 // the Vc headers are guaranteed to be included, and thus its `sqrt()`
68 // overloads have been declared.
69 // The trick is to keep sqrt() dependent (on its argument type) by making it
70 // an unqualified name. The `std::` of `std::sqrt()` makes it a qualified
71 // name, so the code here has to use `sqrt()`, not `std::sqrt()`. To still
72 // find `std::sqrt()` we pull `std::sqrt()` into the surrounding namespace.
73 //
74 // We don't want to use 'using namespace std' because it would polute the including headers.
75 using std::atan2;
76 using std::cos;
77 using std::cosh;
78 using std::exp;
79 using std::floor;
80 using std::log;
81 using std::pow;
82 using std::sin;
83 using std::sinh;
84 using std::sqrt;
85 using std::tan;
86 
87 /**
88  Mathematical constants
89 */
90 inline double Pi()
91 {
92  return M_PI;
93  }
94 
95  /**
96  declarations for functions which are not implemented by some compilers
97  */
98 
99  /// log(1+x) with error cancelatio when x is small
100  inline double log1p(double x)
101  {
102 #ifndef HAVE_NO_LOG1P
103  return ::log1p(x);
104 #else
105  // if log1p is not in c math library
106  volatile double y;
107  y = 1 + x;
108  return std::log(y) - ((y-1)-x)/y ; /* cancels errors with IEEE arithmetic */
109 #endif
110 }
111 /// exp(x) -1 with error cancellation when x is small
112 inline double expm1( double x) {
113 #ifndef HAVE_NO_EXPM1
114  return ::expm1(x);
115 #else
116  // compute using taylor expansion until difference is less than epsilon
117  // use for values smaller than 0.5 (for larger (exp(x)-1 is fine
118  if (std::abs(x) < 0.5)
119  {
120  // taylor series S = x + (1/2!) x^2 + (1/3!) x^3 + ...
121 
122  double i = 1.0;
123  double sum = x;
124  double term = x / 1.0;
125  do {
126  i++ ;
127  term *= x/i;
128  sum += term;
129  }
130  while (std::abs(term) > std::abs(sum) * std::numeric_limits<double>::epsilon() ) ;
131 
132  return sum ;
133  }
134  else
135  {
136  return std::exp(x) - 1;
137  }
138 #endif
139 }
140 
141  } // end namespace Math
142 
143 } // end namespace ROOT
144 
145 
146 
147 
148 
149 #endif /* ROOT_Math_Math */
Namespace for new ROOT classes and functions.
Definition: TFoamSampler.h:19
you should not use this method at all Int_t y
Definition: TRolke.cxx:630
double cos(double)
double sqrt(double)
double sinh(double)
double pow(double, double)
double sin(double)
double Pi()
Mathematical constants.
Definition: Math.h:90
double cosh(double)
double expm1(double x)
exp(x) -1 with error cancellation when x is small
Definition: Math.h:112
#define M_PI
Definition: Math.h:38
double floor(double)
double log1p(double x)
declarations for functions which are not implemented by some compilers
Definition: Math.h:100
REAL epsilon
Definition: triangle.c:617
* x
Deprecated and error prone model selection interface.
Definition: TRolke.cxx:630
double atan2(double, double)
Namespace for new Math classes and functions.
double tan(double)
double exp(double)
double log(double)