Strange calculation problem

Lars T. Kyllingstad public at kyllingen.NOSPAMnet
Tue Aug 25 02:49:46 PDT 2009


JPF wrote:
> I've hit a strange problem somewhere in my code and I narowed it down to
> the following testcase:
> ----------------------
> module test;
> import tango.io.Stdout;
> 
> const ulong SIZE_IN_B = (1024 * 1024 * 1024 * 2); /*should be 2147483648*/
> void main()
> {
> 	Stdout.formatln("{0}", SIZE_IN_B); /*but is 18446744071562067968*/
> }
> ----------------------
> 
> It happens with or without the parenthesis and without the const as well
> . Am I doing something wrong, or did I just hit a bug?

I can reproduce this also with D2:

   enum ulong SIZE_IN_B = (1024 * 1024 * 1024 * 2);
   void main() { writeln(SIZE_IN_B); }

I think what happens is that since integer literals (unless they have a 
type-specific suffix) have the int type. An int will overflow on that 
calculation, and give nonsensical results when it is finally converted 
to ulong.

I think the compiler should be smart enough to figure this out for 
itself, but until that happens you can work around it by suffixing at 
least one of the integer literals with LU, so the compiler interprets 
the entire expression as an ulong:

   const ulong SIZE_IN_B = (1024LU * 1024 * 1024 * 2);

-Lars


More information about the Digitalmars-d-learn mailing list