Bug 1681 – cast(real) ulong.max == 0

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2007-11-21T13:16:00Z
Last change time
2014-02-24T15:33:41Z
Keywords
wrong-code
Assigned to
bugzilla
Creator
andrei

Comments

Comment #0 by andrei — 2007-11-21T13:16:34Z
The title says it all. Here's a test: import std.stdio; import std.conv; void main() { real r = ulong.max; ulong d = cast(ulong) r; writeln(d); assert(d == ulong.max); // should not fail }
Comment #1 by xinok — 2007-11-21T13:31:14Z
Real is a floating point type, typically 80 bits. Ulong is a 64-bit integer type. Real is not capable of holding a value as large as ulong.max. That is why the assert fails. [email protected] wrote: > http://d.puremagic.com/issues/show_bug.cgi?id=1681 > > Summary: cast(real) ulong.max == 0 > Product: D > Version: 2.007 > Platform: PC > OS/Version: Linux > Status: NEW > Severity: normal > Priority: P2 > Component: DMD > AssignedTo: [email protected] > ReportedBy: [email protected] > > > The title says it all. Here's a test: > > import std.stdio; > import std.conv; > > void main() > { > real r = ulong.max; > ulong d = cast(ulong) r; > writeln(d); > assert(d == ulong.max); // should not fail > } > >
Comment #2 by andrei — 2007-11-21T14:23:52Z
(In reply to comment #1) > Real is a floating point type, typically 80 bits. Ulong is a 64-bit > integer type. Real is not capable of holding a value as large as > ulong.max. That is why the assert fails. Float must store integers with proper successors up to 2 ** 24 - 1. Double must store integers with proper successors up to 2 ** 53 - 1. Real (i.e. "double-extended") must store integers with proper successors up to at least 2 ** 64 - 1. The program prints 0.
Comment #3 by wbaxter — 2007-11-21T14:44:41Z
(In reply to comment #2) > (In reply to comment #1) > > Real is a floating point type, typically 80 bits. Ulong is a 64-bit > > integer type. Real is not capable of holding a value as large as > > ulong.max. That is why the assert fails. > > Float must store integers with proper successors up to 2 ** 24 - 1. Double must > store integers with proper successors up to 2 ** 53 - 1. Real (i.e. > "double-extended") must store integers with proper successors up to at least 2 > ** 64 - 1. The program prints 0. > This is also interesting: writefln("ulong.max %-30d", ulong.max); writefln("real ulong.max %-30f", cast(real)ulong.max); --> ulong.max 18446744073709551615 real ulong.max 18446744073709551613.000000
Comment #4 by witold.baryluk+d — 2008-01-20T09:13:33Z
writefln("ulong.max %-30d", ulong.max); writefln("real ulong.max %-30f", cast(real)ulong.max); gdc (GCC) 4.1.3 20080114 (prerelease gdc 0.25 20071124, using dmd 1.022) (Debian 0.25-4.1.2-19) ulong.max 18446744073709551615 real ulong.max 18446744073709551615.000000 dmd 1.025: ulong.max 18446744073709551615 real ulong.max 18446744073709551615.000000 This can depend on processor (some processors aren't strictly following IEEE 754, especially in extented precission). Also real isn't nacassary 80bit, real can be double on some architectures and processors. As far as I known Intel processors do real extended precission operations (in fact they use internally 128 bit precission). Tested machines: Debian GNU/Linux etch, Pentium III (Coppermine) 733MHz Debian GNU/Linux unstable, Intel(R) Pentium(R) 4 CPU 2.80GHz Debian GNU/Linux etch, AMD Duron 1200MHz (Unknown model) Unfortunetly example from first post gives 0 in dmd (gdc works), so it is a bug. The largest integer real which gives nonzero result after cast is 18446744073709550591.000000 18446744073709549568 but as you can see it gives wrong result. Then it is skiping every 2048 (11 bits), with some values correct: 18446744073709549568.000000 18446744073709549568 but most not. Largest continues interval with correct answers: 0.0 ... 8744073709552616.000000 (about 2**53, so doubles are involved not real!) (similar in decimal expansion 18446744073709551615.000000, but probably coincidence)
Comment #5 by witold.baryluk+d — 2008-01-20T09:15:10Z
> > The largest integer real which gives nonzero result after cast is > 18446744073709550591.000000 > 18446744073709549568 > but as you can see it gives wrong result. Then it is skiping every 2048 (11 > bits), with some values correct: 53+11 = 64
Comment #6 by clugdbug — 2009-02-19T07:12:31Z
Also applies in D1.0
Comment #7 by bugzilla — 2009-03-11T14:52:27Z
Fixed dmd 1.041 and 2.026