OpenMM
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
MSVC_erfc.h
1 #ifndef OPENMM_MSVC_ERFC_H_
2 #define OPENMM_MSVC_ERFC_H_
3 
4 /*
5  * Up to version 11 (VC++ 2012), Microsoft does not support the
6  * standard C99 erf() and erfc() functions so we have to fake them here.
7  * These were added in version 12 (VC++ 2013), which sets _MSC_VER=1800
8  * (VC11 has _MSC_VER=1700).
9  */
10 
11 #if defined(_MSC_VER)
12 #ifndef M_PI
13 #define M_PI 3.14159265358979323846264338327950288
14 #endif
15 
16 #if _MSC_VER <= 1700 // 1700 is VC11, 1800 is VC12
17 /***************************
18 * erf.cpp
19 * author: Steve Strand
20 * written: 29-Jan-04
21 ***************************/
22 
23 #include <cmath>
24 
25 static const double rel_error= 1E-12; //calculate 12 significant figures
26 //you can adjust rel_error to trade off between accuracy and speed
27 //but don't ask for > 15 figures (assuming usual 52 bit mantissa in a double)
28 
29 static double erfc(double x);
30 
31 static double erf(double x)
32 //erf(x) = 2/sqrt(pi)*integral(exp(-t^2),t,0,x)
33 // = 2/sqrt(pi)*[x - x^3/3 + x^5/5*2! - x^7/7*3! + ...]
34 // = 1-erfc(x)
35 {
36  static const double two_sqrtpi= 1.128379167095512574; // 2/sqrt(pi)
37  if (fabs(x) > 2.2) {
38  return 1.0 - erfc(x); //use continued fraction when fabs(x) > 2.2
39  }
40  double sum= x, term= x, xsqr= x*x;
41  int j= 1;
42  do {
43  term*= xsqr/j;
44  sum-= term/(2*j+1);
45  ++j;
46  term*= xsqr/j;
47  sum+= term/(2*j+1);
48  ++j;
49  } while (fabs(term)/sum > rel_error);
50  return two_sqrtpi*sum;
51 }
52 
53 
54 static double erfc(double x)
55 //erfc(x) = 2/sqrt(pi)*integral(exp(-t^2),t,x,inf)
56 // = exp(-x^2)/sqrt(pi) * [1/x+ (1/2)/x+ (2/2)/x+ (3/2)/x+ (4/2)/x+ ...]
57 // = 1-erf(x)
58 //expression inside [] is a continued fraction so '+' means add to denominator only
59 {
60  static const double one_sqrtpi= 0.564189583547756287; // 1/sqrt(pi)
61  if (fabs(x) < 2.2) {
62  return 1.0 - erf(x); //use series when fabs(x) < 2.2
63  }
64  // Don't look for x==0 here!
65  if (x < 0) { //continued fraction only valid for x>0
66  return 2.0 - erfc(-x);
67  }
68  double a=1, b=x; //last two convergent numerators
69  double c=x, d=x*x+0.5; //last two convergent denominators
70  double q1, q2= b/d; //last two convergents (a/c and b/d)
71  double n= 1.0, t;
72  do {
73  t= a*n+b*x;
74  a= b;
75  b= t;
76  t= c*n+d*x;
77  c= d;
78  d= t;
79  n+= 0.5;
80  q1= q2;
81  q2= b/d;
82  } while (fabs(q1-q2)/q2 > rel_error);
83  return one_sqrtpi*exp(-x*x)*q2;
84 }
85 
86 #endif // _MSC_VER <= 1700
87 #endif // _MSC_VER
88 
89 #endif // OPENMM_MSVC_ERFC_H_