Skip to content

Instantly share code, notes, and snippets.

@kcrt
Last active May 21, 2018 21:27
Show Gist options
  • Save kcrt/6210661 to your computer and use it in GitHub Desktop.
Save kcrt/6210661 to your computer and use it in GitHub Desktop.
simple implementation of mathematic function erf "Error function", in JavaScript JavaScriptによる誤差関数の実装
function erf(x){
// erf(x) = 2/sqrt(pi) * integrate(from=0, to=x, e^-(t^2) ) dt
// with using Taylor expansion,
// = 2/sqrt(pi) * sigma(n=0 to +inf, ((-1)^n * x^(2n+1))/(n! * (2n+1)))
// calculationg n=0 to 50 bellow (note that inside sigma equals x when n = 0, and 50 may be enough)
var m = 1.00;
var s = 1.00;
var sum = x * 1.0;
for(var i = 1; i < 50; i++){
m *= i;
s *= -1;
sum += (s * Math.pow(x, 2.0 * i + 1.0)) / (m * (2.0 * i + 1.0));
}
return 2 * sum / Math.sqrt(3.14159265358979);
}
@kcrt
Copy link
Author

kcrt commented Aug 12, 2013

See http://app.kcrt.net/med/#lmsvalue for sample application (in Japanese)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment