exp() precision between Mac OS and Windows

2019-03-01 21:08发布

问题:

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.

回答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.



标签: precision exp