statrs/function/
erf.rs

1//! Provides the [error](https://en.wikipedia.org/wiki/Error_function) and
2//! related functions
3
4use crate::function::evaluate;
5use crate::is_zero;
6use std::f64;
7
8/// `erf` calculates the error function at `x`.
9pub fn erf(x: f64) -> f64 {
10    if x.is_nan() {
11        f64::NAN
12    } else if x >= 0.0 && x.is_infinite() {
13        1.0
14    } else if x <= 0.0 && x.is_infinite() {
15        -1.0
16    } else if is_zero(x) {
17        0.0
18    } else {
19        erf_impl(x, false)
20    }
21}
22
23/// `erf_inv` calculates the inverse error function
24/// at `x`.
25pub fn erf_inv(x: f64) -> f64 {
26    if x == 0.0 {
27        0.0
28    } else if x >= 1.0 {
29        f64::INFINITY
30    } else if x <= -1.0 {
31        f64::NEG_INFINITY
32    } else if x < 0.0 {
33        erf_inv_impl(-x, 1.0 + x, -1.0)
34    } else {
35        erf_inv_impl(x, 1.0 - x, 1.0)
36    }
37}
38
39/// `erfc` calculates the complementary error function
40/// at `x`.
41pub fn erfc(x: f64) -> f64 {
42    if x.is_nan() {
43        f64::NAN
44    } else if x == f64::INFINITY {
45        0.0
46    } else if x == f64::NEG_INFINITY {
47        2.0
48    } else {
49        erf_impl(x, true)
50    }
51}
52
53/// `erfc_inv` calculates the complementary inverse
54/// error function at `x`.
55pub fn erfc_inv(x: f64) -> f64 {
56    if x <= 0.0 {
57        f64::INFINITY
58    } else if x >= 2.0 {
59        f64::NEG_INFINITY
60    } else if x > 1.0 {
61        erf_inv_impl(-1.0 + x, 2.0 - x, -1.0)
62    } else {
63        erf_inv_impl(1.0 - x, x, 1.0)
64    }
65}
66
67// **********************************************************
68// ********** Coefficients for erf_impl polynomial **********
69// **********************************************************
70
71/// Polynomial coefficients for a numerator of `erf_impl`
72/// in the interval [1e-10, 0.5].
73const ERF_IMPL_AN: &[f64] = &[
74    0.00337916709551257388990745,
75    -0.00073695653048167948530905,
76    -0.374732337392919607868241,
77    0.0817442448733587196071743,
78    -0.0421089319936548595203468,
79    0.0070165709512095756344528,
80    -0.00495091255982435110337458,
81    0.000871646599037922480317225,
82];
83
84/// Polynomial coefficients for a denominator of `erf_impl`
85/// in the interval [1e-10, 0.5]
86const ERF_IMPL_AD: &[f64] = &[
87    1.0,
88    -0.218088218087924645390535,
89    0.412542972725442099083918,
90    -0.0841891147873106755410271,
91    0.0655338856400241519690695,
92    -0.0120019604454941768171266,
93    0.00408165558926174048329689,
94    -0.000615900721557769691924509,
95];
96
97/// Polynomial coefficients for a numerator in `erf_impl`
98/// in the interval [0.5, 0.75].
99const ERF_IMPL_BN: &[f64] = &[
100    -0.0361790390718262471360258,
101    0.292251883444882683221149,
102    0.281447041797604512774415,
103    0.125610208862766947294894,
104    0.0274135028268930549240776,
105    0.00250839672168065762786937,
106];
107
108/// Polynomial coefficients for a denominator in `erf_impl`
109/// in the interval [0.5, 0.75].
110const ERF_IMPL_BD: &[f64] = &[
111    1.0,
112    1.8545005897903486499845,
113    1.43575803037831418074962,
114    0.582827658753036572454135,
115    0.124810476932949746447682,
116    0.0113724176546353285778481,
117];
118
119/// Polynomial coefficients for a numerator in `erf_impl`
120/// in the interval [0.75, 1.25].
121const ERF_IMPL_CN: &[f64] = &[
122    -0.0397876892611136856954425,
123    0.153165212467878293257683,
124    0.191260295600936245503129,
125    0.10276327061989304213645,
126    0.029637090615738836726027,
127    0.0046093486780275489468812,
128    0.000307607820348680180548455,
129];
130
131/// Polynomial coefficients for a denominator in `erf_impl`
132/// in the interval [0.75, 1.25].
133const ERF_IMPL_CD: &[f64] = &[
134    1.0,
135    1.95520072987627704987886,
136    1.64762317199384860109595,
137    0.768238607022126250082483,
138    0.209793185936509782784315,
139    0.0319569316899913392596356,
140    0.00213363160895785378615014,
141];
142
143/// Polynomial coefficients for a numerator in `erf_impl`
144/// in the interval [1.25, 2.25].
145const ERF_IMPL_DN: &[f64] = &[
146    -0.0300838560557949717328341,
147    0.0538578829844454508530552,
148    0.0726211541651914182692959,
149    0.0367628469888049348429018,
150    0.00964629015572527529605267,
151    0.00133453480075291076745275,
152    0.778087599782504251917881e-4,
153];
154
155/// Polynomial coefficients for a denominator in `erf_impl`
156/// in the interval [1.25, 2.25].
157const ERF_IMPL_DD: &[f64] = &[
158    1.0,
159    1.75967098147167528287343,
160    1.32883571437961120556307,
161    0.552528596508757581287907,
162    0.133793056941332861912279,
163    0.0179509645176280768640766,
164    0.00104712440019937356634038,
165    -0.106640381820357337177643e-7,
166];
167
168///  Polynomial coefficients for a numerator in `erf_impl`
169/// in the interval [2.25, 3.5].
170const ERF_IMPL_EN: &[f64] = &[
171    -0.0117907570137227847827732,
172    0.014262132090538809896674,
173    0.0202234435902960820020765,
174    0.00930668299990432009042239,
175    0.00213357802422065994322516,
176    0.00025022987386460102395382,
177    0.120534912219588189822126e-4,
178];
179
180/// Polynomial coefficients for a denominator in `erf_impl`
181/// in the interval [2.25, 3.5].
182const ERF_IMPL_ED: &[f64] = &[
183    1.0,
184    1.50376225203620482047419,
185    0.965397786204462896346934,
186    0.339265230476796681555511,
187    0.0689740649541569716897427,
188    0.00771060262491768307365526,
189    0.000371421101531069302990367,
190];
191
192/// Polynomial coefficients for a numerator in `erf_impl`
193/// in the interval [3.5, 5.25].
194const ERF_IMPL_FN: &[f64] = &[
195    -0.00546954795538729307482955,
196    0.00404190278731707110245394,
197    0.0054963369553161170521356,
198    0.00212616472603945399437862,
199    0.000394984014495083900689956,
200    0.365565477064442377259271e-4,
201    0.135485897109932323253786e-5,
202];
203
204/// Polynomial coefficients for a denominator in `erf_impl`
205/// in the interval [3.5, 5.25].
206const ERF_IMPL_FD: &[f64] = &[
207    1.0,
208    1.21019697773630784832251,
209    0.620914668221143886601045,
210    0.173038430661142762569515,
211    0.0276550813773432047594539,
212    0.00240625974424309709745382,
213    0.891811817251336577241006e-4,
214    -0.465528836283382684461025e-11,
215];
216
217/// Polynomial coefficients for a numerator in `erf_impl`
218/// in the interval [5.25, 8].
219const ERF_IMPL_GN: &[f64] = &[
220    -0.00270722535905778347999196,
221    0.0013187563425029400461378,
222    0.00119925933261002333923989,
223    0.00027849619811344664248235,
224    0.267822988218331849989363e-4,
225    0.923043672315028197865066e-6,
226];
227
228/// Polynomial coefficients for a denominator in `erf_impl`
229/// in the interval [5.25, 8].
230const ERF_IMPL_GD: &[f64] = &[
231    1.0,
232    0.814632808543141591118279,
233    0.268901665856299542168425,
234    0.0449877216103041118694989,
235    0.00381759663320248459168994,
236    0.000131571897888596914350697,
237    0.404815359675764138445257e-11,
238];
239
240/// Polynomial coefficients for a numerator in `erf_impl`
241/// in the interval [8, 11.5].
242const ERF_IMPL_HN: &[f64] = &[
243    -0.00109946720691742196814323,
244    0.000406425442750422675169153,
245    0.000274499489416900707787024,
246    0.465293770646659383436343e-4,
247    0.320955425395767463401993e-5,
248    0.778286018145020892261936e-7,
249];
250
251/// Polynomial coefficients for a denominator in `erf_impl`
252/// in the interval [8, 11.5].
253const ERF_IMPL_HD: &[f64] = &[
254    1.0,
255    0.588173710611846046373373,
256    0.139363331289409746077541,
257    0.0166329340417083678763028,
258    0.00100023921310234908642639,
259    0.24254837521587225125068e-4,
260];
261
262/// Polynomial coefficients for a numerator in `erf_impl`
263/// in the interval [11.5, 17].
264const ERF_IMPL_IN: &[f64] = &[
265    -0.00056907993601094962855594,
266    0.000169498540373762264416984,
267    0.518472354581100890120501e-4,
268    0.382819312231928859704678e-5,
269    0.824989931281894431781794e-7,
270];
271
272/// Polynomial coefficients for a denominator in `erf_impl`
273/// in the interval [11.5, 17].
274const ERF_IMPL_ID: &[f64] = &[
275    1.0,
276    0.339637250051139347430323,
277    0.043472647870310663055044,
278    0.00248549335224637114641629,
279    0.535633305337152900549536e-4,
280    -0.117490944405459578783846e-12,
281];
282
283/// Polynomial coefficients for a numerator in `erf_impl`
284/// in the interval [17, 24].
285const ERF_IMPL_JN: &[f64] = &[
286    -0.000241313599483991337479091,
287    0.574224975202501512365975e-4,
288    0.115998962927383778460557e-4,
289    0.581762134402593739370875e-6,
290    0.853971555085673614607418e-8,
291];
292
293/// Polynomial coefficients for a denominator in `erf_impl`
294/// in the interval [17, 24].
295const ERF_IMPL_JD: &[f64] = &[
296    1.0,
297    0.233044138299687841018015,
298    0.0204186940546440312625597,
299    0.000797185647564398289151125,
300    0.117019281670172327758019e-4,
301];
302
303/// Polynomial coefficients for a numerator in `erf_impl`
304/// in the interval [24, 38].
305const ERF_IMPL_KN: &[f64] = &[
306    -0.000146674699277760365803642,
307    0.162666552112280519955647e-4,
308    0.269116248509165239294897e-5,
309    0.979584479468091935086972e-7,
310    0.101994647625723465722285e-8,
311];
312
313/// Polynomial coefficients for a denominator in `erf_impl`
314/// in the interval [24, 38].
315const ERF_IMPL_KD: &[f64] = &[
316    1.0,
317    0.165907812944847226546036,
318    0.0103361716191505884359634,
319    0.000286593026373868366935721,
320    0.298401570840900340874568e-5,
321];
322
323/// Polynomial coefficients for a numerator in `erf_impl`
324/// in the interval [38, 60].
325const ERF_IMPL_LN: &[f64] = &[
326    -0.583905797629771786720406e-4,
327    0.412510325105496173512992e-5,
328    0.431790922420250949096906e-6,
329    0.993365155590013193345569e-8,
330    0.653480510020104699270084e-10,
331];
332
333/// Polynomial coefficients for a denominator in `erf_impl`
334/// in the interval [38, 60].
335const ERF_IMPL_LD: &[f64] = &[
336    1.0,
337    0.105077086072039915406159,
338    0.00414278428675475620830226,
339    0.726338754644523769144108e-4,
340    0.477818471047398785369849e-6,
341];
342
343/// Polynomial coefficients for a numerator in `erf_impl`
344/// in the interval [60, 85].
345const ERF_IMPL_MN: &[f64] = &[
346    -0.196457797609229579459841e-4,
347    0.157243887666800692441195e-5,
348    0.543902511192700878690335e-7,
349    0.317472492369117710852685e-9,
350];
351
352/// Polynomial coefficients for a denominator in `erf_impl`
353/// in the interval [60, 85].
354const ERF_IMPL_MD: &[f64] = &[
355    1.0,
356    0.052803989240957632204885,
357    0.000926876069151753290378112,
358    0.541011723226630257077328e-5,
359    0.535093845803642394908747e-15,
360];
361
362/// Polynomial coefficients for a numerator in `erf_impl`
363/// in the interval [85, 110].
364const ERF_IMPL_NN: &[f64] = &[
365    -0.789224703978722689089794e-5,
366    0.622088451660986955124162e-6,
367    0.145728445676882396797184e-7,
368    0.603715505542715364529243e-10,
369];
370
371/// Polynomial coefficients for a denominator in `erf_impl`
372/// in the interval [85, 110].
373const ERF_IMPL_ND: &[f64] = &[
374    1.0,
375    0.0375328846356293715248719,
376    0.000467919535974625308126054,
377    0.193847039275845656900547e-5,
378];
379
380// **********************************************************
381// ********** Coefficients for erf_inv_impl polynomial ******
382// **********************************************************
383
384/// Polynomial coefficients for a numerator of `erf_inv_impl`
385/// in the interval [0, 0.5].
386const ERF_INV_IMPL_AN: &[f64] = &[
387    -0.000508781949658280665617,
388    -0.00836874819741736770379,
389    0.0334806625409744615033,
390    -0.0126926147662974029034,
391    -0.0365637971411762664006,
392    0.0219878681111168899165,
393    0.00822687874676915743155,
394    -0.00538772965071242932965,
395];
396
397/// Polynomial coefficients for a denominator of `erf_inv_impl`
398/// in the interval [0, 0.5].
399const ERF_INV_IMPL_AD: &[f64] = &[
400    1.0,
401    -0.970005043303290640362,
402    -1.56574558234175846809,
403    1.56221558398423026363,
404    0.662328840472002992063,
405    -0.71228902341542847553,
406    -0.0527396382340099713954,
407    0.0795283687341571680018,
408    -0.00233393759374190016776,
409    0.000886216390456424707504,
410];
411
412/// Polynomial coefficients for a numerator of `erf_inv_impl`
413/// in the interval [0.5, 0.75].
414const ERF_INV_IMPL_BN: &[f64] = &[
415    -0.202433508355938759655,
416    0.105264680699391713268,
417    8.37050328343119927838,
418    17.6447298408374015486,
419    -18.8510648058714251895,
420    -44.6382324441786960818,
421    17.445385985570866523,
422    21.1294655448340526258,
423    -3.67192254707729348546,
424];
425
426/// Polynomial coefficients for a denominator of `erf_inv_impl`
427/// in the interval [0.5, 0.75].
428const ERF_INV_IMPL_BD: &[f64] = &[
429    1.0,
430    6.24264124854247537712,
431    3.9713437953343869095,
432    -28.6608180499800029974,
433    -20.1432634680485188801,
434    48.5609213108739935468,
435    10.8268667355460159008,
436    -22.6436933413139721736,
437    1.72114765761200282724,
438];
439
440/// Polynomial coefficients for a numerator of `erf_inv_impl`
441/// in the interval [0.75, 1] with x less than 3.
442const ERF_INV_IMPL_CN: &[f64] = &[
443    -0.131102781679951906451,
444    -0.163794047193317060787,
445    0.117030156341995252019,
446    0.387079738972604337464,
447    0.337785538912035898924,
448    0.142869534408157156766,
449    0.0290157910005329060432,
450    0.00214558995388805277169,
451    -0.679465575181126350155e-6,
452    0.285225331782217055858e-7,
453    -0.681149956853776992068e-9,
454];
455
456/// Polynomial coefficients for a denominator of `erf_inv_impl`
457/// in the interval [0.75, 1] with x less than 3.
458const ERF_INV_IMPL_CD: &[f64] = &[
459    1.0,
460    3.46625407242567245975,
461    5.38168345707006855425,
462    4.77846592945843778382,
463    2.59301921623620271374,
464    0.848854343457902036425,
465    0.152264338295331783612,
466    0.01105924229346489121,
467];
468
469/// Polynomial coefficients for a numerator of `erf_inv_impl`
470/// in the interval [0.75, 1] with x between 3 and 6.
471const ERF_INV_IMPL_DN: &[f64] = &[
472    -0.0350353787183177984712,
473    -0.00222426529213447927281,
474    0.0185573306514231072324,
475    0.00950804701325919603619,
476    0.00187123492819559223345,
477    0.000157544617424960554631,
478    0.460469890584317994083e-5,
479    -0.230404776911882601748e-9,
480    0.266339227425782031962e-11,
481];
482
483/// Polynomial coefficients for a denominator of `erf_inv_impl`
484/// in the interval [0.75, 1] with x between 3 and 6.
485const ERF_INV_IMPL_DD: &[f64] = &[
486    1.0,
487    1.3653349817554063097,
488    0.762059164553623404043,
489    0.220091105764131249824,
490    0.0341589143670947727934,
491    0.00263861676657015992959,
492    0.764675292302794483503e-4,
493];
494
495/// Polynomial coefficients for a numerator of `erf_inv_impl`
496/// in the interval [0.75, 1] with x between 6 and 18.
497const ERF_INV_IMPL_EN: &[f64] = &[
498    -0.0167431005076633737133,
499    -0.00112951438745580278863,
500    0.00105628862152492910091,
501    0.000209386317487588078668,
502    0.149624783758342370182e-4,
503    0.449696789927706453732e-6,
504    0.462596163522878599135e-8,
505    -0.281128735628831791805e-13,
506    0.99055709973310326855e-16,
507];
508
509/// Polynomial coefficients for a denominator of `erf_inv_impl`
510/// in the interval [0.75, 1] with x between 6 and 18.
511const ERF_INV_IMPL_ED: &[f64] = &[
512    1.0,
513    0.591429344886417493481,
514    0.138151865749083321638,
515    0.0160746087093676504695,
516    0.000964011807005165528527,
517    0.275335474764726041141e-4,
518    0.282243172016108031869e-6,
519];
520
521/// Polynomial coefficients for a numerator of `erf_inv_impl`
522/// in the interval [0.75, 1] with x between 18 and 44.
523const ERF_INV_IMPL_FN: &[f64] = &[
524    -0.0024978212791898131227,
525    -0.779190719229053954292e-5,
526    0.254723037413027451751e-4,
527    0.162397777342510920873e-5,
528    0.396341011304801168516e-7,
529    0.411632831190944208473e-9,
530    0.145596286718675035587e-11,
531    -0.116765012397184275695e-17,
532];
533
534/// Polynomial coefficients for a denominator of `erf_inv_impl`
535/// in the interval [0.75, 1] with x between 18 and 44.
536const ERF_INV_IMPL_FD: &[f64] = &[
537    1.0,
538    0.207123112214422517181,
539    0.0169410838120975906478,
540    0.000690538265622684595676,
541    0.145007359818232637924e-4,
542    0.144437756628144157666e-6,
543    0.509761276599778486139e-9,
544];
545
546/// Polynomial coefficients for a numerator of `erf_inv_impl`
547/// in the interval [0.75, 1] with x greater than 44.
548const ERF_INV_IMPL_GN: &[f64] = &[
549    -0.000539042911019078575891,
550    -0.28398759004727721098e-6,
551    0.899465114892291446442e-6,
552    0.229345859265920864296e-7,
553    0.225561444863500149219e-9,
554    0.947846627503022684216e-12,
555    0.135880130108924861008e-14,
556    -0.348890393399948882918e-21,
557];
558
559/// Polynomial coefficients for a denominator of `erf_inv_impl`
560/// in the interval [0.75, 1] with x greater than 44.
561const ERF_INV_IMPL_GD: &[f64] = &[
562    1.0,
563    0.0845746234001899436914,
564    0.00282092984726264681981,
565    0.468292921940894236786e-4,
566    0.399968812193862100054e-6,
567    0.161809290887904476097e-8,
568    0.231558608310259605225e-11,
569];
570
571/// `erf_impl` computes the error function at `z`.
572/// If `inv` is true, `1 - erf` is calculated as opposed to `erf`
573fn erf_impl(z: f64, inv: bool) -> f64 {
574    if z < 0.0 {
575        if !inv {
576            return -erf_impl(-z, false);
577        }
578        if z < -0.5 {
579            return 2.0 - erf_impl(-z, true);
580        }
581        return 1.0 + erf_impl(-z, false);
582    }
583
584    let result = if z < 0.5 {
585        if z < 1e-10 {
586            z * 1.125 + z * 0.003379167095512573896158903121545171688
587        } else {
588            z * 1.125
589                + z * evaluate::polynomial(z, ERF_IMPL_AN) / evaluate::polynomial(z, ERF_IMPL_AD)
590        }
591    } else if z < 110.0 {
592        let (r, b) = if z < 0.75 {
593            (
594                evaluate::polynomial(z - 0.5, ERF_IMPL_BN)
595                    / evaluate::polynomial(z - 0.5, ERF_IMPL_BD),
596                0.3440242112,
597            )
598        } else if z < 1.25 {
599            (
600                evaluate::polynomial(z - 0.75, ERF_IMPL_CN)
601                    / evaluate::polynomial(z - 0.75, ERF_IMPL_CD),
602                0.419990927,
603            )
604        } else if z < 2.25 {
605            (
606                evaluate::polynomial(z - 1.25, ERF_IMPL_DN)
607                    / evaluate::polynomial(z - 1.25, ERF_IMPL_DD),
608                0.4898625016,
609            )
610        } else if z < 3.5 {
611            (
612                evaluate::polynomial(z - 2.25, ERF_IMPL_EN)
613                    / evaluate::polynomial(z - 2.25, ERF_IMPL_ED),
614                0.5317370892,
615            )
616        } else if z < 5.25 {
617            (
618                evaluate::polynomial(z - 3.5, ERF_IMPL_FN)
619                    / evaluate::polynomial(z - 3.5, ERF_IMPL_FD),
620                0.5489973426,
621            )
622        } else if z < 8.0 {
623            (
624                evaluate::polynomial(z - 5.25, ERF_IMPL_GN)
625                    / evaluate::polynomial(z - 5.25, ERF_IMPL_GD),
626                0.5571740866,
627            )
628        } else if z < 11.5 {
629            (
630                evaluate::polynomial(z - 8.0, ERF_IMPL_HN)
631                    / evaluate::polynomial(z - 8.0, ERF_IMPL_HD),
632                0.5609807968,
633            )
634        } else if z < 17.0 {
635            (
636                evaluate::polynomial(z - 11.5, ERF_IMPL_IN)
637                    / evaluate::polynomial(z - 11.5, ERF_IMPL_ID),
638                0.5626493692,
639            )
640        } else if z < 24.0 {
641            (
642                evaluate::polynomial(z - 17.0, ERF_IMPL_JN)
643                    / evaluate::polynomial(z - 17.0, ERF_IMPL_JD),
644                0.5634598136,
645            )
646        } else if z < 38.0 {
647            (
648                evaluate::polynomial(z - 24.0, ERF_IMPL_KN)
649                    / evaluate::polynomial(z - 24.0, ERF_IMPL_KD),
650                0.5638477802,
651            )
652        } else if z < 60.0 {
653            (
654                evaluate::polynomial(z - 38.0, ERF_IMPL_LN)
655                    / evaluate::polynomial(z - 38.0, ERF_IMPL_LD),
656                0.5640528202,
657            )
658        } else if z < 85.0 {
659            (
660                evaluate::polynomial(z - 60.0, ERF_IMPL_MN)
661                    / evaluate::polynomial(z - 60.0, ERF_IMPL_MD),
662                0.5641309023,
663            )
664        } else {
665            (
666                evaluate::polynomial(z - 85.0, ERF_IMPL_NN)
667                    / evaluate::polynomial(z - 85.0, ERF_IMPL_ND),
668                0.5641584396,
669            )
670        };
671        let g = (-z * z).exp() / z;
672        g * b + g * r
673    } else {
674        0.0
675    };
676
677    if inv && z >= 0.5 {
678        result
679    } else if z >= 0.5 || inv {
680        1.0 - result
681    } else {
682        result
683    }
684}
685
686// `erf_inv_impl` computes the inverse error function where
687// `p`,`q`, and `s` are the first, second, and third intermediate
688// parameters respectively
689fn erf_inv_impl(p: f64, q: f64, s: f64) -> f64 {
690    let result = if p <= 0.5 {
691        let y = 0.0891314744949340820313;
692        let g = p * (p + 10.0);
693        let r = evaluate::polynomial(p, ERF_INV_IMPL_AN) / evaluate::polynomial(p, ERF_INV_IMPL_AD);
694        g * y + g * r
695    } else if q >= 0.25 {
696        let y = 2.249481201171875;
697        let g = (-2.0 * q.ln()).sqrt();
698        let xs = q - 0.25;
699        let r =
700            evaluate::polynomial(xs, ERF_INV_IMPL_BN) / evaluate::polynomial(xs, ERF_INV_IMPL_BD);
701        g / (y + r)
702    } else {
703        let x = (-q.ln()).sqrt();
704        if x < 3.0 {
705            let y = 0.807220458984375;
706            let xs = x - 1.125;
707            let r = evaluate::polynomial(xs, ERF_INV_IMPL_CN)
708                / evaluate::polynomial(xs, ERF_INV_IMPL_CD);
709            y * x + r * x
710        } else if x < 6.0 {
711            let y = 0.93995571136474609375;
712            let xs = x - 3.0;
713            let r = evaluate::polynomial(xs, ERF_INV_IMPL_DN)
714                / evaluate::polynomial(xs, ERF_INV_IMPL_DD);
715            y * x + r * x
716        } else if x < 18.0 {
717            let y = 0.98362827301025390625;
718            let xs = x - 6.0;
719            let r = evaluate::polynomial(xs, ERF_INV_IMPL_EN)
720                / evaluate::polynomial(xs, ERF_INV_IMPL_ED);
721            y * x + r * x
722        } else if x < 44.0 {
723            let y = 0.99714565277099609375;
724            let xs = x - 18.0;
725            let r = evaluate::polynomial(xs, ERF_INV_IMPL_FN)
726                / evaluate::polynomial(xs, ERF_INV_IMPL_FD);
727            y * x + r * x
728        } else {
729            let y = 0.99941349029541015625;
730            let xs = x - 44.0;
731            let r = evaluate::polynomial(xs, ERF_INV_IMPL_GN)
732                / evaluate::polynomial(xs, ERF_INV_IMPL_GD);
733            y * x + r * x
734        }
735    };
736    s * result
737}
738
739#[rustfmt::skip]
740#[cfg(test)]
741mod tests {
742    use std::f64;
743
744    #[test]
745    fn test_erf() {
746        assert!(super::erf(f64::NAN).is_nan());
747        assert_almost_eq!(super::erf(-1.0), -0.84270079294971486934122063508260925929606699796630291, 1e-11);
748        assert_eq!(super::erf(0.0), 0.0);
749        assert_eq!(super::erf(1e-15), 0.0000000000000011283791670955126615773132947717431253912942469337536);
750        assert_eq!(super::erf(0.1), 0.1124629160182848984047122510143040617233925185058162);
751        assert_almost_eq!(super::erf(0.2), 0.22270258921047846617645303120925671669511570710081967, 1e-16);
752        assert_eq!(super::erf(0.3), 0.32862675945912741618961798531820303325847175931290341);
753        assert_eq!(super::erf(0.4), 0.42839235504666847645410962730772853743532927705981257);
754        assert_almost_eq!(super::erf(0.5), 0.5204998778130465376827466538919645287364515757579637, 1e-9);
755        assert_almost_eq!(super::erf(1.0), 0.84270079294971486934122063508260925929606699796630291, 1e-11);
756        assert_almost_eq!(super::erf(1.5), 0.96610514647531072706697626164594785868141047925763678, 1e-11);
757        assert_almost_eq!(super::erf(2.0), 0.99532226501895273416206925636725292861089179704006008, 1e-11);
758        assert_almost_eq!(super::erf(2.5), 0.99959304798255504106043578426002508727965132259628658, 1e-13);
759        assert_almost_eq!(super::erf(3.0), 0.99997790950300141455862722387041767962015229291260075, 1e-11);
760        assert_eq!(super::erf(4.0), 0.99999998458274209971998114784032651311595142785474641);
761        assert_eq!(super::erf(5.0), 0.99999999999846254020557196514981165651461662110988195);
762        assert_eq!(super::erf(6.0), 0.99999999999999997848026328750108688340664960081261537);
763        assert_eq!(super::erf(f64::INFINITY), 1.0);
764        assert_eq!(super::erf(f64::NEG_INFINITY), -1.0);
765    }
766
767    #[test]
768    fn test_erfc() {
769        assert!(super::erfc(f64::NAN).is_nan());
770        assert_almost_eq!(super::erfc(-1.0), 1.8427007929497148693412206350826092592960669979663028, 1e-11);
771        assert_eq!(super::erfc(0.0), 1.0);
772        assert_almost_eq!(super::erfc(0.1), 0.88753708398171510159528774898569593827660748149418343, 1e-15);
773        assert_eq!(super::erfc(0.2), 0.77729741078952153382354696879074328330488429289918085);
774        assert_eq!(super::erfc(0.3), 0.67137324054087258381038201468179696674152824068709621);
775        assert_almost_eq!(super::erfc(0.4), 0.57160764495333152354589037269227146256467072294018715, 1e-15);
776        assert_almost_eq!(super::erfc(0.5), 0.47950012218695346231725334610803547126354842424203654, 1e-9);
777        assert_almost_eq!(super::erfc(1.0), 0.15729920705028513065877936491739074070393300203369719, 1e-11);
778        assert_almost_eq!(super::erfc(1.5), 0.033894853524689272933023738354052141318589520742363247, 1e-11);
779        assert_almost_eq!(super::erfc(2.0), 0.0046777349810472658379307436327470713891082029599399245, 1e-11);
780        assert_almost_eq!(super::erfc(2.5), 0.00040695201744495893956421573997491272034867740371342016, 1e-13);
781        assert_almost_eq!(super::erfc(3.0), 0.00002209049699858544137277612958232037984770708739924966, 1e-11);
782        assert_almost_eq!(super::erfc(4.0), 0.000000015417257900280018852159673486884048572145253589191167, 1e-18);
783        assert_almost_eq!(super::erfc(5.0), 0.0000000000015374597944280348501883434853833788901180503147233804, 1e-22);
784        assert_almost_eq!(super::erfc(6.0), 2.1519736712498913116593350399187384630477514061688559e-17, 1e-26);
785        assert_almost_eq!(super::erfc(10.0), 2.0884875837625447570007862949577886115608181193211634e-45, 1e-55);
786        assert_almost_eq!(super::erfc(15.0), 7.2129941724512066665650665586929271099340909298253858e-100, 1e-109);
787        assert_almost_eq!(super::erfc(20.0), 5.3958656116079009289349991679053456040882726709236071e-176, 1e-186);
788        assert_eq!(super::erfc(30.0), 2.5646562037561116000333972775014471465488897227786155e-393);
789        assert_eq!(super::erfc(50.0), 2.0709207788416560484484478751657887929322509209953988e-1088);
790        assert_eq!(super::erfc(80.0), 2.3100265595063985852034904366341042118385080919280966e-2782);
791        assert_eq!(super::erfc(f64::INFINITY), 0.0);
792        assert_eq!(super::erfc(f64::NEG_INFINITY), 2.0);
793    }
794
795    #[test]
796    fn test_erf_inv() {
797        assert!(super::erf_inv(f64::NAN).is_nan());
798        assert_eq!(super::erf_inv(-1.0), f64::NEG_INFINITY);
799        assert_eq!(super::erf_inv(0.0), 0.0);
800        assert_almost_eq!(super::erf_inv(1e-15), 8.86226925452758013649e-16, 1e-30);
801        assert_eq!(super::erf_inv(0.1), 0.08885599049425768701574);
802        assert_almost_eq!(super::erf_inv(0.2), 0.1791434546212916764927, 1e-15);
803        assert_eq!(super::erf_inv(0.3), 0.272462714726754355622);
804        assert_eq!(super::erf_inv(0.4), 0.3708071585935579290582);
805        assert_eq!(super::erf_inv(0.5), 0.4769362762044698733814);
806        assert_eq!(super::erf_inv(1.0), f64::INFINITY);
807        assert_eq!(super::erf_inv(f64::INFINITY), f64::INFINITY);
808        assert_eq!(super::erf_inv(f64::NEG_INFINITY), f64::NEG_INFINITY);
809    }
810
811    #[test]
812    fn test_erfc_inv() {
813        assert_eq!(super::erfc_inv(0.0), f64::INFINITY);
814        assert_almost_eq!(super::erfc_inv(1e-100), 15.065574702593, 1e-11);
815        assert_almost_eq!(super::erfc_inv(1e-30), 8.1486162231699, 1e-12);
816        assert_almost_eq!(super::erfc_inv(1e-20), 6.6015806223551, 1e-13);
817        assert_almost_eq!(super::erfc_inv(1e-10), 4.5728249585449249378479309946884581365517663258840893, 1e-7);
818        assert_almost_eq!(super::erfc_inv(1e-5), 3.1234132743415708640270717579666062107939039971365252, 1e-11);
819        assert_almost_eq!(super::erfc_inv(0.1), 1.1630871536766741628440954340547000483801487126688552, 1e-14);
820        assert_almost_eq!(super::erfc_inv(0.2), 0.90619380243682330953597079527631536107443494091638384, 1e-15);
821        assert_eq!(super::erfc_inv(0.5), 0.47693627620446987338141835364313055980896974905947083);
822        assert_eq!(super::erfc_inv(1.0), 0.0);
823        assert_eq!(super::erfc_inv(1.5), -0.47693627620446987338141835364313055980896974905947083);
824        assert_eq!(super::erfc_inv(2.0), f64::NEG_INFINITY);
825    }
826}