Skip to content

Restore quotient and remainder scales in QuoRem - #201

Open
ac1982 wants to merge 1 commit into
ericlagergren:masterfrom
ac1982:fix/quorem-scale
Open

ac1982 wants to merge 1 commit into
ericlagergren:masterfrom
ac1982:fix/quorem-scale

Conversation

@ac1982

@ac1982 ac1982 commented Oct 20, 2026

Copy link
Copy Markdown

QuoRem(1.2, 1) currently returns (1, 2) instead of (1, 0.2). The coefficient division aligns both operands but does not restore the remainder's exponent. The multiprecision path can also retain a previously used quotient receiver's exponent.

Restore the integer quotient exponent and the common operand exponent for the remainder in Context.QuoRem, matching the responsibilities already handled by QuoInt and Rem. Capture the common exponent before division so input/output aliasing is safe, and use it for zero dividends as well.

Fixes #187.

Tests compare both public entry points with exact math/big rational results across compact and multiprecision operands, signs, zero, reused receivers, and input/output aliases. The regression fails before the fix.

Validation (Go 1.27.1/Linux arm64):

  • go test -short -count=1 -skip 'TestDecTests|TestPytables|TestIssue69' ./...
  • go test -race -run 'Quo|Rem|TestIssue72' -count=1 .
  • go vet ./...

All above pass. Generated decimal/Python test corpora are absent locally. The exhaustive float/square-root tests were not completed; an initial full race run was stopped under the local resource budget, then the short and focused checks above were run.

@AmanBoronbaev AmanBoronbaev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried the zero-dividend case with a negative divisor on 0146c35. With x = 0.00 and y = -1, fresh outputs give q = -0 and r = 0.00. Reusing x as the quotient gives q = -0 and r = -0.00; r.Signbit() changes solely because the destination aliases x.

In the zero branch, z.setZero writes the quotient sign into x when z == x. The subsequent r.setZero then reads that updated x.form. Could you save the dividend sign before writing either output, as you do for the exponent, and include this combination in the alias tests? The rational comparison in TestQuoRemScale considers both signed zeros equal, so an explicit Signbit assertion would catch it. The sign issue also applies to the previous zero branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

QuoRem returns remainder with incorrect scale

2 participants