www.digitalmars.com         C & C++   DMDScript  

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


          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 puremagic.com
          Reporter: keivan.shah silverleafcaps.com


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:
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: 

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"

Feb 14 2022