Skip to content
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
1f28cdb
Fix the accuracy of ERF and ERFC near zero and in the tail
marcin-kordas-hoc Oct 6, 2026
86e35df
docs: link the PR in the CHANGELOG entry
marcin-kordas-hoc Oct 6, 2026
986b0a3
chore: re-run CI against updated tests
marcin-kordas-hoc Oct 7, 2026
ec4130f
docs: drop the unverified distribution accuracy claim from the changelog
marcin-kordas-hoc Oct 8, 2026
329b98d
chore: re-run CI
marcin-kordas-hoc Oct 8, 2026
d0d46b7
docs: place the changelog entry under Unreleased
marcin-kordas-hoc Oct 12, 2026
edadab4
Mention the corrected inverse normal functions in the changelog
marcin-kordas-hoc Oct 15, 2026
1cb6500
Merge remote-tracking branch 'origin/develop' into fix/erf-accuracy-n…
marcin-kordas-hoc Oct 21, 2026
4cb4559
HF-482: State the measured accuracy of the erf series and the erfc co…
marcin-kordas-hoc Oct 22, 2026
605ff58
Merge origin/develop into fix/erf-accuracy-near-zero
marcin-kordas-hoc Oct 30, 2026
c14bb1b
Merge branch 'develop' into fix/erf-accuracy-near-zero
marcin-kordas-hoc Nov 3, 2026
8170cfb
Merge branch 'develop' into fix/erf-accuracy-near-zero
marcin-kordas-hoc Nov 4, 2026
b05689e
Return 0 from ERFC for a result below the smallest normal double
marcin-kordas-hoc Nov 5, 2026
f12f693
Show the NORM.S.INV change in the ERF/ERFC changelog entry
marcin-kordas-hoc Nov 10, 2026
1cd3ba9
Merge branch 'develop' into fix/erf-accuracy-near-zero
marcin-kordas-hoc Nov 10, 2026
b2088a8
Keep develop's order of the CHANGELOG sections
marcin-kordas-hoc Nov 10, 2026
c1cba10
Merge remote-tracking branch 'origin/develop' into fix/erf-accuracy-n…
marcin-kordas-hoc Nov 22, 2026
8a7eb02
Merge remote-tracking branch 'origin/develop' into fix/erf-accuracy-n…
marcin-kordas-hoc Nov 25, 2026
3be5e14
Merge remote-tracking branch 'origin/develop' into fix/erf-accuracy-n…
marcin-kordas-hoc Nov 27, 2026
e6f078b
Let NaN reach the continued fraction in erfc instead of a separate guard
marcin-kordas-hoc Nov 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
96 changes: 63 additions & 33 deletions hyperformula/src/interpreter/plugin/3rdparty/jstat/jstat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,76 @@
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)

t = 2 / (2 + x)
ty = 4 * t - 2
/**
* 2^-1022, the smallest positive double with full precision; smaller ones are subnormal.
*/
const SMALLEST_NORMAL_DOUBLE = 2.2250738585072014e-308

for (; j > 0; j--) {
tmp = d
d = ty * d - dd + cof[j]
dd = tmp
/**
* 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.
*/
Comment thread
Tobiadefami marked this conversation as resolved.
Resolved
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
}

res = t * Math.exp(-x * x + 0.5 * (cof[0] + ty * d) - dd)
return isneg ? res - 1 : 1 - res
/**
* 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)
}

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 (Number.isNaN(x)) {
return x
}
if (x < 0) {
return 2 - erfc(-x)
}
if (x < 1) {
return 1 - erfSeries(x)
}
// erfc(27.3) is below the smallest double
const result = x < 27.3 ? erfcFraction(x) : 0
// A result below the smallest normal double (from about x = 26.55) is returned as 0
return result < SMALLEST_NORMAL_DOUBLE ? 0 : result
}
Comment thread
Tobiadefami marked this conversation as resolved.
Resolved

function erfcinv(p: number): number {
Expand Down
Loading