浮点平等和公差 [英] Floating point equality and tolerances
问题描述
的东西比较两个浮点数像 a_float == b_float
是因为找麻烦a_float / 3.0 * 3.0
可能不等于 a_float
由于舍入误差。
Comparing two floating point number by something like a_float == b_float
is looking for trouble since a_float / 3.0 * 3.0
might not be equal to a_float
due to round off error.
什么一般人不会是像晶圆厂(a_float - b_float)LT; TOL
。
如何计算 TOL
?
理想情况下容限应仅比一个或两个的至少显著数字的值大。因此,如果单precision浮点数是使用 TOL = 10E-6
应该是对的。然而,这并没有对其中 a_float
可能是非常小的,或可能是非常大的一般情况下正常工作。
Ideally tolerance should be just larger than the value of one or two of the least significant figures. So if the single precision floating point number is use tol = 10E-6
should be about right. However this does not work well for the general case where a_float
might be very small or might be very large.
如何计算 TOL
所有一般情况下,是否正确?我感兴趣的是C或C ++案件具体。
How does one calculate tol
correctly for all general cases? I am interested in C or C++ cases specifically.
推荐答案
此博文包含了一个例子,相当万无一失的实现,和它背后的详细理论
<一href=\"http://randomascii.word$p$pss.com/2012/02/25/comparing-floating-point-numbers-2012-edition/\">http://randomascii.word$p$pss.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
它也是一个系列之一,让你随时可以了解更多。
总之:使用ULP对于大多数的数字,用小量的接近零的数字,但还是有注意事项。如果你想确保你的浮点运算,我建议你阅读整个系列。
This blogpost contains an example, fairly foolproof implementation, and detailed theory behind it http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ it is also one of a series, so you can always read more. In short: use ULP for most numbers, use epsilon for numbers near zero, but there are still caveats. If you want to be sure about your floating point math i recommend reading whole series.
这篇关于浮点平等和公差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!