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