sin, cos, other languages and DMD/LDC difference

Philippe Sigaud philippe.sigaud at gmail.com
Sat Feb 8 05:47:28 PST 2014


I was reading this thread on the Clojure Google group:

https://groups.google.com/forum/#!topic/clojure/kFNxGrRPf2k

Where the guy is mostly computing (converting from the C++ code):

import std.math;
import std.stdio;

double g(double x) {
     return sin(2.3*x) + cos(3.7*x);
}

void main() {
     double x = 0;
     foreach(_; 0..100_000_000)
         x = g(x);

     writeln(x);
}

He found different results for Clojure and for (non-JVM) 
languages, like C++, Lua or Python, which all returned the same 
value.

So I tested this code using D, and found yet another result. I 
agree with comments in the original thread that after 100M 
iterations what I see is mostly numerical noise (if that's not 
true, please enlighten me!), so I'm not otherwise stressed by 
this.

Now, what I found more confusing is that, compiling with DMD or 
LDC, I got different results. Since Phobos code defining sin and 
cos in std.math and core.stdc.math is the same for DMD and LDC 
(duh!), I guess that means different intrinsics are used?



More information about the Digitalmars-d mailing list