Skip to content

Fix #11154: integer / BigInt with a negative operand - #11155

Open
G26karthik wants to merge 1 commit into
dlang:masterfrom
G26karthik:fix-int-div-bigint-sign
Open

G26karthik wants to merge 1 commit into
dlang:masterfrom
G26karthik:fix-int-div-bigint-sign

Conversation

@G26karthik

Copy link
Copy Markdown
Contributor

Rationale

integer / BigInt divided x by data.peekUlong(0), which is a ulong, so a negative x was converted to ulong first and the sign of the BigInt was ignored. For example -1000 / BigInt(10) returned -1717987019 and 1000 / BigInt(-10) returned 100. It now divides the magnitudes and applies the sign, the same way the % branch next to it does, and still truncates toward zero like the built-in /.

The unittest covers the three sign combinations with a negative operand, long.min, quotients that truncate, and the unsigned case.

One visible change: with an unsigned left operand and a negative BigInt the quotient is negative and does not fit in T, so it wraps, as int.min / BigInt(-1) already does. 10u / BigInt(-5) now gives cast(uint) -2 instead of 2.

Feature request or issue tracking

Closes #11154.

Pre-review checklist

  • I have performed a self-review of my code.
  • If my PR fixes a bug or introduces a new feature, I have added thorough tests.
  • If my changes are non-trivial and do not concern a reported issue, I have added a changelog entry.

This fixes a reported issue, so I did not add a changelog entry.

opBinaryRight!"/" divided x by data.peekUlong(0), which is a ulong, so a
negative x was converted to ulong before the division, and the sign of
the BigInt was never used. Divide the magnitudes and apply the sign, as
the % branch next to it already does.
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.

integer / BigInt gives wrong results when either operand is negative

1 participant