Skip to content

math: compute Erfcinv accurately for small arguments - #82104

Open
shogo82148 wants to merge 1 commit into
golang:masterfrom
shogo82148:issue-82102-erfcinv
Open

shogo82148 wants to merge 1 commit into
golang:masterfrom
shogo82148:issue-82102-erfcinv

Conversation

@shogo82148

@shogo82148 shogo82148 commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Erfcinv(x) was implemented as Erfinv(1-x).
For small x, computing 1-x discards most of the information in x,
and the large derivative of Erfinv near 1 amplifies the rounding error.
For x <= 2**-54, 1-x rounds to 1 and Erfcinv returned +Inf.
Tiny negative arguments such as -1e-40, which are outside the domain,
also returned +Inf instead of NaN.

Split the rational approximations of Erfinv into helpers
and give Erfcinv its own implementation. It checks the domain directly,
uses the identity Erfcinv(x) = -Erfcinv(2-x) for x > 1 (2-x is exact there),
and evaluates the tail approximation from x itself instead of from 1-x.

Fixes #82102

Erfcinv(x) was implemented as Erfinv(1-x). For small x, computing
1-x discards most of the information in x, and the large derivative
of Erfinv near 1 amplifies the rounding error. For x <= 2**-54,
1-x rounds to 1 and Erfcinv returned +Inf. Tiny negative arguments
such as -1e-40, which are outside the domain, also returned +Inf
instead of NaN.

Split the rational approximations of Erfinv into helpers and give
Erfcinv its own implementation. It checks the domain directly, uses
the identity Erfcinv(x) = -Erfcinv(2-x) for x > 1 (2-x is exact
there), and evaluates the tail approximation from x itself instead
of from 1-x.

Fixes golang#82102
@shogo82148
shogo82148 force-pushed the issue-82102-erfcinv branch 2 times, most recently from 7d177bf to 8c25c45 Compare October 9, 2026 17:27
@gopherbot

Copy link
Copy Markdown
Contributor

This PR (HEAD: 8c25c45) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/848186.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

math: Erfcinv loses precision and returns +Inf for small arguments

2 participants