Repository navigation
Fix #11108: preserve incomplete-gamma upper tails for small shapes - #11109
Merged
Merged
Conversation
Avoid cancellation in gammaIncompleteCompl when the lower tail is close to one. For example, Q(1e-20, 0.5) should be approximately 5.59774e-21, but Windows DMD returns approximately 2.92735e-18. Evaluate the upper tail directly for a <= 1/8 and x < 1 using expm1 and a stable evaluation of logGamma(1+a)/a. Factor out a for tiny arguments to preserve representable subnormal results. Keep the existing algorithms outside this range. Add reference cases covering tiny shapes, branch boundaries, and subnormal tails. Regression tests pass on DMD and LDC. Local benchmarks improve across the affected cases on both compilers. Fixes dlang#11108. AI-Assisted-By: Codex (GPT-6 Astra Medium)
thewilsonator
approved these changes
Oct 14, 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.
Fixes #11108.
For small positive shape parameters and x < 1,
gammaIncompleteComplcomputes Q by subtracting P from one. Since P is close to one, cancellation can overwhelm the upper tail. For example,gammaIncompleteCompl(1e-20L, 0.5L)returns approximately 2.92735e-18 on Windows DMD instead of 5.597735947761608e-21.Evaluate Q directly for a <= 1/8 and x < 1 using the series in DLMF 8.7.3. Use
expm1and evaluate logGamma(1+a)/a without forming 1+a. For tiny arguments, factor out a to preserve representable subnormal results. Other parameter ranges retain their existing algorithms.Validation
std.internal.math.gammafunctionandstd.mathspecialunit tests pass, with and without optimization.-O2 --noasm. The complete module suite still encounters an existingigammaTemmeLargeassertion (original line 295), also observed on the baseline.realwith 64 mantissa bits under DMD and 53 under LDC; 113-bitrealwas not tested.Performance
Compared with original commit
1877367354fadf001365292d74e39ffd15c21bbc, in ns/call (original → patched):Measured on Windows x64, Intel Core Ultra 7 258V, using DMD
-O -inlineand LDC-O2 --noasm. Both implementations run in one process pinned to one logical processor, with 512 input pairs repeated 100 times per sample. Values are medians of ten samples after one warm-up, alternating execution order.The direct series avoids the general
logGammaevaluation. The tiny-shape shortcut also avoids rational-approximation arithmetic on subnormal operands. All affected benchmark cases improved.These are local microbenchmarks, and the original results in the affected range are inaccurate. The small differences in unchanged paths, including the 1.5% LDC slowdown for x >= 1, do not establish a systematic performance change.
This PR was written entirely by an LLM/AI.
AI-Assisted-By: Codex (GPT-6 Astra Medium)