Repository navigation
Fix #11123: preserve representable subnormal erfc results - #11124
Merged
Merged
Conversation
Replace the normal-range cutoff with a conservative zero cutoff derived from the real format. The previous cutoff returned zero for representable 80-bit subnormal results, including erfc(106.625). In the extreme tail, evaluate exp(-x*x/2) and multiply its two factors with the scaled erfc approximation so only the final product enters the subnormal range. This avoids premature intermediate rounding while retaining the original calculation for ordinary inputs. Add regression tests for 64-bit and 80-bit subnormal results, the transition to zero, negative arguments, and large finite inputs. Validate module tests and public-API checks with Windows DMD/LDC and Linux LDC, plus high-precision boundary sweeps. Ordinary-case performance remains broadly similar. Producing the previously discarded 80-bit subnormal results costs more than the incorrect early return; sampled Windows LDC subnormal cases improve. Fixes dlang#11123. AI-Assisted-By: Codex (GPT-6 Astra Medium)
thewilsonator
approved these changes
Oct 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Rationale
With 80-bit
real,erfc(106.625L)returns zero instead of approximately1.892705840797887e-4940, a representable subnormal value. The existing cutoff uses the normal-range limit and discards part of the subnormal output range.Replace that cutoff with a conservative zero bound derived from the floating-point format. In the extreme tail, evaluate
z = exp(-x*x/2)and form(z * erfce(x)) * z, keeping intermediate factors normal and rounding into the subnormal range only at the final multiplication. Ordinary inputs retain the original calculation.Feature request or issue tracking
Closes #11123.
Validation
real), Windows LDC 1.43.0 (64-bitreal), and Linux LDC 1.36.0 (80-bitreal).These are targeted checks, not a full Phobos test-suite run or a correctly-rounded accuracy guarantee. Reference tolerances allow the existing exponential's argument-dependent error. IEEE quadruple precision was not tested locally.
Performance
Optimized local microbenchmarks compared this commit with
6f9665c6c. Each sample made 100,000 calls through function pointers. Times below are medians of six alternating baseline/PR pairs after discarding the first pair, in nanoseconds per call.realrealrealrealrealrealrealSampled ordinary-case timings varied approximately -7% to +6%. Windows LDC subnormal cases improved by about 2x, consistent with avoiding subnormal intermediate arithmetic. The substantial slowdown at 106.625 with 80-bit
realreplaces an incorrect immediate zero with a computed subnormal result. These short local measurements are not performance guarantees.Pre-review checklist
LLM/AI disclosure
This PR was written entirely by an LLM/AI.
AI-Assisted-By: Codex (GPT-6 Astra Medium)