Casting double to ulong weirdness

bachmeier via Digitalmars-d digitalmars-d at puremagic.com
Tue Aug 25 07:21:52 PDT 2015


On Tuesday, 25 August 2015 at 13:51:18 UTC, Márcio Martins wrote:

> import std.stdio;
> void main() {
> 	double x = 1.2;
> 	writeln(cast(ulong)(x * 10.0 + x.epsilon));
>
> 	double y = x * 10.0;
> 	writeln(cast(ulong)(y + x.epsilon));
> 	
> 	double z = x * 10.0 + x.epsilon;
> 	writeln(cast(ulong)(z));
> }
>
> Outputs:
> 11
> 12
> 12
>
> I leave it at this. It seems like this only bothers me, and I 
> have no more time to argue.
> The workaround is not that bad, and at the end of the day, it 
> is just one more thing on the list.

What you are attempting to do is impossible. Is there a reason 
you can't use std.math.round, which is the tool that was made for 
the task?


More information about the Digitalmars-d mailing list