accuracy of floating point calculations: d vs cpp

Guillaume Piolat first.last at gmail.com
Mon Jul 22 13:26:42 UTC 2019


On Monday, 22 July 2019 at 13:23:26 UTC, Guillaume Piolat wrote:
> On Monday, 22 July 2019 at 12:49:24 UTC, drug wrote:
>> I have almost identical (I believe it at least) implementation 
>> (D and C++) of the same algorithm that uses Kalman filtering. 
>> These implementations though show different results (least 
>> significant digits). Before I start investigating I would like 
>> to ask if this issue (different results of floating points 
>> calculation for D and C++) is well known? May be I can read 
>> something about that in web? Does D implementation of floating 
>> point types is different than the one of C++?
>>
>> Most of all I'm interesting in equal results to ease comparing 
>> outputs of both implementations between each other. The 
>> accuracy itself is enough in my case, but this difference is 
>> annoying in some cases.
>
> Typical floating point operations in single-precision like a 
> simple (a * b) + c will provide a -140dB difference if order is 
> changed. It's likely the order of operations is not the same in 
> your program, so the least significant digit should be 
> different.

What I would recommend is compute the mean relative error, in 
double, and if it's below -200 dB, not bother. This is an 
incredibly low relative error of 0.00000001%.
You will have no difficulty making your D program deterministic, 
but knowing exactly where the C++ and D differ will be long and 
serve no purpose.


More information about the Digitalmars-d-learn mailing list