Thread overview
[Issue 12974] Integer constant exponentiation gives wrong value
Jun 23, 2014
safety0ff.bugz
Jun 24, 2014
safety0ff.bugz
Jun 24, 2014
safety0ff.bugz
Jun 27, 2014
safety0ff.bugz
Jun 27, 2014
safety0ff.bugz
June 23, 2014
https://issues.dlang.org/show_bug.cgi?id=12974

safety0ff.bugz <safety0ff.bugz@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|x86_64                      |All
                 OS|Linux                       |All
           Severity|enhancement                 |normal

--
June 24, 2014
https://issues.dlang.org/show_bug.cgi?id=12974

bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc

--- Comment #1 from bearophile_hugs@eml.cc ---
This seems to pass on my 32 bit system:

assert((3uL)^^40uL == 12157665459056928801uL); // error

--
June 24, 2014
https://issues.dlang.org/show_bug.cgi?id=12974

--- Comment #2 from bearophile_hugs@eml.cc ---
ulong(uint.max) ^^ 40UL is something like:

208158641954659548848625708480171133381207412524816152551724014122904182251 307912571997657650065254566688428866939275444029732004368372970852338827367 264791762120127429330987166710374655101628820795072338450535380344923607954 436493529260566657177085349603902288285849258947350283095860468263104249444 251291644696840982120081667552033239985460311393336090041066199773922562599 18212890625

--
June 24, 2014
https://issues.dlang.org/show_bug.cgi?id=12974

--- Comment #3 from bearophile_hugs@eml.cc ---
So I don't see the bug.

--
June 24, 2014
https://issues.dlang.org/show_bug.cgi?id=12974

--- Comment #4 from safety0ff.bugz <safety0ff.bugz@gmail.com> ---
(In reply to bearophile_hugs from comment #1)
> This seems to pass on my 32 bit system:
> 
> assert((3uL)^^40uL == 12157665459056928801uL); // error

A quick check on dpaste suggests that it passes in 2.065 and fails in git. I suspect it's just a const-folding change that enables that line to get CT-evaluated in git versus 2.065 because the other expression fails in both.

(In reply to bearophile_hugs from comment #2)
> ulong(uint.max) ^^ 40UL is something like:
> 
> 208158641954659548848625708480171133381207412524816152551724014122904182251 307912571997657650065254566688428866939275444029732004368372970852338827367 264791762120127429330987166710374655101628820795072338450535380344923607954 436493529260566657177085349603902288285849258947350283095860468263104249444 251291644696840982120081667552033239985460311393336090041066199773922562599 18212890625

Typo, the exponent was supposed to be 2uL.

Corrected:
//CODE: http://dpaste.dzfl.pl/ef7cd3cfd32d
import std.math;
void main()
{
    ulong uimax = uint.max;
    assert(uimax^^2uL == 18446744065119617025uL); //passes
    ulong three = 3uL;
    assert(three^^40uL == 12157665459056928801uL); // passes

    assert((cast(ulong)uint.max)^^2uL == 12157665459056928801uL); // error
    assert((3uL)^^40uL == 12157665459056928801uL); // error
}

--
June 24, 2014
https://issues.dlang.org/show_bug.cgi?id=12974

--- Comment #5 from bearophile_hugs@eml.cc ---
With the latest alpha compiler this works correctly on my Windows 32 bit:

void main() {
    import std.math;

    immutable ulong r2 = 12157665459056928801uL;
    immutable ulong three = 3uL;
    assert(three ^^ 40uL == r2); // OK
    assert(3uL ^^ 40uL == r2);   // OK
}


While this shows the problem:

void main() {
    import std.math;
    ulong X = uint.max;
    assert(X ^^ 2uL == 18446744065119617025uL);               // OK
    assert(ulong(uint.max) ^^ 2uL == 12157665459056928801uL); // Fails
}

--
June 24, 2014
https://issues.dlang.org/show_bug.cgi?id=12974

safety0ff.bugz <safety0ff.bugz@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Hardware|All                         |x86_64

--- Comment #6 from safety0ff.bugz <safety0ff.bugz@gmail.com> ---
(In reply to bearophile_hugs from comment #5)
> While this shows the problem:
> 
> void main() {
>     import std.math;
>     ulong X = uint.max;
>     assert(X ^^ 2uL == 18446744065119617025uL);               // OK
>     assert(ulong(uint.max) ^^ 2uL == 12157665459056928801uL); // Fails
> }

Bah, I screwed up the constants, that second constant is the one for 3^40.
The bug is still there on 64bit once I fixed the constants.
http://dpaste.dzfl.pl/51133cbb1d74
Marking as 64 bit only.

--
June 27, 2014
https://issues.dlang.org/show_bug.cgi?id=12974

--- Comment #7 from safety0ff.bugz <safety0ff.bugz@gmail.com> ---
Odd, I can't reproduce locally, dpaste's 2.065 passes the test but dpaste git
version doesn't.
Perhaps it's a regression that got fixed.

--
June 27, 2014
https://issues.dlang.org/show_bug.cgi?id=12974

safety0ff.bugz <safety0ff.bugz@gmail.com> changed:

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

--- Comment #8 from safety0ff.bugz <safety0ff.bugz@gmail.com> ---
Looks like a bug from dpaste using an old git version. Sorry for the noise.

--