diff --git a/CHANGELOG.md b/CHANGELOG.md index e744a38dd7..306df5172a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -72,6 +72,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Fixed the `ADDRESS` function adding the sheet separator (`!`) when the `sheetName` argument is an empty argument slot (e.g. `=ADDRESS(2,3,1,FALSE(),)` now returns `R2C3` instead of `!R2C3`), while an explicit `""` still adds it. [#1641](https://github.com/handsontable/hyperformula/issues/1641) - Fixed `IMSUM`, `IMPRODUCT` and `TEXTJOIN` throwing an uncaught `RangeError` for very large ranges, such as `=IMSUM(A1:D40000)`, which stopped the recalculation; they now return a result or an error value in the cell. [#1901](https://github.com/handsontable/hyperformula/pull/1901) - Fixed `AVEDEV`, `DEVSQ`, `GCD`, `GEOMEAN`, `HARMEAN`, `LCM`, `MEDIAN`, `NPV`, `SKEW` and `SKEW.P` stopping the engine from building on a range of about 125,000 cells or more. [#1800](https://github.com/handsontable/hyperformula/pull/1800) +- Fixed the `ERF` function losing precision near zero and the `ERFC` function losing it for large arguments, which also corrected `NORM.INV`, `NORM.S.INV` and `LOGNORM.INV` in the tails (e.g. `=NORM.S.INV(1e-20)` now returns `-9.2623400898` instead of `-9.1468519416`). [#1795](https://github.com/handsontable/hyperformula/pull/1795) ## [3.5.0] - 2026-10-09 diff --git a/hyperformula/src/interpreter/plugin/3rdparty/jstat/jstat.ts b/hyperformula/src/interpreter/plugin/3rdparty/jstat/jstat.ts index 94e10c8dd5..99f783327b 100644 --- a/hyperformula/src/interpreter/plugin/3rdparty/jstat/jstat.ts +++ b/hyperformula/src/interpreter/plugin/3rdparty/jstat/jstat.ts @@ -21,46 +21,74 @@ THE SOFTWARE. */ -export function erf(x: number): number { - const cof = [-1.3026537197817094, 6.4196979235649026e-1, 1.9476473204185836e-2, - -9.561514786808631e-3, -9.46595344482036e-4, 3.66839497852761e-4, - 4.2523324806907e-5, -2.0278578112534e-5, -1.624290004647e-6, - 1.303655835580e-6, 1.5626441722e-8, -8.5238095915e-8, - 6.529054439e-9, 5.059343495e-9, -9.91364156e-10, - -2.27365122e-10, 9.6467911e-11, 2.394038e-12, - -6.886027e-12, 8.94487e-13, 3.13092e-13, - -1.12708e-13, 3.81e-16, 7.106e-15, - -1.523e-15, -9.4e-17, 1.21e-16, - -2.8e-17] - let j = cof.length - 1 - let isneg = false - let d = 0 - let dd = 0 - let t, ty, tmp, res - - if (x === 0) { - return 0 - } - if (x < 0) { - x = -x - isneg = true - } +const SQRT_PI = Math.sqrt(Math.PI) + +/** + * 2^-1022, the smallest positive double with full precision; smaller ones are subnormal. + */ +const SMALLEST_NORMAL_DOUBLE = 2.2250738585072014e-308 - t = 2 / (2 + x) - ty = 4 * t - 2 +/** + * erf(x) for 0 <= x < 2 from its series exp(-x^2) * 2 / sqrt(pi) * sum(2^n * x^(2n + 1) / (2n + 1)!!). + * All terms are positive, so there is no cancellation, and the relative error stays below about 1.1e-15 down to the + * smallest numbers. + */ +function erfSeries(x: number): number { + const xx = x * x + let term = x + let sum = x + for (let n = 0; n < 500; n++) { + term *= 2 * xx / (2 * n + 3) + sum += term + if (term < sum * 1e-17) { + break + } + } + return 2 / SQRT_PI * Math.exp(-xx) * sum +} - for (; j > 0; j--) { - tmp = d - d = ty * d - dd + cof[j] - dd = tmp +/** + * erfc(x) for x >= 1 from the continued fraction exp(-x^2) / sqrt(pi) / (x + (1/2) / (x + 1 / (x + (3/2) / (x + ...)))), + * evaluated from the tail. The relative error is below about 4e-15 up to x = 6.5 and grows with x^2 beyond that, + * to about 6e-14 near x = 24, because x * x is rounded before exp(-x * x). It stays accurate where erfc is tiny + * and 1 - erf(x) would be 0. + */ +function erfcFraction(x: number): number { + let tail = 0 + for (let k = 300; k >= 1; k--) { + tail = (k / 2) / (x + tail) } + return Math.exp(-x * x) / SQRT_PI / (x + tail) +} - res = t * Math.exp(-x * x + 0.5 * (cof[0] + ty * d) - dd) - return isneg ? res - 1 : 1 - res +export function erf(x: number): number { + if (x === 0 || Number.isNaN(x)) { + return x + } + const abs = Math.abs(x) + let result + if (abs < 2) { + result = erfSeries(abs) + } else if (abs < 6.5) { + result = 1 - erfcFraction(abs) + } else { + result = 1 + } + return x < 0 ? -result : result } export function erfc(x: number): number { - return 1 - erf(x) + if (x < 0) { + return 2 - erfc(-x) + } + if (x < 1) { + return 1 - erfSeries(x) + } + // erfc(27.3) is below the smallest double. NaN fails every comparison, so it is tested with >= to reach + // erfcFraction, which returns NaN; x < 27.3 would turn it into 0. + const result = x >= 27.3 ? 0 : erfcFraction(x) + // A result below the smallest normal double (from about x = 26.55) is returned as 0 + return result < SMALLEST_NORMAL_DOUBLE ? 0 : result } function erfcinv(p: number): number {