STIR 6.4.0
erf.inl
1//
2//
3/*-
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32/* Modified Nov 30, 1992 P. McILROY:
33 * Replaced expansions for x >= 1.25 (error 1.7ulp vs ~6ulp)
34 * Replaced even+odd with direct calculation for x < .84375,
35 * to avoid destructive cancellation.
36 *
37 * Performance of erfc(x):
38 * In 300000 trials in the range [.83, .84375] the
39 * maximum observed error was 3.6ulp.
40 *
41 * In [.84735,1.25] the maximum observed error was <2.5ulp in
42 * 100000 runs in the range [1.2, 1.25].
43 *
44 * In [1.25,26] (Not including subnormal results)
45 * the error is < 1.7ulp.
46 */
47
48/* double erf(double x)
49 * double erfc(double x)
50 * x
51 * 2 |\
52 * erf(x) = --------- | exp(-t*t)dt
53 * sqrt(pi) \|
54 * 0
55 *
56 * erfc(x) = 1-erf(x)
57 *
58 * Method:
59 * 1. Reduce x to |x| by erf(-x) = -erf(x)
60 * 2. For x in [0, 0.84375]
61 * erf(x) = x + x*P(x^2)
62 * erfc(x) = 1 - erf(x) if x<=0.25
63 * = 0.5 + ((0.5-x)-x*P) if x in [0.25,0.84375]
64 * where
65 * 2 2 4 20
66 * P = P(x ) = (p0 + p1 * x + p2 * x + ... + p10 * x )
67 * is an approximation to (erf(x)-x)/x with precision
68 *
69 * -56.45
70 * | P - (erf(x)-x)/x | <= 2
71 *
72 *
73 * Remark. The formula is derived by noting
74 * erf(x) = (2/sqrt(pi))*(x - x^3/3 + x^5/10 - x^7/42 + ....)
75 * and that
76 * 2/sqrt(pi) = 1.128379167095512573896158903121545171688
77 * is close to one. The interval is chosen because the fixed
78 * point of erf(x) is near 0.6174 (i.e., erf(x)=x when x is
79 * near 0.6174), and by some experiment, 0.84375 is chosen to
80 * guarantee the error is less than one ulp for erf.
81 *
82 * 3. For x in [0.84375,1.25], let s = x - 1, and
83 * c = 0.84506291151 rounded to single (24 bits)
84 * erf(x) = c + P1(s)/Q1(s)
85 * erfc(x) = (1-c) - P1(s)/Q1(s)
86 * |P1/Q1 - (erf(x)-c)| <= 2**-59.06
87 * Remark: here we use the taylor series expansion at x=1.
88 * erf(1+s) = erf(1) + s*Poly(s)
89 * = 0.845.. + P1(s)/Q1(s)
90 * That is, we use rational approximation to approximate
91 * erf(1+s) - (c = (single)0.84506291151)
92 * Note that |P1/Q1|< 0.078 for x in [0.84375,1.25]
93 * where
94 * P1(s) = degree 6 poly in s
95 * Q1(s) = degree 6 poly in s
96 *
97 * 4. For x in [1.25, 2]; [2, 4]
98 * erf(x) = 1.0 - tiny
99 * erfc(x) = (1/x)exp(-x*x-(.5*log(pi) -.5z + R(z)/S(z))
100 *
101 * Where z = 1/(x*x), R is degree 9, and S is degree 3;
102 *
103 * 5. For x in [4,28]
104 * erf(x) = 1.0 - tiny
105 * erfc(x) = (1/x)exp(-x*x-(.5*log(pi)+eps + zP(z))
106 *
107 * Where P is degree 14 polynomial in 1/(x*x).
108 *
109 * Notes:
110 * Here 4 and 5 make use of the asymptotic series
111 * exp(-x*x)
112 * erfc(x) ~ ---------- * ( 1 + Poly(1/x^2) );
113 * x*sqrt(pi)
114 *
115 * where for z = 1/(x*x)
116 * P(z) ~ z/2*(-1 + z*3/2*(1 + z*5/2*(-1 + z*7/2*(1 +...))))
117 *
118 * Thus we use rational approximation to approximate
119 * erfc*x*exp(x*x) ~ 1/sqrt(pi);
120 *
121 * The error bound for the target function, G(z) for
122 * the interval
123 * [4, 28]:
124 * |eps + 1/(z)P(z) - G(z)| < 2**(-56.61)
125 * for [2, 4]:
126 * |R(z)/S(z) - G(z)| < 2**(-58.24)
127 * for [1.25, 2]:
128 * |R(z)/S(z) - G(z)| < 2**(-58.12)
129 *
130 * 6. For inf > x >= 28
131 * erf(x) = 1 - tiny (raise inexact)
132 * erfc(x) = tiny*tiny (raise underflow)
133 *
134 * 7. Special cases:
135 * erf(0) = 0, erf(inf) = 1, erf(-inf) = -1,
136 * erfc(0) = 1, erfc(inf) = 0, erfc(-inf) = 2,
137 * erfc/erf(NaN) is NaN
138 */
139
140/* Modified for STIR library Jun 05, 2005 Ch Tsoumpas and K Thielemans.
141
142 WARNING:
143 There are some assumptions here about IEEE arithmetic etc.
144 On non-IEEE machines you're bound to have problems.
145
146 We put in some work-arounds by using (hopefully) portable STIR_isnan,
147 and STIR_finite().
148*/
149
150#ifdef _IEEE_LIBM
151/*
152 * redefining "___function" to "function" in _IEEE_LIBM mode
153 */
154# include "ieee_libm.h"
155#endif
156
157#if defined(__vax__) || defined(tahoe)
158// non-IEEE machines
159# define _IEEE 0
160# define TRUNC(x) (double)(x) = (float)(x)
161#else
162// assume everything uses IEEE floating point arithmetic
163# define _IEEE 1
164// warning: strange definition of TRUNC that will go very weird on non-IEEE machines
165# define TRUNC(x) *(((int*)&x) + 1) &= 0xf8000000
166#endif
167
168#include <cmath>
169#include "stir/numerics/ieeefp.h"
170
171START_NAMESPACE_STIR
172
173static const double tiny = 1e-300, half = 0.5, one = 1.0, two = 2.0, c = 8.45062911510467529297e-01, /* (float)0.84506291151 */
174 /*
175 * Coefficients for approximation to erf in [0,0.84375]
176 */
177 p0t8 = 1.02703333676410051049867154944018394163280, p0 = 1.283791670955125638123339436800229927041e-0001,
178 p1 = -3.761263890318340796574473028946097022260e-0001, p2 = 1.128379167093567004871858633779992337238e-0001,
179 p3 = -2.686617064084433642889526516177508374437e-0002, p4 = 5.223977576966219409445780927846432273191e-0003,
180 p5 = -8.548323822001639515038738961618255438422e-0004, p6 = 1.205520092530505090384383082516403772317e-0004,
181 p7 = -1.492214100762529635365672665955239554276e-0005, p8 = 1.640186161764254363152286358441771740838e-0006,
182 p9 = -1.571599331700515057841960987689515895479e-0007, p10 = 1.073087585213621540635426191486561494058e-0008;
183/*
184 * Coefficients for approximation to erf in [0.84375,1.25]
185 */
186static const double pa0 = -2.362118560752659485957248365514511540287e-0003, pa1 = 4.148561186837483359654781492060070469522e-0001,
187 pa2 = -3.722078760357013107593507594535478633044e-0001, pa3 = 3.183466199011617316853636418691420262160e-0001,
188 pa4 = -1.108946942823966771253985510891237782544e-0001, pa5 = 3.547830432561823343969797140537411825179e-0002,
189 pa6 = -2.166375594868790886906539848893221184820e-0003, qa1 = 1.064208804008442270765369280952419863524e-0001,
190 qa2 = 5.403979177021710663441167681878575087235e-0001, qa3 = 7.182865441419627066207655332170665812023e-0002,
191 qa4 = 1.261712198087616469108438860983447773726e-0001, qa5 = 1.363708391202905087876983523620537833157e-0002,
192 qa6 = 1.198449984679910764099772682882189711364e-0002;
193/*
194 * log(sqrt(pi)) for large x expansions.
195 * The tail (lsqrtPI_lo) is included in the rational
196 * approximations.
197 */
198static const double lsqrtPI_hi = .5723649429247000819387380943226;
199/*
200 * lsqrtPI_lo = .000000000000000005132975581353913;
201 *
202 * Coefficients for approximation to erfc in [2, 4]
203 */
204static const double rb0 = -1.5306508387410807582e-010, /* includes lsqrtPI_lo */
205 rb1 = 2.15592846101742183841910806188e-008, rb2 = 6.24998557732436510470108714799e-001,
206 rb3 = 8.24849222231141787631258921465e+000, rb4 = 2.63974967372233173534823436057e+001,
207 rb5 = 9.86383092541570505318304640241e+000, rb6 = -7.28024154841991322228977878694e+000,
208 rb7 = 5.96303287280680116566600190708e+000, rb8 = -4.40070358507372993983608466806e+000,
209 rb9 = 2.39923700182518073731330332521e+000, rb10 = -6.89257464785841156285073338950e-001,
210 sb1 = 1.56641558965626774835300238919e+001, sb2 = 7.20522741000949622502957936376e+001,
211 sb3 = 9.60121069770492994166488642804e+001;
212/*
213 * Coefficients for approximation to erfc in [1.25, 2]
214 */
215static const double rc0 = -2.47925334685189288817e-007, /* includes lsqrtPI_lo */
216 rc1 = 1.28735722546372485255126993930e-005, rc2 = 6.24664954087883916855616917019e-001,
217 rc3 = 4.69798884785807402408863708843e+000, rc4 = 7.61618295853929705430118701770e+000,
218 rc5 = 9.15640208659364240872946538730e-001, rc6 = -3.59753040425048631334448145935e-001,
219 rc7 = 1.42862267989304403403849619281e-001, rc8 = -4.74392758811439801958087514322e-002,
220 rc9 = 1.09964787987580810135757047874e-002, rc10 = -1.28856240494889325194638463046e-003,
221 sc1 = 9.97395106984001955652274773456e+000, sc2 = 2.80952153365721279953959310660e+001,
222 sc3 = 2.19826478142545234106819407316e+001;
223/*
224 * Coefficients for approximation to erfc in [4,28]
225 */
226static const double rd0 = -2.1491361969012978677e-016, /* includes lsqrtPI_lo */
227 rd1 = -4.99999999999640086151350330820e-001, rd2 = 6.24999999772906433825880867516e-001,
228 rd3 = -1.54166659428052432723177389562e+000, rd4 = 5.51561147405411844601985649206e+000,
229 rd5 = -2.55046307982949826964613748714e+001, rd6 = 1.43631424382843846387913799845e+002,
230 rd7 = -9.45789244999420134263345971704e+002, rd8 = 6.94834146607051206956384703517e+003,
231 rd9 = -5.27176414235983393155038356781e+004, rd10 = 3.68530281128672766499221324921e+005,
232 rd11 = -2.06466642800404317677021026611e+006, rd12 = 7.78293889471135381609201431274e+006,
233 rd13 = -1.42821001129434127360582351685e+007;
234
235inline double
236erf(double x)
237{
238 double R, S, P, Q, ax, s, y, z, r;
239 if (!STIR_finite(x))
240 { /* erf(nan)=nan */
241 if (STIR_isnan(x))
242 return (x);
243 return (x > 0 ? one : -one); /* erf(+/-inf)= +/-1 */
244 }
245 if ((ax = x) < 0)
246 ax = -ax;
247 if (ax < .84375)
248 {
249 if (ax < 3.7e-09)
250 {
251 if (ax < 1.0e-308)
252 return 0.125 * (8.0 * x + p0t8 * x); /*avoid underflow */
253 return x + p0 * x;
254 }
255 y = x * x;
256 r = y * (p1 + y * (p2 + y * (p3 + y * (p4 + y * (p5 + y * (p6 + y * (p7 + y * (p8 + y * (p9 + y * p10)))))))));
257 return x + x * (p0 + r);
258 }
259 if (ax < 1.25)
260 { /* 0.84375 <= |x| < 1.25 */
261 s = std::fabs(x) - one;
262 P = pa0 + s * (pa1 + s * (pa2 + s * (pa3 + s * (pa4 + s * (pa5 + s * pa6)))));
263 Q = one + s * (qa1 + s * (qa2 + s * (qa3 + s * (qa4 + s * (qa5 + s * qa6)))));
264 if (x >= 0)
265 return (c + P / Q);
266 else
267 return (-c - P / Q);
268 }
269 if (ax >= 6.0)
270 { /* inf>|x|>=6 */
271 if (x >= 0.0)
272 return (one - tiny);
273 else
274 return (tiny - one);
275 }
276 /* 1.25 <= |x| < 6 */
277 z = -ax * ax;
278 s = -one / z;
279 if (ax < 2.0)
280 {
281 R = rc0
282 + s * (rc1 + s * (rc2 + s * (rc3 + s * (rc4 + s * (rc5 + s * (rc6 + s * (rc7 + s * (rc8 + s * (rc9 + s * rc10)))))))));
283 S = one + s * (sc1 + s * (sc2 + s * sc3));
284 }
285 else
286 {
287 R = rb0
288 + s * (rb1 + s * (rb2 + s * (rb3 + s * (rb4 + s * (rb5 + s * (rb6 + s * (rb7 + s * (rb8 + s * (rb9 + s * rb10)))))))));
289 S = one + s * (sb1 + s * (sb2 + s * sb3));
290 }
291 y = (R / S - .5 * s) - lsqrtPI_hi;
292 z += y;
293 z = std::exp(z) / ax;
294 if (x >= 0)
295 return (one - z);
296 else
297 return (z - one);
298}
299
300inline double
301erfc(double x)
302{
303 double R, S, P, Q, s, ax, y, z, r;
304 if (!STIR_finite(x))
305 {
306 if (STIR_isnan(x)) /* erfc(NaN) = NaN */
307 return (x);
308 else if (x > 0) /* erfc(+-inf)=0,2 */
309 return 0.0;
310 else
311 return 2.0;
312 }
313 if ((ax = x) < 0)
314 ax = -ax;
315 if (ax < .84375)
316 { /* |x|<0.84375 */
317 if (ax < 1.38777878078144568e-17) /* |x|<2**-56 */
318 return one - x;
319 y = x * x;
320 r = y * (p1 + y * (p2 + y * (p3 + y * (p4 + y * (p5 + y * (p6 + y * (p7 + y * (p8 + y * (p9 + y * p10)))))))));
321 if (ax < .0625)
322 { /* |x|<2**-4 */
323 return (one - (x + x * (p0 + r)));
324 }
325 else
326 {
327 r = x * (p0 + r);
328 r += (x - half);
329 return (half - r);
330 }
331 }
332 if (ax < 1.25)
333 { /* 0.84375 <= |x| < 1.25 */
334 s = ax - one;
335 P = pa0 + s * (pa1 + s * (pa2 + s * (pa3 + s * (pa4 + s * (pa5 + s * pa6)))));
336 Q = one + s * (qa1 + s * (qa2 + s * (qa3 + s * (qa4 + s * (qa5 + s * qa6)))));
337 if (x >= 0)
338 {
339 z = one - c;
340 return z - P / Q;
341 }
342 else
343 {
344 z = c + P / Q;
345 return one + z;
346 }
347 }
348 if (ax >= 28)
349 { /* Out of range */
350 if (x > 0)
351 return (tiny * tiny);
352 else
353 return (two - tiny);
354 }
355 z = ax;
356 TRUNC(z);
357 y = z - ax;
358 y *= (ax + z);
359 z *= -z; /* Here z + y = -x^2 */
360 s = one / (-z - y); /* 1/(x*x) */
361 if (ax >= 4)
362 { /* 6 <= ax */
363 R = s
364 * (rd1
365 + s
366 * (rd2
367 + s
368 * (rd3
369 + s
370 * (rd4
371 + s
372 * (rd5
373 + s
374 * (rd6
375 + s
376 * (rd7
377 + s
378 * (rd8
379 + s
380 * (rd9
381 + s
382 * (rd10
383 + s
384 * (rd11
385 + s
386 * (rd12
387 + s * rd13))))))))))));
388 y += rd0;
389 }
390 else if (ax >= 2)
391 {
392 R = rb0
393 + s * (rb1 + s * (rb2 + s * (rb3 + s * (rb4 + s * (rb5 + s * (rb6 + s * (rb7 + s * (rb8 + s * (rb9 + s * rb10)))))))));
394 S = one + s * (sb1 + s * (sb2 + s * sb3));
395 y += R / S;
396 R = -.5 * s;
397 }
398 else
399 {
400 R = rc0
401 + s * (rc1 + s * (rc2 + s * (rc3 + s * (rc4 + s * (rc5 + s * (rc6 + s * (rc7 + s * (rc8 + s * (rc9 + s * rc10)))))))));
402 S = one + s * (sc1 + s * (sc2 + s * sc3));
403 y += R / S;
404 R = -.5 * s;
405 }
406 /* return std::exp(-x^2 - lsqrtPI_hi + R + y)/x; */
407 // s = ((R + y) - lsqrtPI_hi) + z;
408 // y = (((z-s) - lsqrtPI_hi) + R) + y;
409 // r = std::exp(-x + s/x);
410 // r = __exp__D(s, y)/x;
411 if (x > 0)
412 return (std::exp(-x * x - lsqrtPI_hi + R + y)) / x;
413 // return r;
414 else
415 return 2. - (std::exp(-x * x - lsqrtPI_hi + R + y)) / x;
416 // return two-r;
417}
418
419#undef TRUNC
420#undef _IEEE
421END_NAMESPACE_STIR
Definition of work-around macros STIR_isnan and STIR_finite for a few non-portable IEEE floating poin...
#define STIR_finite(x)
Definition ieeefp.h:69
#define STIR_isnan(x)
Definition ieeefp.h:60