wrong rounding

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Mon Jun 1 14:44:57 PDT 2015


On 6/1/15 5:29 PM, Oleg B wrote:
> Hello. I found unexpected (for me) behavior of rounding double values at
> casting to ulong:
>
> $ cat roundtest.d
> import std.stdio;
>
> void main()
> {
>      double a = 10, b = 0.01;
>
>      writeln( "int:   ", cast(int)(a/b) );
>      writeln( "uint:  ", cast(uint)(a/b) );
>      writeln( "long:  ", cast(long)(a/b) );
>      writeln( "ulong: ", cast(ulong)(a/b) );
> }
>
> $ rdmd roundtest.d
> int:   1000
> uint:  1000
> long:  1000
> ulong: 999   <----- WTF?? -------

These are NOT roundings. They are truncations.

Note that for floating point 0.01 is not representable exactly. This is 
the reason you get the error. On other systems, you may not get errors 
for this one case, but you could get errors for other. Please read about 
floating point error: 
http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

-Steve


More information about the Digitalmars-d mailing list