Skip to content

Error functions

The error function and its inverse are the building blocks for normal distribution CDF/inverse-CDF computations. All three are pure scalar functions with no effects and an explicit {f32, f64} dtype bound.

The approximation coefficients limit erf's measured error to about 1.4e-7 at f64, compared with 4.4e-7 at f32. erfc returns exactly 0 from about x = 3.92 at f32, while f64 extends its nonzero positive tail to about x = 5.5. See the precision guide.

Signature: [prec: {f32, f64}](x: prec) -> prec

Computes the error function erf(x) = (2/sqrt(pi)) * integral(0, x, exp(-t^2) dt). Uses the Abramowitz & Stegun rational (Horner) approximation with a linear fallback for |x| < 1e-5. The function is odd: erf(-x) = -erf(x).

  • Domain: all reals
  • Range: [-1, 1] at finite precision; values round to the endpoints for sufficiently large magnitudes
  • Precision: ~1e-7 relative
import Nautilus.Special (erf)
val = erf(cast(0.5, f32)) -- approximately 0.5205
neg_val = erf(cast(-1.0, f32)) -- approximately -0.8427

Signature: [prec: {f32, f64}](x: prec) -> prec

Computes the complementary error function erfc(x) = 1 - erf(x), implemented literally as that subtraction. It therefore shares erf's absolute error and does not avoid the cancellation for large positive x, where erf(x) is close to 1: at f32 it returns exactly 0 from about x = 3.92.

  • Domain: all reals
  • Range: [0, 2] at finite precision; the result can round to either endpoint
  • Precision: ~1e-7 absolute (inherits erf's error); relative error grows in the upper tail
import Nautilus.Special (erfc)
tail = erfc(cast(2.0, f32)) -- approximately 0.0047

Signature: [prec: {f32, f64}](x: prec) -> prec

Computes the inverse error function: if y = erf(x), then x = erfinv(y). Internally delegates to the Acklam rational approximation of the normal inverse CDF, then rescales by 1/sqrt(2).

  • Domain: (-1, 1) strictly; values at or beyond the boundary produce inf/NaN
  • Precision: ~1e-8 relative
import Nautilus.Special (erfinv)
x = erfinv(cast(0.5, f32)) -- approximately 0.4769
Inputerf resulterfc resulterfinv result
0.00.01.00.0
+largeapproaches 1.0approaches 0.0n/a (outside domain)
-largeapproaches -1.0approaches 2.0n/a (outside domain)
NaNNaNNaNNaN
1.0n/an/a+inf
-1.0n/an/a-inf