[Issue 8011] BigInt ++ and -- do wrong thing on negative numbers

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue May 1 11:36:26 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=8011


c.m.brandenburg at gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |2.059


--- Comment #1 from c.m.brandenburg at gmail.com 2012-05-01 11:37:37 PDT ---
The root cause is in bigint.d, in the opUnary() function. BigInt uses an
underlying BigUint and maps the ++ and -- operators directly through to the
BigUint as addition and subtraction, respectively, disregarding the BigInt's
sign. However, this is wrong. Signed ++ and -- must be passed through as
subtraction and addition, respectively.

I modified the function by commenting out the two errant lines and replacing
them each with a correct one. This is a proof-of-concept fix for the bug. 

    // non-const unary operations
    BigInt opUnary(string op)() if (op=="++" || op=="--")
    {
        static if (op=="++")
        {
            //data = BigUint.addOrSubInt(data, 1UL, false, sign);
            data = BigUint.addOrSubInt(data, 1UL, sign ? true : false, sign);
            return this;
        }
        else static if (op=="--")
        {
            //data = BigUint.addOrSubInt(data, 1UL, true, sign);
            data = BigUint.addOrSubInt(data, 1UL, sign ? false : true, sign);
            return this;
        }
    }

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list