www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7973] New: BigInt %= long/ulong gives wrong value

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7973

           Summary: BigInt %= long/ulong gives wrong value
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: smjg iname.com


--- Comment #0 from Stewart Gordon <smjg iname.com> 2012-04-23 04:23:34 PDT ---
import std.stdio, std.bigint;

void main() {
    const mod = 10_000_000_000_000_000;
    pragma(msg, typeof(mod));
    BigInt value = 2551700137;

    writefln("%d", value);
    writefln("%d", mod);
    value %= mod;
    writefln("%d", value);
}
----------
C:\Users\Stewart\Documents\Programming\D\Tests\bugs>dmd bigint_mod.d
const(long)

C:\Users\Stewart\Documents\Programming\D\Tests\bugs>bigint_mod
2551700137
10000000000000000
676780713
----------

Interestingly, if I %= a long or ulong literal or non-const variable instead, I
get
----------
d:\dmd2\windows\bin\..\..\src\phobos\std\bigint.d(163): Error: static assert 
(true && false) is false
bigint_mod.d(10):        instantiated from here: opOpAssign!("%",ulong)
----------
and looking at bigint.d I see
    static assert(!is(T==long) && !is(T==ulong));

so it appears that doing %= on a long/ulong has been deliberately disabled
pending getting it working properly, but const/immutable long/ulong has been
overlooked in doing so.

Since BigInt %= BigInt seems to work correctly, how about making it construct a
BigInt from the long/ulong and using that?  It can later be replaced with an
implementation optimised to long/ulong once that has been written and tested.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 23 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7973


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au


--- Comment #1 from Don <clugdbug yahoo.com.au> 2012-04-23 06:04:31 PDT ---
(In reply to comment #0)
 Since BigInt %= BigInt seems to work correctly, how about making it construct a
 BigInt from the long/ulong and using that?  It can later be replaced with an
 implementation optimised to long/ulong once that has been written and tested.

It's a template problem, (related to implicit conversion) not a mathematical problem. At the time I wrote the code, I could not find any way to select the correct function in all cases, if I allowed long and ulong. At least one of the bugs I was hitting was fixed during the last three releases, so it is probably possible to implement it now. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Apr 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7973



--- Comment #2 from github-bugzilla puremagic.com 2012-07-01 19:53:15 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/5da45b213eaa54696a866728fea6ec296360c10e
Fix issue 7973 BigInt %= long/ulong gives wrong value

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 01 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7973


Denis Shelomovskij <verylonglogin.reg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |verylonglogin.reg gmail.com


--- Comment #3 from Denis Shelomovskij <verylonglogin.reg gmail.com> 2012-07-02
10:37:08 MSD ---
Still fails for `immutable(long)`

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 01 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7973



--- Comment #4 from github-bugzilla puremagic.com 2012-07-14 03:55:37 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/5e7b33383a284e4b28fb27561012f6d1f618ec5c
Fix issue 7973 BigInt %= long/ulong gives wrong value

Previous fix only worked for const; this fixes immutable too.

https://github.com/D-Programming-Language/phobos/commit/52b5058d069de8f6a02a12c70a6fd495b79beff8
Merge pull request #685 from donc/bigint7993div

Fix issue 7973 BigInt %= long/ulong gives wrong value

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 14 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7973


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


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