fix negative zero from multiplication/division by zero bigint#143
Merged
michae2 merged 1 commit intocockroachdb:masterfrom Mar 17, 2026
Merged
fix negative zero from multiplication/division by zero bigint#143michae2 merged 1 commit intocockroachdb:masterfrom
michae2 merged 1 commit intocockroachdb:masterfrom
Conversation
michae2
reviewed
Dec 11, 2025
Contributor
michae2
left a comment
There was a problem hiding this comment.
Thanks for the fix! Would you be willing to add a short test to bigint_test.go?
bigint.go
Outdated
| @@ -326,6 +326,9 @@ func addInline(xVal, yVal uint64, xNeg, yNeg bool) (zVal uint64, zNeg, ok bool) | |||
| func mulInline(xVal, yVal uint64, xNeg, yNeg bool) (zVal uint64, zNeg, ok bool) { | |||
| hi, lo := bits.Mul64(xVal, yVal) | |||
| neg := xNeg != yNeg | |||
Contributor
There was a problem hiding this comment.
nit: This could be shortened to neg := xNeg != yNeg && lo != 0 (and same with a couple others).
Contributor
Author
|
Is that better? |
michae2
approved these changes
Dec 22, 2025
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.
(-1) * 0 was producing a "negative zero" where Sign() returns -1 but String() returns "0". This breaks Cmp() comparisons.
addInline already handles this correctly - just needed the same fix in mulInline, quoInline, and remInline.
Same issue with division and remainder when the result is zero.
Added the same zero check to all three functions. Existing tests pass.