cast(int) getting an unexpected number

Michal Minich michal at minich.sk
Wed Nov 4 08:22:57 PST 2009


Hello rmcguire,

> why is this not a compiler bug?
> because:
> import std.stdio;
> void main() {
> float f=0.01;
> writefln("%0.2f->%d",f,cast(int)(f*100f));
> writefln("%0.2f->%d",f,cast(int)(.01*100f));
> writefln("%0.2f->%f",f,(f*100f));
> }
> results in:
> 0.01->0
> 0.01->1
> 0.01->1.000000
> I would say something is dodgy.
> 
> -Rory
> 

I think this may be case of:
At comple time floating point computations may be done at a higher precision 
than run time.

http://www.digitalmars.com/d/2.0/function.html#interpretation

It seems that .01*100f is computed at compile time.
try declarting float f as const or invariant and see what happens (I'm not 
sure..)




More information about the Digitalmars-d-learn mailing list