[Issue 22771] New: BigInt divMod can return "-0" (negative zero)

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Feb 14 10:07:41 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=22771

          Issue ID: 22771
           Summary: BigInt divMod can return "-0" (negative zero)
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: keivan.shah at silverleafcaps.com

Overview:

In certain cases, when using divMod from std.bigint, it can return remainder
value as "-0" which is incorrect and can causes some logical errors when the
remainder is compared with 0.
The issue happens in case of negative numbers and seems to be a logical error
in the divMod function:
https://github.com/dlang/phobos/blob/2629671c1556a81c4e7eef26e16b331c960e2b6f/std/bigint.d#L2247.
Here the sign is directly copied from the dividend without considering that the
remainder itself can be zero. Comparing the returned remainder with 0 leads to
unexpected logical errors.
The bug is a bit similar to the one resolved here: 
https://issues.dlang.org/show_bug.cgi?id=14124

Steps to Reproduce:

It is present in the currently latest version on D and can be reproduced online
currently (https://run.dlang.io/is/YsaAq8) 

Minimal reproduction code:

void main()
{
    import std.bigint;

    BigInt dividend = "-50";
    BigInt divisor = "1";
    BigInt quotient, remainder;
    divMod(dividend, divisor, quotient, remainder);
    assert(remainder == 0); // This is false, remainder is "-0"
}

--


More information about the Digitalmars-d-bugs mailing list