Skip to content

Instantly share code, notes, and snippets.

@duckworthd
Created December 26, 2011 02:08
Show Gist options
  • Save duckworthd/1520395 to your computer and use it in GitHub Desktop.
Save duckworthd/1520395 to your computer and use it in GitHub Desktop.
erf() and erfi()
private val a = 0.147 // constant for calculating erf/erfi
/**
* An approximation to the error function
*/
def erf(x: Double) = {
val x2 = x*x
val inner = exp(-1*x2*(4/pi + a*x2) /
(1.0 + a*x2))
signum(x)*sqrt(1.0 - inner)
}
def erfi(x: Double) = {
val x2 = x*x
val i1 = 2.0/(pi*a) + log(1-x2)/2.0
val i2 = log(1-x2)/a
signum(x) * sqrt( sqrt(i1*i1 - i2) - i1 )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment