Double.Epsilon相等,大于,小于,小于或等于,大于或等于 [英] Double.Epsilon for equality, greater than, less than, less than or equal to, greater than or equal to

查看:649
本文介绍了Double.Epsilon相等,大于,小于,小于或等于,大于或等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://msdn.microsoft.com/en-us/library/system.double.epsilon.aspx\">http://msdn.microsoft.com/en-us/library/system.double.epsilon.aspx

如果您创建自定义算法
  确定是否两个浮点
  号码可以认为是相等的,则
  必须使用一个值,该值大于
  小量不断建立
  可接受的绝对利润率
  为两个值差为
  认为是相等的。 (通常情况下,该
  差的利润率多次
  大于小量。)

If you create a custom algorithm that determines whether two floating-point numbers can be considered equal, you must use a value that is greater than the Epsilon constant to establish the acceptable absolute margin of difference for the two values to be considered equal. (Typically, that margin of difference is many times greater than Epsilon.)

因此​​,这不是真的,可能被用于比较的小量?我真的不明白MSDN措辞。

So is this not really an epsilon that could be used for comparisons? I don't really understand the MSDN wording.

它可以被用作这里的例子EPSILON? - <一个href=\"http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison/253874#253874\">http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison/253874#253874

Can it be used as the epsilon in the examples here? - http://stackoverflow.com/questions/17333/most-effective-way-for-float-and-double-comparison/253874#253874

最后这似乎真的很重要,所以我想确保我有平等扎实实施,大于,小于,小于或等于和大于或等于。

And finally this seems really important so I'd like to make sure I have a solid implementation for equality, greater than, less than, less than or equal to, and greater than or equal to.

推荐答案

我不知道的什么的他们,当他们写道,吸烟。 Double.Epsilon 是最小重presentable非非正规浮点值不为0所有你知道的是,如果有一个截断误差,它会永远的放大的比这个值。大得多。

I don't know what they were smoking when they wrote that. Double.Epsilon is the smallest representable non-denormal floating point value that isn't 0. All you know is that, if there's a truncation error, it will always be larger than this value. Much larger.

System.Double 键入可以重新present值精确到最多15位。因此,一个简单的一阶估计,如果双值 X 等于某个常数是使用的EPSILON常数* 1E-15

The System.Double type can represent values accurate to up to 15 digits. So a simple first order estimate if a double value x is equal to some constant is to use an epsilon of constant * 1E-15

public static bool AboutEqual(double x, double y) {
    double epsilon = Math.Max(Math.Abs(x), Math.Abs(y)) * 1E-15;
    return Math.Abs(x - y) <= epsilon;
}

您必须虽然看出来,截断误差可以累积。如果两个 X 计算值,则必须增加小量。

You have to watch out though, truncation errors can accumulate. If both x and y are computed values then you have to increase the epsilon.

这篇关于Double.Epsilon相等,大于,小于,小于或等于,大于或等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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