|
Archives
D Programming
digitalmars.Ddigitalmars.D.bugs digitalmars.D.dtl digitalmars.D.ide digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger D.gnu D C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript electronics |
digitalmars.D.learn - Floating point differences at compile-time
I don't understand where the result differences come from in this code:
import std.math: sqrt, PI;
import std.stdio: writefln;
void main() {
const double x1 = 3.0 * PI / 16.0;
writefln("%.17f", sqrt(1.0 / x1));
double x2 = 3.0 * PI / 16.0;
writefln("%.17f", sqrt(1.0 / x2));
real x3 = 3.0 * PI / 16.0;
writefln("%.17f", sqrt(1.0 / x3));
real x4 = 3.0L * PI / 16.0L;
writefln("%.17f", sqrt(1.0L / x4));
}
Output with various D compilers:
DMD1:
1.30294003174111994
1.30294003174111972
1.30294003174111979
1.30294003174111979
DMD2:
1.30294003174111972
1.30294003174111972
1.30294003174111979
1.30294003174111979
LDC:
1.30294003174111994
1.30294003174111994
1.30294003174111972
1.30294003174111972
I'd like the compiler(s) to give more deterministic results here.
Bye,
bearophile
Dec 31 2009
bearophile wrote: Jan 01 2010
|