BigInt divMod is wrong for negative first argument

NonNull non-null at use.startmail.com
Fri Aug 18 16:02:23 UTC 2023


On Friday, 18 August 2023 at 12:19:04 UTC, Timon Gehr wrote:
> On 8/18/23 13:44, NonNull wrote:
>> If % was actually mod these would work.
> I think it would also be confusing if BigInt operations were 
> incompatible with operations on `int` when operating on values 
> within the range of `int`.

At least have divMod do the right thing, and rename the old 
divMod to divRem. And document properly.

>> There is also good reason to take the value of (a mod m) to be 
>> independent of the sign of m so that it is the canonical 
>> representative of the congruence class of a modulo m, which 
>> doesn't change when the sign of m is changed because a is 
>> congruent to b modulo m iff a is congruent to b modulo (-m).
>> ...
>
> This I think is more debatable, as congruence classes of 
> different moduli need not be compatible.

Congruence classes modulo m and modulo -m are exactly the same, 
so making their canonical representatives (a mod m) and (a mod 
-m) be the same makes sense.



More information about the Digitalmars-d mailing list