我想问一个问题,为什么非常接近于零的数字的 (e^x-1)/x 的计算值不能正常工作(例如,如果 x=10^-15,结果是 1.1102230),但是当我使用等价的数学公式 (e^x-1)/log(e^x) 时,它给出了 1.000000 的正确结果。谢谢。
最佳答案
问题是第一个函数表现出所谓的灾难性抵消:对于 x 接近 0,ex/em> 非常接近 1 + x。由于 float 在 1 附近的密度低于 0,表达式 ex − 1 的结果将非常接近 x,但是由于中间舍入而失去准确性。
第二个利用了一个巧妙的技巧来“抵消”舍入误差。事实上,此特定示例在 section 1.14.1 中有详细介绍。 Nicholas J. Higham 的优秀著作数值算法的准确性和稳定性。他解释的关键是
The expression (ex − 1) / x cannot be accurately evaluated for a given x ≈ 0 in floating point arithmetic, while the expression (y − 1) / log y can be be accurately evaluated for a given y ≈ 1. Since these functions are slowly varying near x = 0 (y = 1), evaluating (y − 1) / log y with an accurate, if inexact, approximation to y = ex ≈ 1 produces an accurate result.
关于r - 为什么 (e^x-1)/x 不能正常工作,但 (e^x-1)/log(e^x) 可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33187696/