wrong rounding
John Colvin via Digitalmars-d
digitalmars-d at puremagic.com
Mon Jun 1 15:24:29 PDT 2015
On Monday, 1 June 2015 at 21:44:57 UTC, Steven Schveighoffer
wrote:
> 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
Nonetheless, surely in this case the values should be the same
regardless of the integer target type, no?
More information about the Digitalmars-d
mailing list