Source File
erfinv.go
Belonging Package
math
// Copyright 2017 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package math/*Inverse of the floating-point error function.*/// This implementation is based on the rational approximation// of percentage points of normal distribution available from// https://www.jstor.org/stable/2347330.const (// Coefficients for approximation to erf in |x| <= 0.85a0 = 1.1975323115670912564578e0a1 = 4.7072688112383978012285e1a2 = 6.9706266534389598238465e2a3 = 4.8548868893843886794648e3a4 = 1.6235862515167575384252e4a5 = 2.3782041382114385731252e4a6 = 1.1819493347062294404278e4a7 = 8.8709406962545514830200e2b0 = 1.0000000000000000000e0b1 = 4.2313330701600911252e1b2 = 6.8718700749205790830e2b3 = 5.3941960214247511077e3b4 = 2.1213794301586595867e4b5 = 3.9307895800092710610e4b6 = 2.8729085735721942674e4b7 = 5.2264952788528545610e3// Coefficients for approximation to erf in 0.85 < |x| <= 1-2*exp(-25)c0 = 1.42343711074968357734e0c1 = 4.63033784615654529590e0c2 = 5.76949722146069140550e0c3 = 3.64784832476320460504e0c4 = 1.27045825245236838258e0c5 = 2.41780725177450611770e-1c6 = 2.27238449892691845833e-2c7 = 7.74545014278341407640e-4d0 = 1.4142135623730950488016887e0d1 = 2.9036514445419946173133295e0d2 = 2.3707661626024532365971225e0d3 = 9.7547832001787427186894837e-1d4 = 2.0945065210512749128288442e-1d5 = 2.1494160384252876777097297e-2d6 = 7.7441459065157709165577218e-4d7 = 1.4859850019840355905497876e-9// Coefficients for approximation to erf in 1-2*exp(-25) < |x| < 1e0 = 6.65790464350110377720e0e1 = 5.46378491116411436990e0e2 = 1.78482653991729133580e0e3 = 2.96560571828504891230e-1e4 = 2.65321895265761230930e-2e5 = 1.24266094738807843860e-3e6 = 2.71155556874348757815e-5e7 = 2.01033439929228813265e-7f0 = 1.414213562373095048801689e0f1 = 8.482908416595164588112026e-1f2 = 1.936480946950659106176712e-1f3 = 2.103693768272068968719679e-2f4 = 1.112800997078859844711555e-3f5 = 2.611088405080593625138020e-5f6 = 2.010321207683943062279931e-7f7 = 2.891024605872965461538222e-15)// Erfinv returns the inverse error function of x.//// Special cases are:// Erfinv(1) = +Inf// Erfinv(-1) = -Inf// Erfinv(x) = NaN if x < -1 or x > 1// Erfinv(NaN) = NaNfunc ( float64) float64 {// special casesif IsNaN() || <= -1 || >= 1 {if == -1 || == 1 {return Inf(int())}return NaN()}:= falseif < 0 {= -= true}var float64if <= 0.85 { // |x| <= 0.85:= 0.180625 - 0.25**:= ((((((a7*+a6)*+a5)*+a4)*+a3)*+a2)*+a1)* + a0:= ((((((b7*+b6)*+b5)*+b4)*+b3)*+b2)*+b1)* + b0= ( * ) /} else {var , float64:= Sqrt(Ln2 - Log(1.0-))if <= 5.0 {-= 1.6= ((((((c7*+c6)*+c5)*+c4)*+c3)*+c2)*+c1)* + c0= ((((((d7*+d6)*+d5)*+d4)*+d3)*+d2)*+d1)* + d0} else {-= 5.0= ((((((e7*+e6)*+e5)*+e4)*+e3)*+e2)*+e1)* + e0= ((((((f7*+f6)*+f5)*+f4)*+f3)*+f2)*+f1)* + f0}= /}if {return -}return}// Erfcinv returns the inverse of Erfc(x).//// Special cases are:// Erfcinv(0) = +Inf// Erfcinv(2) = -Inf// Erfcinv(x) = NaN if x < 0 or x > 2// Erfcinv(NaN) = NaNfunc ( float64) float64 {return Erfinv(1 - )}