BigInt divMod is wrong for negative first argument
NonNull
non-null at use.startmail.com
Sun Aug 20 14:56:51 UTC 2023
On Saturday, 19 August 2023 at 06:14:48 UTC, Timon Gehr wrote:
> So I guess your suggestion is:
>
> divMod(8, 5, div, mod) -> div=1, mod=3
> divMod(-8, 5, div, mod) -> div=-2, mod=2
> divMod(8, -5, div, mod) -> div=-1, mod=3
> divMod(-8, -5, div, mod) -> div=2, mod=2
>
> And then divMod and divRem would have different "div" behavior
> I guess.
Yes.
And `div` is defined so that when `b` is divided by `m`, `b` can
be reconstructed from its quotient `q` and remainder `r`, i.e. `b
= qm + r` should work with `r = b mod m` with the correct
definition of `mod`, and `q = b div m`.
So `b div m = (b - (b mod m))/m` which is well defined since `b -
(b mod m)` is exactly divisible by m.
If we write `b rem m` as the mistakenly defined remainder, then
by the same reasoning it will have its own, different `div`
behavior, `b div m = (b - (b rem m)/m`.
More information about the Digitalmars-d
mailing list