Repository navigation
Fix #11140: preserve subnormal inputs in expm1 - #11142
Merged
Merged
Conversation
The portable double-precision approximation effectively halves tiny inputs before doubling the rounded intermediate. This can lose one smallest-subnormal unit or turn the smallest nonzero inputs into signed zero. Return x directly when its magnitude is below epsilon / 4, where the correction to x rounds away under round-to-nearest. Extend the existing zero shortcut while preserving signed zero and leaving the wider-format algorithms unchanged. Cover signed subnormal inputs, the normal/subnormal boundary, and values immediately around the shortcut cutoff. AI-Assisted-By: Codex (GPT-6 Astra Medium)
thewilsonator
approved these changes
Oct 21, 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
expm1(double)can lose one smallest-subnormal unit—a tiny absolute error that can represent a substantial relative error for subnormal inputs. For example, an input of −5 minimum-subnormal units produces −4 units instead of −5. The smallest positive and negative inputs produce signed zero.The portable double-precision approximation effectively halves these inputs before doubling the rounded intermediate. That intermediate rounding discards information that the final multiplication cannot recover.
Extend the existing zero shortcut to return
xwhen|x| < epsilon / 4, where the correction inexpm1(x) = x + x²/2 + …rounds away under round-to-nearest. This preserves signed zero and avoids the problematic intermediate arithmetic. The wider-format algorithms remain unchanged.Feature request or issue tracking
Closes #11140.
Validation
-preview=dip1000. LDC testing disables assembly to exercise the portable implementation.Module testing used the source under a temporary module name with installed runtime libraries. The full Phobos suite was not run. The shortcut is justified for round-to-nearest; this change does not establish correctly rounded behavior under directed rounding modes.
Performance
A local paired benchmark uses 128 varied inputs per dataset, alternates measurement order, and reports median nanoseconds per call over six batches after warmup. DMD uses
-O -inline -release; LDC uses-O3 -release --noasm.Tiny-input calls benefit from bypassing the approximation, while ordinary-input timings remain broadly similar. These are local microbenchmark results; small differences should not be interpreted as general performance guarantees.
Pre-review checklist
The changelog condition does not apply because this fixes a reported issue.
LLM/AI disclosure
This PR was written with the assistance of an LLM/AI.
AI-Assisted-By: Codex (GPT-6 Astra Medium)