Strange that D is printing large values as zero. Any mistake in my code?

monarch_dodra monarchdodra at gmail.com
Thu Feb 14 07:55:43 PST 2013


On Thursday, 14 February 2013 at 15:44:25 UTC, Sparsh Mittal 
wrote:
> Here is the program:
>
>
> import std.stdio;
>
> const long DIM = 1024*1024*1024*1024*4;
> void main()
> {
> writeln(" DIM  is ", DIM);
> writeln(" Value ", 1024*1024*1024*1024*4);
> writeln(" Max ", long.max);
>
> }
>
> I compiled it:
> gdc -frelease -O3 temp.d -o t1 ; ./t1
>  DIM  is 0
>  Value 0
>  Max 9223372036854775807
>
> Can you please tell, why it is taking DIM as zero? If I reduce 
> DIM, it works fine. It is strange.

It's not because "DIM" is a long, that "1024*1024*1024*1024*4" is 
a long. Case in point, it is calculated as an int, and becomes 0.

Try it with:
"1UL * 1024*1024*1024*1024*4"
or
"1024UL*1024*1024*1024*4"
or
"cast(ulong)1024*1024*1024*1024*4"
To force the actual calculation into long mode, and you'll get 
the desired result.

Or, just "2UL^^42", depending on just what you want "DIM" to 
represent.


More information about the Digitalmars-d-learn mailing list