accuracy of floating point calculations: d vs cpp

Dennis dkorpel at gmail.com
Mon Jul 22 14:18:20 UTC 2019


On Monday, 22 July 2019 at 12:49:24 UTC, drug wrote:
> 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?

This likely has little to do with the language, and more with the 
implementation. Basic floating point operations at the same 
precision should give the same results. There can be differences 
in float printing (see [1]) and math functions (sqrt, cos, pow 
etc.) however.

Tips for getting consistent results between C/C++ and D:
- Use the same backend, so compare DMD with DMC, LDC with CLANG 
and GDC with GCC.
- Use the same C runtime library. On Unix glibc will likely be 
the default, on Windows you likely use snn.lib, libcmt.lib or 
msvcrt.dll.
- On the D side, use core.stdc.math instead of std.math
- Use the same optimizations. (Don't use -ffast-math for C)

[1] 
https://forum.dlang.org/post/fndyoiawueefqoeobdur@forum.dlang.org


More information about the Digitalmars-d-learn mailing list