exp() precision between Mac OS and Windows
Asked Answered
T

1

1

I got a code here, and when I run them on Win and Mac OS, the precision of the results is different, anyone can help?

const double c = 1 - exp(-2.0);
double x = (139 + 0.5) / 2282.0;
x = ( 1 - exp(-2 * (1 - x))) / c;

The results are both 0.979645005277687, but the Hex are different:

Win: 3FEF59407B6B6FF1
Mac: 3FEF59407B6B6FF2

How Can I get the same result.

Tropho answered 5/3, 2013 at 6:3 Comment(5)
"Slightly different implementations?"Alright
I don't think you can - the difference is inherent in the way the two platforms implement FP calculations.Ouzel
@roger_rowland Even if both were running on the same processor/FPU? That is, why would the platform inherently be different?Alright
@roger_rowland How does that inherently make these operations behave different? Consider if the code was compiled/run under the same version of the GCC compiler/libc stack with the same flags and ran on the same hardware. Now, consider code compiled by GCC under no optimizations and then with the Intel Compiler using all the "fast math" options enabled - what would the result be? Thus, it can be demonstrated that the OS is not the underlying/fundamental cause.Alright
@pst - good point, not thinking straight. Under those constraints I agree with you. I guess I was thinking about CRT implementation on Windows and a different runtime on Mac - I hadn't considered same compiler/linker on both.Ouzel
A
1

How Can I get the same result.

Unless the math library on OS X uses the very same implementation/algorithm for calculating e ^ x, you won't and can't really get exactly the same results. Floating-point calculations aren't exact, deal with it.

Adriene answered 5/3, 2013 at 6:5 Comment(2)
Actually I use pow(M_E,n) and then get the same result, but sometimes the pow() precision is different either. So the precision is always borning me.Tropho
@RyanZheng Yes, pow() has different implementations too.Adriene

© 2022 - 2024 — McMap. All rights reserved.