Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 _MSC_VER
17#define _USE_MATH_DEFINES
18#endif
19
20#include <cmath>
21
22#if defined(__sun) || defined(_MSC_VER)
23//Microsoft and solaris definition of cmath does not include math.h which has the definitions of numerical constants
24#include <math.h> // for M_PI
25// TODO replace with std::numbers::pi once minimum version is C++20, and remove this code block
26#endif
27
28
29#ifdef HAVE_NO_EXPM1
30// needed to implement expm1
31#include <limits>
32#endif
33
34
35#ifndef M_PI
36
37#define M_PI 3.14159265358979323846264338328 // Pi
38#endif
39
40#ifndef M_PI_2
41#define M_PI_2 1.57079632679489661923132169164 // Pi/2
42#endif
43
44#ifndef M_PI_4
45#define M_PI_4 0.78539816339744830961566084582 // Pi/4
46#endif
47
48namespace ROOT {
49
50/**
51\namespace Math
52Namespace for new Math classes and functions.
53See the \ref Math "Math Libraries" page for a detailed description.
54*/
55
56namespace Math {
57
58/**
59 Mathematical constants
60*/
61inline double Pi()
62{
63 return M_PI;
64 }
65
66 /**
67 declarations for functions which are not implemented by some compilers
68 */
69
70 /// log(1+x) with error cancelation when x is small
71 inline double log1p(double x)
72 {
73#ifndef HAVE_NO_LOG1P
74 return ::log1p(x);
75#else
76 // if log1p is not in c math library
77 volatile double y;
78 y = 1 + x;
79 return std::log(y) - ((y-1)-x)/y ; /* cancels errors with IEEE arithmetic */
80#endif
81}
82/// exp(x) -1 with error cancellation when x is small
83inline double expm1( double x) {
84#ifndef HAVE_NO_EXPM1
85 return ::expm1(x);
86#else
87 // compute using taylor expansion until difference is less than epsilon
88 // use for values smaller than 0.5 (for larger (exp(x)-1 is fine
89 if (std::abs(x) < 0.5)
90 {
91 // taylor series S = x + (1/2!) x^2 + (1/3!) x^3 + ...
92
93 double i = 1.0;
94 double sum = x;
95 double term = x / 1.0;
96 do {
97 i++ ;
98 term *= x/i;
99 sum += term;
100 }
101 while (std::abs(term) > std::abs(sum) * std::numeric_limits<double>::epsilon() ) ;
102
103 return sum ;
104 }
105 else
106 {
107 return std::exp(x) - 1;
108 }
109#endif
110}
111
112} // end namespace Math
113
114} // end namespace ROOT
115
116
117
118
119
120#endif /* ROOT_Math_Math */
#define M_PI
Definition Rotated.cxx:105
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
Namespace for new Math classes and functions.
double log1p(double x)
declarations for functions which are not implemented by some compilers
Definition Math.h:71
double Pi()
Mathematical constants.
Definition Math.h:61
double expm1(double x)
exp(x) -1 with error cancellation when x is small
Definition Math.h:83
static uint64_t sum(uint64_t i)
Definition Factory.cxx:2339