Repository navigation
Stop ExpM1, LogP1 and the hyperbolics building million-digit sums for large arguments [patch] - #168
Merged
Conversation
… large arguments [patch] Exact addition aligns both operands to the smaller exponent, so subtracting one from e^-1e7, or adding one to 1e1000000, built a significand millions of digits wide only for the caller to round it away. ExpM1(-1e10) overflowed the exponent of e^-1e10 before it got that far. AddToPrecision and SubtractToPrecision collapse an operand that lies wholly below the larger one's last digit and the caller's rounding position into a single sticky unit, which rounds the same way as the exact sum. ExpM1, LogP1, Sinh, Cosh, Asinh and Acosh use them, and ExpM1 returns -1 outright once e^x is below the working width. Fixes #133 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TTeaDNAaTEviJbNWuEi5hP
|
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 #133
What was wrong
AddandSubtractare exact, so they align both operands to the smaller exponent. When one operand is millions of digits below the other, that builds a significand millions of digits wide, and the caller then rounds almost all of it away:ExpM1(-1e7)took about 10 s to return-1.ExpM1(-1e10)threwOverflowException, becauseExp(-1e10)overflows its own exponent.LogP1,Sinh,Cosh,AsinhandAcoshtook tens of seconds on arguments like1e7and1e1000000.Change
This is the general fix the triage comment suggested.
New helpers.
AddToPrecisionandSubtractToPrecision(internal, inPreciseNumber.cs) check whether the smaller operand lies wholly below both the larger operand's last digit and the caller's rounding position (with 3 guard digits). If it does, the smaller operand is replaced by one unit of its sign at that position. The exact sum and this sticky sum fall strictly inside the same gap between neighbouring multiples of that digit. Every rounding boundary at the caller's precision is a multiple of it, including after the halving inSinh/Cosh, so both sums round the same way.Where they're used.
ExpM1: the- 1.LogP1: the1 +.Sinh/Cosh: the± 1/e^x.Asinh:1 + x²and1 + √….Acosh:x ∓ 1.Near one these sums are still exact, because the helper only takes over when the gap exceeds the working width. The cancellation-sensitive small-argument paths described in CLAUDE.md are unchanged.
Early return in
ExpM1. It returns-1oncex < -3·(working + 1). Since ln 10 < 3,e^xthere is below every kept digit. This coversExp2M1andExp10M1too, because they route throughExpM1.Docs. CLAUDE.md now describes the helpers and the new test file.
Tests
Added
PreciseNumberLargeArgumentTests:ExpM1,Exp2M1andExp10M1of-1e10return-1.ExpM1(±…),Exp10M1(-1e7),LogP1(1e1000000),Sinh(±1e7),Cosh(-1e7),Asinh(±1e1000000)andAcosh(1e1000000)each finish within a 5 s budget. They run on the thread pool so the test fails fast instead of hanging. The test also checks they equalExp(x),Log(x),e^x/2andln(2x)respectively.ExpM1(-100),ExpM1(300),Exp10M1(100.5),LogP1(3.7e300),Sinh(300),Cosh(-300.5),Asinh(-2.5e300),Acosh(7.25e300)andAcosh(1e40)are pinned. These values were captured from the exact path before this change, and the bounded path reproduces every one.How the tests were checked:
OverflowException; the other four overrun the 5 s budget. The digit-pinning test passes on both versions, which is what it exists to show.🤖 Generated with Claude Code
https://claude.ai/code/session_01TTeaDNAaTEviJbNWuEi5hP
Generated by Claude Code