Bug 12974 – Integer constant exponentiation gives wrong value

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2014-06-23T23:05:00Z
Last change time
2014-06-27T04:57:17Z
Assigned to
nobody
Creator
safety0ff.bugz

Comments

Comment #0 by safety0ff.bugz — 2014-06-23T23:05:28Z
Applying the unit tests from my own integer power function: 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)^^40uL == 12157665459056928801uL); // error assert((3uL)^^40uL == 12157665459056928801uL); // error }
Comment #1 by bearophile_hugs — 2014-06-24T08:30:12Z
This seems to pass on my 32 bit system: assert((3uL)^^40uL == 12157665459056928801uL); // error
Comment #2 by bearophile_hugs — 2014-06-24T08:34:30Z
ulong(uint.max) ^^ 40UL is something like: 208158641954659548848625708480171133381207412524816152551724014122904182251 307912571997657650065254566688428866939275444029732004368372970852338827367 264791762120127429330987166710374655101628820795072338450535380344923607954 436493529260566657177085349603902288285849258947350283095860468263104249444 251291644696840982120081667552033239985460311393336090041066199773922562599 18212890625
Comment #3 by bearophile_hugs — 2014-06-24T08:35:19Z
So I don't see the bug.
Comment #4 by safety0ff.bugz — 2014-06-24T08:41:58Z
(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 }
Comment #5 by bearophile_hugs — 2014-06-24T10:30:37Z
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 }
Comment #6 by safety0ff.bugz — 2014-06-24T10:37:07Z
(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.
Comment #7 by safety0ff.bugz — 2014-06-27T03:56:58Z
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.
Comment #8 by safety0ff.bugz — 2014-06-27T04:57:17Z
Looks like a bug from dpaste using an old git version. Sorry for the noise.