Repository navigation
Return an error for division by zero in DecimalArithmetic - #124
Open
JayOfTheKeyboard wants to merge 1 commit into
Open
JayOfTheKeyboard wants to merge 1 commit into
JayOfTheKeyboard wants to merge 1 commit into
Conversation
shopspring/decimal panics when Div or Mod gets a zero divisor, and Pow divides by the base for exponents of -1 or less. The panic escaped Evaluate, so 1 / 0, 1 % 0 or 0 ** (0 - 1) crashed the caller when DecimalArithmetic was enabled. Check for these cases and return an error instead.
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 #123
What
With
DecimalArithmetic(),1 / 0,1 % 0and0 ** (0 - 1)panicked inside shopspring/decimal, and the panic escapedEvaluate. They now return adivision by zeroerror.Fix
The decimal
/and%operators check for a zero divisor before callingDivandMod.**returns the same error when the base is zero and the exponent is negative, becausePowdivides by the base there.This changes one result:
0 ** (0 - 0.5)used to return 1, sincePowin decimal v1.3.1 only looks at the integer part of the exponent. It now returns the error like the other negative powers of zero.Constant expressions are folded while parsing, so
1 / 0is reported as a parsing error. With parameters,x / yis an evaluation error.Tests
TestDecimalArithmeticZeroDivisorcovers each operator with literals and with parameters, plus cases that must keep working (0 / 4,0 % 4,2 ** (0 - 1),0 ** 0). On master the test panics at the first case. With the fix,go test ./...passes.