Skip to content

fix: add the missing sqrt(-ln(x)) >= 18 branch to math/base/special/erfcinv - #15760

Open
Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/erfcinv-far-tail
Open

Abhist17 wants to merge 1 commit into
stdlib-js:developfrom
Abhist17:fix/erfcinv-far-tail

Conversation

@Abhist17

@Abhist17 Abhist17 commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

Description

What is the purpose of this pull request?

This pull request:

  • adds the missing rational approximation for 18 <= sqrt(-ln(x)) < 44 to math/base/special/erfcinv (JS and C), which fixes its accuracy for x below about 1e-140.

The implementation follows Boost's erf_inv_imp, which for small x uses a series of rational approximations in p = sqrt(-ln(x)), one each for p < 3, p < 6, p < 18, p < 44 and p >= 44. The JSDoc here already lists all five error bounds, but the code stops at the third. Every p >= 6 goes through the [6, 18) approximation, and past p = 18 (x ~ 1e-141) it is extrapolating. For doubles, p reaches 27.28... at the smallest subnormal, so the [18, 44) branch is the only missing one, and p >= 44 can't occur.

Error against 2048-bit erfcinv (Julia, SpecialFunctions) on x = 10^-k for k = 100..323, plus subnormals:

max error points over 2 ULP
develop 1,340 ULP (at 5e-324) 131 of 228
this PR 1 ULP 0

Changes:

  • scripts/evalrational.js gains Boost's P/Q coefficients for the [18, 44) range (padded with zeros like the other tables). It generates lib/rational_p6q6.js and the rational_p6q6 function in src/main.c, inside new BEGIN/END markers.
  • lib/main.js and src/main.c gain Y6 = 0.99714565277099609375 and the q < 18 / q >= 18 split.
  • Tests: there's a new x_tiny.json fixture (1,001 points, x log-spaced over [1e-323, 1e-15] plus 5e-324), generated by a gen_tiny function in runner.jl that evaluates erfcinv in 2048-bit BigFloat, with a 2-ULP tolerance. On develop, 416 of its assertions fail. With this change, all 7,013 assertions pass in both test.js and test.native.js.

Related Issues

Does this pull request have any related issues?

This pull request has the following related issues:

Questions

Any questions for reviewers of this pull request?

No.

Other

Any other information relevant to this pull request? This may include screenshots, references, and/or implementation notes.

  • Running the whole runner.jl with a current JSON.jl fails on the existing x_1.9999..8_2.json block, because erfcinv( 2.0 ) is -Inf and newer JSON.jl refuses to write it. So I generated only the new fixture, with the same gen_tiny code, and the existing fixture files are unchanged.
  • The generator also rewrites rational_p1q1.js to rational_p5q5.js, but only their copyright year changed, so I left them out of the diff.

Checklist

Please ensure the following tasks are completed before submitting this pull request.

AI Assistance

When authoring the changes proposed in this PR, did you use any kind of AI assistance?

  • Yes
  • No

If you answered "yes" above, how did you use AI assistance?

  • Code generation (e.g., when writing an implementation or fixing a bug)
  • Test/benchmark generation
  • Documentation (including examples)
  • Research and understanding

Disclosure

If you answered "yes" to using AI assistance, please provide a short disclosure indicating how you used AI assistance. This helps reviewers determine how much scrutiny to apply when reviewing your contribution. Example disclosures: "This PR was written primarily by Claude Code.

This PR was written primarily by Claude Code.


@stdlib-js/reviewers

…l/erfcinv`

The Boost implementation this package follows has a separate rational
approximation for `18 <= sqrt(-ln(x)) < 44`, and the JSDoc already
lists its error bound, but the code used the `[6, 18)` approximation
for every `sqrt(-ln(x)) >= 6`. Outside its range that approximation
drifts: for `x` below about `1e-140` the error grows to 1,340 ULP at the
smallest subnormal. Add the `[18, 44)` rational approximation (which
covers every double, as the smallest gives `27.28...`) in JS and C,
generated by `scripts/evalrational.js`, and add a fixture for tiny `x`
computed in 2048-bit precision.
@Abhist17
Abhist17 requested a review from a team October 1, 2026 22:24
@stdlib-bot stdlib-bot added Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review. and removed Needs Review A pull request which needs code review. labels Oct 1, 2026
@stdlib-bot

Copy link
Copy Markdown
Contributor

Coverage Report

Package Statements Branches Functions Lines
math/base/special/erfcinv $\\color{red}707/731$
$\\color{green}+96.72\\%$
$\\color{red}53/66$
$\\color{green}+80.30\\%$
$\\color{green}8/8$
$\\color{green}+100.00\\%$
$\\color{red}707/731$
$\\color{green}+96.72\\%$

The above coverage report was generated for the changes in this PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Math Issue or pull request specific to math functionality. Needs Review A pull request which needs code review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants