[LLVMdev] pow operator on Windows

Eli Friedman eli.friedman at gmail.com
Fri Feb 11 22:03:57 PST 2011


On Fri, Feb 11, 2011 at 8:52 PM, Michael Smith
<Michael.Smith at synopsys.com> wrote:
> I have a very simple test case on Windows that shows some surprising behavior. This doesn't seem to be a problem on Linux.
>
> The example is:
> #include <stdio.h>
> #include <math.h>
> double heat(double Pr) {
>  return pow(Pr, 0.33);
> }
> int main(int argc, char **argv) {
>  double Nu = heat(291.00606180486119);
>  printf("%.20f\n", Nu);
> }
>
> I've tested with MinGW's gcc.exe 3.4.5 and the Visual Studio 2008 C++ compiler, and LLVM 2.7 and 2.8.
> With "gcc test.c; ./a.exe", the printed result is 6.50260946378542390000
> With "clang -emit-llvm -c test.c -o test.bc; lli test.bc", the printed result is 6.50260946378542480000
>
> The difference in the last 2 digits is significant in the scientific software I'm working on. Does anyone have an explanation for why they differ, or how to make them behave the same?

Both answers are within one double-precision ulp of the true answer,
which is roughly 6.5026094637854243.  So both answers are acceptable
return values for pow(); normal implementations do not guarantee
half-ulp accuracy for transcendental functions like pow().  My guess
is that lli and the gcc-compiled program are using different versions
of the C runtime library, and therefore different pow()
implementations.  If you really care about floating-point calculations
being precisely reproducible across platforms, I would suggest using a
library like mpfr instead of the compiler's floating-point
implementation.

-Eli




More information about the llvm-dev mailing list