[OT] The Usual Arithmetic Confusions

Timon Gehr timon.gehr at gmx.ch
Sun Feb 6 03:43:42 UTC 2022


On 06.02.22 00:16, Walter Bright wrote:
> https://issues.dlang.org/show_bug.cgi?id=22740

Thanks! One place where this has now actually bit me is this calculation 
(DMD on linux):

```d
void main(){
     import std.stdio;
     assert(42*6==252);
     // constant folding, uses extended precision, overall less accurate 
result due to double rounding:
     assert(cast(int)(4.2*60)==251);
     // no constant folding, uses double precision, overall more 
accurate result
     double x=4.2;
     assert(cast(int)(x*60)==252);
}
```

4.2 and 60 were named constants and the program would have worked fine 
with a result of either 251 or 252, I did not rely on the result being a 
specific one of those. However, because the result was sometimes 251 and 
at other times 252, this resulted in a hard to track down bug caused by 
the inconsistency. I even got one result on Windows and the other one on 
linux when compiling _exactly the same expression_. This was with LDC 
though, not sure if the platform dependency is reproducible with DMD.

Note that this was relatively recently, but I had seen this coming for a 
long time before it actually happened to me, which is why I had 
consistently argued so vehemently against this kind of precision 
"enhancements".


More information about the Digitalmars-d mailing list