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