Keep the base's precision when Pow or Exp takes an integer exponent - #104
Merged
Merged
Conversation
…patch] An integer exponent carried its own single significant digit into the result's precision, so 1.234.Pow(2) returned 2 and Exp(2) returned 7. An integer power is now treated as an exact count, like a counting number in a multiplication: - Pow computes integer powers up to 1024 by exact repeated squaring (a reciprocal for a negative power) and keeps the base's significant digits, so x.Pow(2) equals x * x. Larger integer powers keep the base's digits, capped at what the double they are computed in can hold. - Exp with an integer exponent keeps up to 15 significant digits, the precision of the double it is computed in. Fixes #103 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NvZoJBSka1emZMaEXy4Cfj
|
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 #103
What was wrong
PowandExpset their precision withLowestSignificantDigits(base, power). An integer exponent like2has 1 significant digit, so the result was rounded to 1 digit:1.234.Pow(2)21.523(equals1.234 * 1.234)1.234.Pow(3)21.879(equals1.234 * 1.234 * 1.234)(-2.5).Pow(3)-20-16(−15.625 to 2 digits)1.234.Pow(-2)0.70.6567Exp(2)77.38905609893065Change
Powwith an integer exponent up to 1024: the exponent is treated as an exact count, like a counting number in a multiplication.PreciseNumber. A negative power takes the reciprocal.x.Pow(2) == x * x. This path also keeps the sign of a negative base, as the triage suggested.Powwith a larger integer exponent: it still uses the log/exp path so that it can't build a huge intermediate. It keeps the base's digits, capped at 15 because the value is computed in adouble.Expwith an integer exponent: keeps 15 significant digits (Eis capped to thedoubleit is computed in).The commit carries
[patch], matching #102. Results only gain precision; nothing that returned a value now throws.Tests
New tests in
SignificantNumberTests:Pow_IntegerExponent_MatchesRepeatedMultiplicationchecksx.Pow(2) == x * xandx.Pow(3) == x * x * xforx = 1.234, including the exact values and the digit count.Pow_NegativeBaseAndOddExponent_KeepsBasePrecisionchecks(-2.5)^3→-16.Pow_NegativeIntegerExponent_MatchesReciprocalOfPowerchecks1.234^-2→0.6567.Exp_IntegerExponent_KeepsMoreThanOneSignificantDigitchecks thatExp(2)has more than 1 digit and matchesMath.Exp(2).Verification
main, all 4 new tests fail (2,-20,0.7and 1 digit).3^2,2^3and(-2)^3cases included.🤖 Generated with Claude Code
https://claude.ai/code/session_01NvZoJBSka1emZMaEXy4Cfj
Generated by Claude Code