cast(int) getting an unexpected number

Ali Cehreli acehreli at yahoo.com
Wed Nov 4 11:25:07 PST 2009


rmcguire Wrote:

> why is this not a compiler bug?
> because:
> import std.stdio;
> 
> void main() {
>         float f=0.01;

> results in:
> 0.01->0

0.01 is double, there is type conversion there.

>         writefln("%0.2f->%d",f,cast(int)(f*100f));

> results in:
> 0.01->1

f*100f is represented to be less than 0.

>         writefln("%0.2f->%d",f,cast(int)(.01*100f));

.01 * 100f is double and apparently represented as something more than 1.

>         writefln("%0.2f->%f",f,(f*100f));

> results in:
> 0.01->1.000000

Someting that is less than 0 is being "printed" as 1 at that precision. Printing with a higher precision should print something less than 0.

Ali



More information about the Digitalmars-d-learn mailing list