Repository navigation
Fix the accuracy of ERF and ERFC near zero and for large arguments (HF-482) - #1795
marcin-kordas-hoc wants to merge 20 commits into
Conversation
Deploying with
|
| Status | Name | Latest Commit | Preview URL | Updated (UTC) |
|---|---|---|---|---|
| ✅ Deployment successful! View logs |
hyperformula-docs | e6f078b | Commit Preview URL Branch Preview URL |
Nov 29 2026, 03:24 PM |
Performance comparison of head (e6f078b) vs base (ce5e52f) |
854d69d to
f7728e9
Compare
Bugbot is paused — on-demand spend limit reachedBugbot uses usage-based billing for this team and has hit its on-demand spend limit. A team admin can raise the spend limit in the Cursor dashboard, or wait for the next billing cycle to continue. |
erf is computed from its series below 2 and from the continued fraction of erfc above, instead of 1 - exp(...), which lost every digit below about 1e-16. erfc uses the continued fraction from 1 upwards. Relative error against Python's math.erf and math.erfc is at most 7e-16 for erf and 2e-16 for erfc. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
f7728e9 to
d0d46b7
Compare
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Bugbot is paused — on-demand spend limit reachedBugbot uses usage-based billing for this team and has hit its on-demand spend limit. A team admin can raise the spend limit in the Cursor dashboard, or wait for the next billing cycle to continue. |
…ntinued fraction Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # CHANGELOG.md
# Conflicts: # CHANGELOG.md
Tobiadefami
left a comment
There was a problem hiding this comment.
hyperformula/src/interpreter/plugin/3rdparty/jstat/jstat.ts:306
Not for this PR, but worth its own task: the normal and lognormal CDFs still compute the left tail as 0.5 * (1 + erf(z)), the same cancellation this PR removes from ERFC, so they don't benefit from the new erfc:
const hf = HyperFormula.buildFromArray([[
'=NORM.S.DIST(-10,TRUE())', // 0 Microsoft Excel Online: 7.61985302416048E-24
'=NORM.S.DIST(-8,TRUE())', // 6.1062266354E-16 Microsoft Excel Online: 6.22096057427174E-16
'=LOGNORM.DIST(0.00001,0,1,TRUE())', // 0 Microsoft Excel Online: 5.67797929684089E-31
]], { licenseKey: 'gpl-v3' })Rewriting them as 0.5 * erfc(-(x - mean) / (std * Math.SQRT2)) (and the same for the lognormal) would fix all three. Could you open a follow-up task for it?
From about x = 26.55 the continued fraction gives a subnormal double, which Excel Online returns as 0 (=ERFC(26.54) is 2.64555817446854E-308, =ERFC(26.55) is 0). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
About the left tail of the normal and lognormal distributions: at b05689e, |
Tobiadefami
left a comment
There was a problem hiding this comment.
hyperformula/src/interpreter/plugin/StatisticalPlugin.ts:491
suggestion: Not for this PR, but worth its own task: GAUSS doesn't benefit from the new erf. It computes normal.cdf(z, 0, 1) - 0.5, and adding and then removing 0.5 cancels a small result:
HyperFormula.buildFromArray([['=GAUSS(1e-20)']], {licenseKey: 'gpl-v3'})
// 0, Microsoft Excel Online: 3.98942280401433E-21develop gives the same 0, and it isn't the left-tail case listed under Out of scope. Could you open a follow-up task to compute GAUSS from erf directly?
The entry now says which function changed where (ERF near zero, ERFC for large arguments) and gives the NORM.S.INV(1e-20) result before and after. The erfSeries comment no longer describes the code it replaced. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
# Conflicts: # CHANGELOG.md
The develop merge moved the Changed section; develop lists it first. The entry of this pull request stays in Fixed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Agreed; the follow-up task is HF-594. |
…ear-zero # Conflicts: # CHANGELOG.md
There was a problem hiding this comment.
Note
Medium risk: Replaces the core erf/erfc numerics used by many statistical formulas (ERF, ERFC, NORM.DIST, NORM.S.INV, GAUSS, LOGNORM, CONFIDENCE.NORM, Z.TEST), so accuracy and edge-case behaviour of all of them shift at once; the change is self-contained and heavily validated against Excel.
Full review: no issues found.
Want a deeper review? Comment /review improve on its own line: a full review of the latest commit with an extra fix-oriented pass (about 2x the cost).
…ear-zero # Conflicts: # CHANGELOG.md
### Context The PR adds the `ERF.PRECISE` function. - **Why:** it is a standard Excel function that HyperFormula did not have, so formulas using it returned `#NAME?`. - **What changed:** - `StatisticalPlugin.ts`: `ERF.PRECISE(x)`, computed by the same `erf` as the one-argument `ERF`. - `ERF.PRECISE` reads its argument through `ArithmeticHelper.coerceScalarToNumberRejectingBooleans`, which `GESTEP` (#1788) added on `develop`: it returns `#VALUE!` for a boolean or the empty text. `ArithmeticHelper.ts` is not changed. - `engineering.ts`: catalogue entry. `functionCapabilities.ts`: `ERF.PRECISE` in the license capability table. - 17 language packs: names from the Excel 2013 translation table (dolf.trieschnigg.nl), a source listed in `hyperformula/dev-docs/I18N.md`, except `ptPT`. That pack uses the Brazilian Portuguese names (`ERF` is `FUNERRO`), so `ERF.PRECISE` is `FUNERRO.PRECISO`, as on Microsoft's pt-BR function page; the table gives the European `FUNCERRO.PRECISO`. - `docs/guide/list-of-differences.md`: a paragraph on accuracy near zero (see Out of scope), a row for `=ERF.PRECISE(A1:B1)` in A5, which returns a number here and `#VALUE!` in Excel, and a row for `=ERF.PRECISE(A1:A3)` with `useArrayArithmetic: true`, which spills one value per cell here. - `CHANGELOG.md`: one entry. - **Result:** `ERF.PRECISE(x)` returns the error function integrated from 0 to `x`. - Numbers, dates, numeric text and empty cells are accepted. - A boolean and the empty text `""` are `#VALUE!`, as in Excel. An error argument is returned. `ERF` and `ERFC` keep their current handling of booleans here; that change is in #1836, which can use the same `coerceScalarToNumberRejectingBooleans`. ### How did you test your changes? - Paired tests branch: `feat/hf-482-erf-precise` in `hyperformula-tests`, spec `unit/interpreter/function-erf.precise.spec.ts`: 72 tests with one assertion each, all passing. Two of them use the Polish name `FUNKCJA.BŁ.DOKŁ`: parsed in a `plPL` engine, and shown in `plPL` for a formula entered in English; both fail when the `plPL` entry is misspelled. Excel Online shows the same Polish name. A third calls `FUNERRO.PRECISO` in a `ptPT` engine and fails with `FUNCERRO.PRECISO`. - With the boolean and empty-text check removed from `coerceScalarToNumberRejectingBooleans`, the 5 boolean cases fail. `""` still gives `#VALUE!`, because `coerceScalarToNumberOrError` has rejected it since #1768. - Full Jest run at this head with the paired tests attached (CI): 16479 passed, 3 skipped, 0 failed, in 582 suites. `tsc --noEmit`: 0 errors. ESLint on the changed `.ts` files (engine and tests): 0 errors; the full lint runs in CI. - 54 `ERF.PRECISE`, `ERF` and `ERFC` formulas were measured earlier in Excel Online (Microsoft Graph). Replayed in HyperFormula at this head, relative tolerance 1e-12: 41 identical, 11 different, 2 not evaluable in Excel (zero and two arguments). - 2: `ERF(TRUE())` and `ERFC(TRUE())` are `#VALUE!` in Excel and numbers in HyperFormula (#1836). - 2: `ERF.PRECISE(1e-10)` and `ERF.PRECISE(1e-300)` lose relative accuracy in the shared `erf` (fixed in #1795). - 6: decimal-separator cases. The Excel account used Polish settings, where `"0,5"` is a number and `"0.5"` is not. - 1: a two-cell range in a one-value argument, recorded in the list of differences. - Edge inputs from the edge matrix (48 cases: empty cell, text, booleans, errors, magnitudes, ranges, array literals, names) measured in Excel Online on 2026-11-05: 14 differ. 10 are ranges of more than one cell, `#VALUE!` in Excel, where HyperFormula takes one cell or spills (the list-of-differences row); 2 are `1e-7` and `1e-300` (accuracy, fixed in #1795); 2 are inputs Excel does not accept (no argument, `1e308`). - The accuracy paragraph was checked against the Maclaurin series of `erf` (60-digit decimal arithmetic) for 120 arguments from `1e-10` to `0.9`: at the 11 significant digits returned by default, every argument from `1e-6` down differs from the exact value, and the largest argument with a different last digit is `1.5e-4`. - Away from zero the results agree with Excel to the 11 significant digits HyperFormula returns by default. ### AI assistance Claude Code (Claude Opus and Claude Sonnet) wrote the code, the tests and the documentation changes in this pull request and in hyperformula-tests#82. Every commit on both branches, other than merges, carries its co-author line. ### Compatibility with Excel and Google Sheets Excel values are the saved Excel Online results. Google Sheets was not measured. | Formula | Data | Excel | Google Sheets | HyperFormula | |---|---|---|---|---| | `=ERF.PRECISE(0.5)` | | 0.520499877813047 | not measured | 0.52049987781 | | `=ERF.PRECISE(-1)` | | -0.842700792949715 | not measured | -0.84270079295 | | `=ERF.PRECISE(E1)` | E1 empty | 0 | not measured | 0 | | `=ERF.PRECISE("1")` | | 0.842700792949715 | not measured | 0.84270079295 | | `=ERF.PRECISE(TRUE())` | | `#VALUE!` | not measured | `#VALUE!` | | `=ERF.PRECISE("")` | | `#VALUE!` | not measured | `#VALUE!` | | `=ERF.PRECISE(F1)` | F1 `=1/0` | `#DIV/0!` | not measured | `#DIV/0!` | | `=ERF.PRECISE(1e-10)` | | 1.12837916709551E-10 | not measured | 1.128379612E-10 | | `=ERF.PRECISE(A1:B1)` in A5 | 0.5, 1 | `#VALUE!` | not measured | 0.52049987781 | ### Public API changes None. The function is listed by `getAvailableFunctions()` like any other built-in function. ### Out of scope and follow-ups - Accuracy near zero: the list-of-differences paragraph added here describes the current `erf`. #1795 fixes it, so whichever of the two merges second drops that paragraph. - English names are kept where the translation table has none: `csCZ`, `daDK`, `enGB` and `idID`. The individual Microsoft pages were checked only for `ptPT`. ### Types of changes - [ ] Breaking change (existing functionality no longer works as before) – agreed with the maintainers - [x] New feature or improvement (a non-breaking change that adds functionality) - [ ] Bug fix (a non-breaking change that fixes an issue) - [x] Additional language file, or a change to an existing language file (translations) - [x] Change to the documentation ### Related issues 1. HF-482 ### Checklist - [x] My pull request meets the [Definition of Done](https://github.com/handsontable/hyperformula/blob/develop/dev-docs/DEFINITION-OF-DONE.md). External contributors: see also [Contributing to HyperFormula](https://handsontable.com/docs/hyperformula/guide/contributing.html). - [ ] I have signed the [Contributor License Agreement](https://goo.gl/forms/yuutGuN0RjsikVpM2). - [x] I read my own diff end to end before requesting a review. - [x] I added a [CHANGELOG.md](https://github.com/handsontable/hyperformula/blob/develop/CHANGELOG.md) entry, written by [the changelog rules](https://github.com/handsontable/hyperformula/blob/develop/dev-docs/DOC-STANDARDS.md#the-changelog) – or none is needed (docs, tests or tooling only). - [x] I updated the documentation and JSDoc to match the change. - [ ] Breaking change only: I added a section to the migration guide. - [x] I updated the `dev-docs/` pages this change affects – or none needed it. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Non-breaking additive function with localized names and documentation; numerical behavior reuses existing erf and only adds targeted coercion. > > **Overview** > Adds **`ERF.PRECISE`**, the Excel one-argument Gaussian error integral from 0 to *x*, wired through **`StatisticalPlugin`** using the same **`erf`** implementation as single-argument **`ERF`**. > > Argument handling is stricter for Excel parity: a new **`ArithmeticHelper.coerceScalarToNumberRejectingBooleanOrError`** returns **`#VALUE!`** for booleans and **`""`** (while other coercions still apply). The function is registered in engineering metadata, the license capability list, **17** locale dictionaries, **CHANGELOG**, and **list-of-differences** (multi-cell range vs Excel **`#VALUE!`**, shared near-zero accuracy limits with **`ERF`**/**`ERFC`**). > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit e424abc. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> --------- Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
erfc compares x >= 27.3 so that NaN, which fails every comparison, goes to erfcFraction and comes back as NaN, without a check that no formula can reach. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1795 +/- ##
========================================
Coverage 97.76% 97.76%
========================================
Files 212 212
Lines 19159 19173 +14
Branches 4540 4547 +7
========================================
+ Hits 18730 18744 +14
+ Misses 429 421 -8
- Partials 0 8 +8
🚀 New features to boost your workflow:
|
Context
The PR fixes the accuracy of
ERFandERFCclose to zero and for large arguments.erfreturned1 - exp(...), which loses every digit below about 1e-16, anderfcwas1 - erf.hyperformula/src/interpreter/plugin/3rdparty/jstat/jstat.ts:erfuses its power series below 2 (all terms positive, no cancellation) and the continued fraction oferfcabove.erfcuses the continued fraction from 1 upwards, and returns 0 for a result below the smallest normal double,2^-1022(from about 26.55), as Excel does.CHANGELOG.md: one entry under Fixed.erfc:develop=ERF(1e-10)=ERF(1e-20)=ERFC(5)=ERFC(10)=NORM.S.INV(1e-20)=LOGNORM.INV(1e-10,0,1)How did you test your changes?
fix/erf-accuracy-near-zeroinhyperformula-tests: Excel-measured cases in theERFandERFCspecs, among them=ERFC(27)and=ERFC(26.55)(0) and=ERFC(26.54)(2.64555817446854E-308, read withsmartRoundingoff; see Out of scope), and tail cases in theNORM.S.INV,NORM.INVandLOGNORM.INVspecs.jstat.tsfromdevelop, 16 of the 56 tests in these five specs fail; at this head all pass.2^-1022, the=ERFC(27)and=ERFC(26.55)cases fail.tsc --noEmit: 0 errors. ESLint on the changed.tsfiles (engine and tests): 0 errors; the full lint runs in CI.ERFandERFCformulas from1e-300to27(both signs, and the two-argumentERF) were measured earlier in Excel Online (Microsoft Graph). Replayed at this head:develop, 14 of them differ beyond a relative 1e-9.NaNfrom the engine:ERF(1e-300)(see Out of scope).ERFCfrom 26 to 27.3 was measured in Excel Online on 2026-11-05: 2.64555817446854E-308 at 26.54, and 0 at 26.55 and every argument above it that was measured.ERFwith one and two arguments, andERFC) measured in Excel Online on 2026-11-05: no case that agrees with Excel ondevelopdiffers at this head, and 3 more agree; the others differ on both, in coercion and range handling this PR does not change (booleans and""are Return #VALUE! for boolean and empty-text arguments of DELTA, ERF and ERFC (HF-554) #1836).developon 858 formulas (ERFCfrom 26 to 28 in steps of 0.01,ERFandERFCof both signs from1e-300to 30, the two-argumentERF,NORM.S.DIST,NORM.DIST,GAUSS,LOGNORM.DIST, the three inverse functions,CONFIDENCE.NORM,Z.TEST): every result that turns from a number into a non-number is the rounding case under Out of scope,ERFCfrom 26.13 to 26.54 andERF(±1e-300).AI assistance
Claude Code (Claude Opus and Claude Sonnet) wrote the code, the tests and the documentation changes in this pull request and in hyperformula-tests#85. Every commit on both branches, other than merges, carries its co-author line.
Compatibility with Excel and Google Sheets
Excel values are the saved Excel Online results. Google Sheets was not measured.
=ERF(1e-100)=ERF(-1e-10)=ERF(0.5,1)=ERFC(8)=ERFC(-1)=NORM.INV(1e-10,10,2)=ERFC(27)=ERFC(26.54)NaN(2.6455581745E-308 with #1837)=ERF(1e-300)NaN(1.1283791671E-300 with #1837)Public API changes
None.
Out of scope and follow-ups
smartRounding, the engine turns any result below about1e-298intoNaN, and so does a plain=1e-300*1: that is HF-555, fixed by Round exported values below about 1e-298 instead of returning NaN (HF-555) #1837.ERFCnow reaches it too: from about 26.13 to 26.54 it returnsNaNwheredevelopreturned 0, and so do=ERF(1e-300)and=ERF(-1e-300). With Round exported values below about 1e-298 instead of returning NaN (HF-555) #1837 merged into this branch, the band gives Excel's values:=ERFC(26.15)is2.2557867866E-299(Excel2.25578678656069E-299) and=ERFC(26.54)is2.6455581745E-308(Excel2.64555817446854E-308).1E-10) do not parse before Accept number literals with an uppercase exponent (HF-553) #1835, so the cases use1e-10.ERF.PRECISEaccuracy near zero. This fix makes it obsolete, so whichever of the two merges second drops it.NORM.S.DIST,NORM.DISTandLOGNORM.DISTstill compute the cumulative left tail as0.5 * (1 + erf(z)), so they do not gain from the newerfc:=NORM.S.DIST(-10,TRUE())and=LOGNORM.DIST(0.00001,0,1,TRUE())are 0, where Excel returns7.61985302416048E-24and5.67797929684089E-31. Written as0.5 * erfc(-z)with thiserfc, both agree with Excel to within 3e-14 relative. That is HF-578.GAUSSis computed asnormal.cdf(z, 0, 1) - 0.5, which cancels for small arguments:=GAUSS(1e-20)is 0 here and ondevelop, where Excel returns3.98942280401433E-21. Computing it fromerfis a separate change.Types of changes
Related issues
Checklist
dev-docs/pages this change affects – or none needed it.🤖 Generated with Claude Code
Note
Medium Risk
Numerical changes in shared statistical primitives affect many formulas, but the scope is a targeted accuracy fix with no public API change.
Overview
Replaces the legacy
erf/erfcimplementation injstat.tsso ERF and ERFC match Excel across tiny and very large inputs.ERF now uses a power series for
|x| < 2and derives larger values from a dedicated ERFC continued-fraction path instead of1 - exp(...), which was wiping precision near zero. ERFC is computed directly (including negative arguments via symmetry), returns 0 when the magnitude falls below the smallest normal double—matching Excel—and no longer relies on1 - erf(x)in the tail.The CHANGELOG records the fix and notes that NORM.INV, NORM.S.INV, and LOGNORM.INV inherit better tail accuracy because they depend on ERFC.
Reviewed by Cursor Bugbot for commit b2088a8. Bugbot is set up for automated code reviews on this repo. Configure here.