| 16 | * Except for low and high x, it is based on the type erf(x) = sgn(x) * sqrt(1-exp(-x^2*f(x)), |
| 17 | * with the function f(x) having a smooth transition from 4/pi = 1.273... at x=0 to 1 at high x */ |
| 18 | public static double erf(double x) { |
| 19 | double x2 = sqr(x); |
| 20 | if (x2 < 1e-8) |
| 21 | return (2/Math.sqrt(Math.PI))*x*(1+x2*(-1./3.+x2*(1./10.))); // Taylor series for low x |
| 22 | double erf = x2 > 36 ? 1 : //the polynomials go crazy for some large x2; erf(6) is 1 - 2e-17, less than ulp from 1 |
| 23 | Math.sqrt(1 - Math.exp(-sqr(x*((2/Math.sqrt(Math.PI) - A) + A * |
| 24 | (1 + x2*x2*(B + x2*(C + x2*(H + x2*I)))) / |
| 25 | (1 + x2*(D + x2*(E + x2*(F + x2*G)))) |
| 26 | )))); |
| 27 | return x>0 ? erf : -erf; //could be Math.copySign in Java 1.6 & up |
| 28 | } |
| 29 | |
| 30 | static double sqr(double x) { |
| 31 | return x*x; |