Repository navigation
Fix #11133: preserve incomplete-beta accuracy near one - #11141
Merged
thewilsonator merged 1 commit intoOct 22, 2026
Merged
Conversation
betaIncomplete can lose substantial relative accuracy near x=1 when the second shape is tiny. For a=1, b=1e-20, and the largest double below one, the result has approximately 64% relative error. Reuse the stable complementary series and coordinate handling already used by betaIncompleteComplSmallX. Route both entry points through a shared implementation with a runtime nearOne parameter, avoiding separate template instantiations while retaining the ordinary path for x <= 0.95. Cover the exact shape-one identity through the last representable real below one, plus independent high-precision references for fractional and larger first shapes. 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
betaIncompletecan lose substantial relative accuracy nearx = 1when the second shape is tiny. Fora = 1,b = 1e-20, and the largest double below one, the expected result is approximately3.673680056967710e-19, but the existing implementation returns approximately1.314235314789e-19: about 64% relative error, although the absolute error is small.Reuse the stable complementary series and coordinate handling already used by
betaIncompleteComplSmallX. Both entry points now use a shared implementation with a runtimenearOneparameter. The ordinary path remains in use forx <= 0.95.The regression tests cover the exact shape-one identity through the last representable
realbelow one, together with independent high-precision references for fractional and larger first shapes.Feature request or issue tracking
Closes #11133.
Performance
Compared three implementations: upstream master, an initial fix retaining separate template instantiations, and the final shared implementation. Measurements used Windows DMD 2.113.0 (
-O -inline -release) and LDC 1.43.0 (-O3 -enable-inlining -release), without fast-math.Each dataset contains 128 varied inputs. The benchmark runs all three implementations in the same process through equivalent function pointers, rotates their measurement order, and pins execution to one logical CPU. Results below are median nanoseconds per call across three process runs, with nine retained batches per run after warmup.
Both fixes avoid the expensive, inaccurate continued-fraction calculation in the affected cases. The shared implementation performs similarly to the initial fix: median paired timing changes across datasets range from −2.7% to +0.6% with LDC and −0.6% to +2.2% with DMD. These are local microbenchmarks, and small differences vary between runs.
Sharing the implementation also reduces generated code size. Summed code-section sizes for the optimized module object, excluding data and metadata, are:
Validation
std.internal.math.gammafunctionmodule tests pass with DMD.1e-11or one result ULP for rounded subnormal results.The full Phobos suite was not run. Full-module LDC testing has a previously reproduced baseline assertion failure in
igammaTemmeLarge; LDC validation here used focused tests. A platform with 113-bitrealwas not available.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)