Repository navigation
Conversation
Context.Pow computes negative integer powers as 1/x**|y| via Quo, which
since v3.1.0 keeps precision trailing zeros, so Pow(2, -1) at precision 34
returned 0.5000...0 (exponent -34, Rounded) instead of 0.5. The General
Decimal Arithmetic power operation specifies that an exact integer-power
result carries the exponent it would have under repeated multiplication,
e.g. power('2','-3') ==> '0.125'.
Reduce the quotient when the reciprocal is exact so the exact result keeps
its natural exponent and no longer reports Rounded. Inexact reciprocals are
left untouched.
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.
What
Context.Powwith an exact negative integer exponent no longer preserves the exponent that the General Decimal Arithmeticpoweroperation specifies.At precision 34,
Pow(2, -1)returns0.5000000000000000000000000000000000(exponent -34) and reportsRounded;Pow(10, -2)returns0.0100...0. apd v3.0.0 returned0.5and0.01with no flags. The GDApoweroperation says an exact integer-power result carries the exponent it would have under repeated multiplication (with the reciprocal of the base for a negative exponent), e.g.power('2','-3') ==> '0.125'(http://speleotrove.com/decimal/daops.html#refpower). Python'sdecimalreturns0.5at the same precision.The regression came in with 04d1483 ("apd: don't strip trailing zeros in Quo and Sqrt", v3.1.0):
integerPowerdivides one by the computed power withQuo, which now keepsprecisiondigits, soPowinherited a division convention thepowerspec does not share. The GDA suite ingda_test.gocompares withCmp, which ignores the exponent, sopowx101 power 2 -1 -> 0.5still passed.Reported in #161 (found via cue-lang/cue, which uses apd for arbitrary-precision numbers). This PR addresses the exact-exponent part only, not the separate precision-0 question in that issue.
Fix
In
integerPower, after computing the reciprocal for a negative exponent,Reducethe quotient when it is exact (!Inexact). An exact reciprocal of an integer power has no genuine trailing zeros, so this strips only the precision padding and restores the spec exponent; theRoundedflag is no longer raised for exact results. Inexact reciprocals (e.g.3**-1) are left untouched.Tests
Added
TestPowExactIntegerindecimal_test.goasserting the text, exponent, and condition flags for exact positive and negative integer powers plus one inexact case.Validation (go1.23.2, linux/amd64):
The full suite (including the GDA
gda_test.gopower tests) passes on the branch.