9999999999999999.0 - 9999999999999998.0

Adam D. Ruppe destructionator at gmail.com
Sun Jan 6 01:05:08 UTC 2019

On Sunday, 6 January 2019 at 00:20:40 UTC, Samir wrote:
> import std.stdio: writeln;
> void main(){
>     writeln(cast(double)9999999999999999.0-9999999999999998.0);
> }

That's because it is done at compile time, since both are 
compile-time constants. The compiler will evaluate it using the 
maximum precision available to the compiler, ignoring your 
request to cast it to double (which annoys some people who value 
predictability over precision btw). At different precisions, you 
get different results.

I suggest breaking it up into a different variable to force a 
runtime evaluation instead of using the compiler's constant 

import std.stdio: writeln;
void main(){
     double d = 9999999999999999.0;

This gives 1. Making it float instead of double, you get 
something different. With real (which btw is higher precision, 
but terrible speed), you get 1 - this is what the compiler 
happened to use at compile time.

More information about the Digitalmars-d-learn mailing list