Keep the exponential CDF and quantile precise near zero - #58
Merged
Merged
Conversation
Cdf used 1 - exp(-rx) and Quantile -log(1 - p), which keep only the digits of a number near one: below about 1e-16 both rounded to zero and the quantile to negative zero. Add Kahan-corrected ExpM1 and Log1P to SpecialFunctions and use them. Fixes #52 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPis8BNX5ETFM2XytgJCz7
Below epsilon both return the argument, which is exact to within an ulp and keeps the Kahan correction off a 0/0; the underflow and pole checks become ordered comparisons. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPis8BNX5ETFM2XytgJCz7
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPis8BNX5ETFM2XytgJCz7
|
This was referenced Sep 28, 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 #52
What changed
ExponentialDistributionProvider.Cdfcomputed1 - exp(-λx)andQuantilecomputed-log(1 - p) / λ. Both subtract from a number near one, so they keep only the digits that survive that rounding. The relative error is about ε/x. Below about 1e-16 the result was0, andQuantilereturned-0, which is outside the support.SpecialFunctions.ExpM1andSpecialFunctions.Log1P. Both use Kahan's correction, which divides by a quantity carrying the same rounding error so the error cancels, and are accurate to a few ulps. They are written out becausedouble.ExpM1/LogP1are unavailable on netstandard2.1 and are the naive subtraction on .NET.Cdfis now-ExpM1(-λx)andQuantileis now-Log1P(-p) / λ.Quantile(0)now returns+0.SpecialFunctions.csis linked into the Exponential project, as it already is into Normal, LogNormal, Binomial and Poisson. I updated CLAUDE.md to match.The triage on #52 says #51 (Geometric with tiny p) needs the same helpers. Once this lands, that fix can reuse them.
Tests
Exponential_Keeps_Its_Precision_Near_Zero. It covers the repro values from the issue at 1e-15 relative tolerance, checks that the quantile is never negative zero, and checks that ordinary and far-tail values are unchanged.Cdf(1e-17)returned 0). With the fix it passes.Essentials.Testssuite passes: 917/917. The Exponential project builds cleanly in Release for all six target frameworks.🤖 Generated with Claude Code
https://claude.ai/code/session_01VPis8BNX5ETFM2XytgJCz7
Generated by Claude Code