Compute Pow and Exp beyond the range of a double [patch] - #112
Merged
Merged
Conversation
Pow and Exp computed through double whenever they left the exact integer path, so a result above double's range threw "Infinite values are not supported" and one below it silently became an exact zero. When the double result isn't a normal number, work in base-10 logarithms built from the base's significand and exponent instead, and rebuild the result with its power of ten kept as an integer. The result is rounded to the digits the logarithm can determine, and a power of ten beyond ±1e9 throws OverflowException rather than returning infinity or zero. Fixes #106 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QBrEwaLGeo2SvjVK3AnLrp
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QBrEwaLGeo2SvjVK3AnLrp
… bounds Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QBrEwaLGeo2SvjVK3AnLrp
|
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 #106
What was wrong
Outside the exact integer path,
PowandExpcomputed throughdouble:ArgumentOutOfRangeException("Infinite values are not supported"). This hit10.Pow(2000),Exp(710), and also1e400.Pow(0.5), where the base itself converts to Infinity.0. This hit2.Pow(-1100),1e-400.Pow(0.5)andExp(-1000).Fix
log10|x|comes fromBigInteger.Log10(significand) + exponent, so a base outside double's range works too.Exp,log10(e^p) = p·log10(e).10^fraction × 10^wholewith the whole power kept as anint(CreateFromComponents), so the exponent never passes through adouble.Exp(±1000)that gives 12 digits, all of them correct.OverflowExceptionnaming the operation, instead of returning infinity or zero. The Pow/Exp docs list the new exception.Tests
The new
SignificantNumberRangeTestscovers every call in the issue's table, plus:1e400.Pow(-0.5)Exp(1000)andExp(-1000)to 12 digits against high-precision reference valuesOverflowExceptionfor extreme exponents in both directionsOne correction to the issue:
1.5.Pow(2000)is 1.52e352, not ≈1.4e352, and the test expects1.5E352.With the fix reverted, all 12 new tests fail. With it, the suite passes, and the full solution builds clean for net7.0 through net10.0. This branch merges cleanly with #110 and #111.
🤖 Generated with Claude Code
https://claude.ai/code/session_01QBrEwaLGeo2SvjVK3AnLrp
Generated by Claude Code