使用机器epsilon适合于浮点相等性测试吗? [英] Is the use of machine epsilon appropriate for floating-point equality tests?

查看:204
本文介绍了使用机器epsilon适合于浮点相等性测试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是
是一个非常类似的问题 Double.Epsilon 表示相等,大于,小于,小于或等于,大于或等于 。 / b>




众所周知,对于两个浮点值 x 应该看起来更像这样(而不是一个简单的=):
$ b


abs( x - y )<   ,   是一些非常小的值。


如何为 epsilon



显然,选择 epsilon 尽可能小,例如,.NET框架提供了一个常量 System.Double.Epsilon (= 4.94066× 10 <-324 ),它表示大于零的最小的正值 System.Double 值。

然而,事实证明,这个特殊的值不能可靠地用作 epsilon ,因为:


0  + System.Double.Epsilon ≠  0



1  + System.Double.Epsilon =  1  (!)


也就是说,如果我理解正确,因为那个常量小于 machine epsilon


→这是正确的吗?



→这是否也意味着我可以可靠地使用 epsilon:=机器epsilon 进行相等性测试?



删除了这两个问题,因为它们已经被第二个SO问题充分地回答了 连结维基百科文章指出,对于64位浮点数(即许多语言中的 double 类型),机器epsilon等于:


<2> -53 , 或约。 0.000000000000000111(小数点后有15个零的数字)

→是否遵循这个原则,所有的64位浮点值保证精确到14位(如果不是15位)?

解决方案<如何为epsilon选择一个值?

简短的回答:

您可以获得一个适合您的应用程序需求的小值。
$ b 长答案:准确你期望你的结果是。由于舍入误差总结机器epsilon将几乎所有的时间太大,所以你必须选择自己的价值。根据你的需要,0.01是足够的,或者可能是0.00000000000001或更少。



问题是,你真的想/需要做平等测试浮点值?也许你应该重新设计你的算法。

This is a follow-up to Testing for floating-point value equality: Is there a standard name for the "precision" constant?.
There is a very similar question Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to.


It is well known that an equality test for two floating-point values x and y should look more like this (rather than a straightforward =):

abs( x - y ) < epsilon   ,   where epsilon is some very small value.

How to choose a value for epsilon?

It would obviously be preferable to choose for epsilon as small a value as possible, to get the highest-possible precision for the equality check.

As an example, the .NET framework offers a constant System.Double.Epsilon (= 4.94066 × 10-324), which represents the smallest positive System.Double value that is greater than zero.

However, it turns out that this particular value can't be reliably used as epsilon, since:

0  + System.Double.Epsilon ≠  0

1  + System.Double.Epsilon =  1   (!)

which is, if I understand correctly, because that constant is less than machine epsilon.

→ Is this correct?

→ Does this also mean that I can reliably use epsilon := machine epsilon for equality tests?

Removed these two questions, as they are already adequately answered by the second SO question linked-to above.


The linked-to Wikipedia article says that for 64-bit floating-point numbers (ie. the double type in many languages), machine epsilon is equal to:

2-53,   or approx. 0.000000000000000111 (a number with 15 zeroes after the decimal point)

→ Does it follow from this that all 64-bit floating point values are guaranteed to be accurate to 14 (if not 15) digits?

解决方案

How to choose a value for epsilon?

Short Answer: You take a small value which fits your applications needs.

Long Answer: Nobody can know which calculations your application does and how accurate you expect your results to be. Since rounding errors sum up machine epsilon will be almost all times far too big so you have to chose your own value. Depending on your needs, 0.01 be be sufficient, or maybe 0.00000000000001 or less will.

The question is, do you really want/need to do equality tests on floating point values? Maybe you should redesign your algorithms.

这篇关于使用机器epsilon适合于浮点相等性测试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆