| 413 | const tiny = 1e-300; |
| 414 | let f = x === 0 ? tiny : x; |
| 415 | let c = f; |
| 416 | let d = 0; |
| 417 | for (let k = 1; k <= 500; k++) { |
| 418 | const a = k / 2; |
| 419 | d = x + a * d; |
| 420 | if (d === 0) d = tiny; |
| 421 | d = 1 / d; |
| 422 | c = x + a / c; |
| 423 | if (c === 0) c = tiny; |
| 424 | const delta = c * d; |
| 425 | f *= delta; |
| 426 | if (Math.abs(delta - 1) < 1e-17) break; |
| 427 | } |
| 428 | return Math.exp(-x * x) / (Math.sqrt(Math.PI) * f); |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * The Gauss error function, erf(x), accurate to full machine (double) |
| 433 | * precision. |
| 434 | * |
| 435 | * Computed from the well-conditioned Maclaurin series (DLMF 7.6.2): |
| 436 | * erf(x) = (2/√π) e^{-x²} Σ_{n≥0} 2^n x^{2n+1} / (1·3·5···(2n+1)) |
| 437 | * All terms are positive, so there is no subtractive cancellation. For |