LifeV
winmath.h
Go to the documentation of this file.
1 #ifndef WINMATH_H
2 #define WINMATH_H
3 
4 /**********************************************************************
5 
6  acosh.c -
7 
8  $Author$
9  $Date$
10  created at: Fri Apr 12 00:34:17 JST 2002
11 
12  public domain rewrite of acosh(3), asinh(3) and atanh(3)
13 
14 **********************************************************************/
15 
16 #include <errno.h>
17 #include <float.h>
18 #include <math.h>
19 #ifndef M_PI
20 #define M_PI 3.14159265358979323846
21 #endif
22 
23 /* DBL_MANT_DIG must be less than 4 times of bits of int */
24 #ifndef DBL_MANT_DIG
25 #define DBL_MANT_DIG 53 /* in this case, at least 12 digit precision */
26 #endif
27 #define BIG_CRITERIA_BIT (1<<DBL_MANT_DIG/2)
28 #if BIG_CRITERIA_BIT > 0
29 #define BIG_CRITERIA (1.0*BIG_CRITERIA_BIT)
30 #else
31 #define BIG_CRITERIA (1.0*(1<<DBL_MANT_DIG/4)*(1<<(DBL_MANT_DIG/2+1-DBL_MANT_DIG/4)))
32 #endif
33 #define SMALL_CRITERIA_BIT (1<<(DBL_MANT_DIG/3))
34 #if SMALL_CRITERIA_BIT > 0
35 #define SMALL_CRITERIA (1.0/SMALL_CRITERIA_BIT)
36 #else
37 #define SMALL_CRITERIA (1.0*(1<<DBL_MANT_DIG/4)*(1<<(DBL_MANT_DIG/3+1-DBL_MANT_DIG/4)))
38 #endif
39 
40 inline double acosh(double x)
41 {
42  if (x < 1)
43  x = -1; /* NaN */
44  else if (x == 1)
45  return 0;
46  else if (x > BIG_CRITERIA)
47  x += x;
48  else
49  x += sqrt((x + 1) * (x - 1));
50  return log(x);
51 }
52 
53 inline double asinh(double x)
54 {
55  int neg = x < 0;
56  double z = fabs(x);
57 
58  if (z < SMALL_CRITERIA) return x;
59  if (z < (1.0/(1<<DBL_MANT_DIG/5))) {
60  double x2 = z * z;
61  z *= 1 + x2 * (-1.0/6.0 + x2 * 3.0/40.0);
62  }
63  else if (z > BIG_CRITERIA) {
64  z = log(z + z);
65  }
66  else {
67  z = log(z + sqrt(z * z + 1));
68  }
69  if (neg) z = -z;
70  return z;
71 }
72 
73 inline double atanh(double x)
74 {
75  int neg = x < 0;
76  double z = fabs(x);
77 
78  if (z < SMALL_CRITERIA) return x;
79  z = log(z > 1 ? -1 : (1 + z) / (1 - z)) / 2;
80  if (neg) z = -z;
81  return z;
82 }
83 
84 inline double round(double val)
85 {
86  return floor(val + 0.5);
87 }
88 
89 inline void srand48(double seed)
90 {
91  srand(seed);
92 }
93 
94 inline double drand48()
95 {
96  return (double(rand()) / RAND_MAX);
97 }
98 
99 float tgammaf(float z){
100  static unsigned int c = 9;
101  static float lanczos_coefficients[] = {1.000000000000000174663f,
102  5716.400188274341379136f,
103  -14815.30426768413909044f,
104  14291.49277657478554025f,
105  -6348.160217641458813289f,
106  1301.608286058321874105f,
107  -108.1767053514369634679f,
108  2.605696505611755827729f,
109  -0.7423452510201416151527e-2f,
110  0.5384136432509564062961e-7f,
111  -0.4023533141268236372067e-8f
112  };
113  float return_val = 0.0f;
114 
115  z -= 1.0f;
116  float temp = z + c + 0.5f;
117  float A = lanczos_coefficients[0];
118  int i = 0;
119  for(i = 1; i < c+2; ++i){
120  A += lanczos_coefficients[i]/(z+i);
121  }
122  return_val = sqrt(2 * M_PI)* pow(temp, z + 0.5f) * exp(-temp) * A;
123 
124  return return_val;
125 }
126 
127 double tgamma(double z){
128  static unsigned int c = 9;
129  static double lanczos_coefficients[] = {1.000000000000000174663,
130  5716.400188274341379136,
131  -14815.30426768413909044,
132  14291.49277657478554025,
133  -6348.160217641458813289,
134  1301.608286058321874105,
135  -108.1767053514369634679,
136  2.605696505611755827729,
137  -0.7423452510201416151527e-2,
138  0.5384136432509564062961e-7,
139  -0.4023533141268236372067e-8
140  };
141  double return_val = 0.0;
142 
143  z -= 1.0;
144  double temp = z + c + 0.5;
145  double A = lanczos_coefficients[0];
146  int i = 0;
147  for(i = 1; i < c+2; ++i){
148  A += lanczos_coefficients[i]/(z+i);
149  }
150  return_val = sqrt(2 * M_PI)* pow(temp, z + 0.5) * exp(-temp) * A;
151 
152  return return_val;
153 }
154 
155 long double tgammal(long double z){
156  static unsigned int c = 9;
157  static long double lanczos_coefficients[] = {1.000000000000000174663,
158  5716.400188274341379136,
159  -14815.30426768413909044,
160  14291.49277657478554025,
161  -6348.160217641458813289,
162  1301.608286058321874105,
163  -108.1767053514369634679,
164  2.605696505611755827729,
165  -0.7423452510201416151527e-2,
166  0.5384136432509564062961e-7,
167  -0.4023533141268236372067e-8
168  };
169  long double return_val = 0.0;
170 
171  z -= 1.0;
172  long double temp = z + c + 0.5;
173  long double A = lanczos_coefficients[0];
174  int i = 0;
175  for(i = 1; i < c+2; ++i){
176  A += lanczos_coefficients[i]/(z+i);
177  }
178  return_val = sqrt(2 * M_PI)* pow(temp, z + 0.5) * exp(-temp) * A;
179 
180  return return_val;
181 }
182 
183 // This function was adapted from a public domain implementation of erf
184 // which is available at http://www.johndcook.com/cpp_erf.html. The only
185 // changes made were to change the type from double to float.
186 float erff(float x)
187 {
188  // constants
189  float a1 = 0.254829592f;
190  float a2 = -0.284496736f;
191  float a3 = 1.421413741f;
192  float a4 = -1.453152027f;
193  float a5 = 1.061405429f;
194  float p = 0.3275911f;
195 
196  // Save the sign of x
197  int sign = 1;
198  if (x < 0)
199  sign = -1;
200  x = fabs(x);
201 
202  // A&S formula 7.1.26
203  float t = 1.0f/(1.0f + p*x);
204  float y = 1.0f - (((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*exp(-x*x);
205 
206  return sign*y;
207 }
208 
209 #endif
#define SMALL_CRITERIA
Definition: winmath.h:35
double tgamma(double z)
Definition: winmath.h:127
float tgammaf(float z)
Definition: winmath.h:99
long double tgammal(long double z)
Definition: winmath.h:155
double atanh(double x)
Definition: winmath.h:73
#define M_PI
Definition: winmath.h:20
#define DBL_MANT_DIG
Definition: winmath.h:25
double drand48()
Definition: winmath.h:94
double acosh(double x)
Definition: winmath.h:40
#define BIG_CRITERIA
Definition: winmath.h:29
#define SMALL_CRITERIA_BIT
Definition: winmath.h:33
double asinh(double x)
Definition: winmath.h:53
void srand48(double seed)
Definition: winmath.h:89
#define BIG_CRITERIA_BIT
Definition: winmath.h:27
double round(double val)
Definition: winmath.h:84
float erff(float x)
Definition: winmath.h:186