我们是否需要Epsilon值才能进行较小或更大的比较以使浮点值进行比较?

发布于 2025-01-30 01:16:30 字数 466 浏览 2 评论 0原文

我已经经历了不同的线程,以比较较小或更高的浮点值不相等的比较,但尚不清楚我们需要Epsilon值逻辑以比较较小或更大的浮点值吗?

例如 - >

float a, b;
 if (a < b) // is this correct way to compare two float value or we need epsilon value for lesser comparator 
{
}
if (a > b) // is this correct way to compare two float value for greater comparator
{
}

我知道要比较浮子的平等,我们需要一些epsilon值

bool AreSame(double a, double b)
{
    return fabs(a - b) < EPSILON;
}

I have gone through different threads for comparing lesser or greater float value not equal comparison but not clear do we need epsilon value logic to compare lesser or greater float value as well?

e.g ->

float a, b;
 if (a < b) // is this correct way to compare two float value or we need epsilon value for lesser comparator 
{
}
if (a > b) // is this correct way to compare two float value for greater comparator
{
}

I know for comparing for equality of float, we need some epsilon value

bool AreSame(double a, double b)
{
    return fabs(a - b) < EPSILON;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

╄→承喏 2025-02-06 01:16:30

这实际上取决于当两个值都足够接近以至于相等时应该发生的事情,含义fabs(a -b)&lt; Epsilon。在某些用例中(例如,用于计算统计数据),如果2个近距值之间的比较给出或不相等,这并不重要。

如果重要的话,您应该首先确定值的不确定性。这实际上取决于用例(输入值来自何处以及如何处理),然后2个值不同于不确定性的差异应视为平等。 但是 equality 不再是一个真正的数学等价关系:您可以轻松地想象如何在2个truely Alting Difeble之间构建链条a clos 。用数学词,这种关系不是传递的(或几乎是传递的是当前的语言单词)。

很抱歉,但是,一旦您必须处理近似,就不可能有任何精确且一致的方法:您必须考虑现实世界的用例,以确定如何处理近似值。

It really depends on what should happen when both value are close enough to be seen as equal, meaning fabs(a - b) < EPSILON. In some use cases (for example for computing statistics), it is not very important if the comparison between 2 close values gives or not equality.

If it matters, you should first determine the uncertainty of the values. It really depends on the use case (where the input values come from and how they are processed), and then 2 value differing by less than that uncertainty should be considered as equal. But that equality is not longer a true mathematical equivalence relation: you can easily imagine how to build a chain a close values between 2 truely different values. In math words, the relation is not transitive (or is almost transitive is current language words).

I am sorry but as soon as you have to process approximations there cannot be any precise and consistent way: you have to think of the real world use case to determine how you should handle the approximation.

千寻… 2025-02-06 01:16:30

当您使用浮子时,您不可避免地会遇到精确错误。

为了减轻这种情况,在检查平等时,我们经常检查它们的差异是否足够小。

但是,对于较小和更大的速度,无法完全确定哪种浮子更大。最好使用Aresame函数,最好(大概是针对您的意图)方法是首先检查两个浮子是否相同。如果是这样,返回false(如a = b,意味着a&lt; b and a&gt; b都是false)。

否则,返回a&lt的值; ba&gt; B

When you are working with floats, it's inevitable that you will run into precision errors.

In order to mitigate this, when checking for the equality two floats we often check if their difference is small enough.

For lesser and greater, however, there is no way to tell with full certainty which float is larger. The best (presumably for your intentions) approach is to first check if the two floats are the same, using the areSame function. If so return false (as a = b implies that a < b and a > b are both false).

Otherwise, return the value of either a < b or a > b.

迷鸟归林 2025-02-06 01:16:30

答案取决于应用程序。

如果您确定A和B足够不同,即数值错误不会逆转顺序,则A&lt; B足够好。

但是,如果A和B危险地接近,则可能需要A&lt; B + Epsilon。在这种情况下,您应该清楚地表明&lt; ≤无法区分。

不用说,应该以最大的护理选择Epsilon(通常很难)。

The answer is application dependent.

If you are sure that a and b are sufficiently different that numerical errors will not reverse the order, then a < b is good enough.

But if a and b are dangerously close, you might require a < b + EPSILON. In such a case, it should be clear to you that < and ≤ are not distinguishable.

Needless to say, EPSILON should be chosen with the greatest care (which is often pretty difficult).

行雁书 2025-02-06 01:16:30

最终取决于您的应用程序,但我通常会说不。

问题非常简化,是如果您计算:(1/3) * 3并获取答案0.999999,那么您希望它将其比较等于> 1。这就是为什么我们使用Epsilon值进行平等比较的原因(应根据应用程序和预期的精度选择Epsilon)。

另一方面,如果要对浮子的列表进行排序,则默认情况下,0.999999值将在1之前进行排序。但是,正确的行为是什么?如果它们俩都被排序为1,则将是一个随机的,它实际上是首先排序的(取决于列表的初始顺序以及您使用的排序算法)。

浮点数的问题不是它们是“随机”,也无法预测其确切值。问题在于碱基10分数不能干净地转换为碱基-2分数,并且一个系统中的不重复小数可以转化为另一个重复一个,然后在截断为有限数量的有限数时会导致舍入错误小数。我们使用Epsilon值进行平等比较来处理由这些来回翻译引起的舍入误差。

但是请注意,==&lt;&lt; =具有整数的良好关系浮点完全是因为涉及的伊普西隆。示例:

  • a = x
  • b = a + epsilon/2
  • c = b + epsilon/2
  • d = c + epsilon/2

现在:a == bb == b == c,<代码> c == d ,但是a!= da&lt; D。实际上,您可以继续保持序列num(n)== num(n+1),同时在a和最后一个之间有一个任意的差异序列中的数字。

It ultimately depends on your application, but I would say generally no.

The problem, very simplified, is that if you calculate: (1/3) * 3 and get the answer 0.999999, then you want that to compare equal to 1. This is why we use epsilon values for equal comparisons (and the epsilon should be chosen according to the application and expected precision).

On the other hand, if you want to sort a list of floats then by default the 0.999999 value will sort before 1. But then again what would the correct behavior be? If they both are sorted as 1, then it will be somewhat random which one is actually sorted first (depending on the initial order of the list and the sorting algorithm you use).

The problem with floating point numbers is not that they are "random" and that it is impossible to predict their exact values. The problem is that base-10 fractions don't translate cleanly into base-2 fractions, and that non-repeating decimals in one system can translate into repeating one in the other - which then result in rounding errors when truncated to a finite number of decimals. We use epsilon values for equal comparisons to handle rounding errors that arise from these back and forth translations.

But do be aware that the nice relations that ==, < and <= have for integers, don't always translate over to floating points exactly because of the epsilons involved. Example:

  • a = x
  • b = a + epsilon/2
  • c = b + epsilon/2
  • d = c + epsilon/2

Now: a == b, b == c, c == d, BUT a != d, a < d. In fact, you can continue the sequence keeping num(n) == num(n+1) and at the same time get an arbitrarily large difference between a and the last number in the sequence.

霞映澄塘 2025-02-06 01:16:30

正如其他人所说,处理浮子时总会出现精确错误。

因此,即使比较小于 /大于 /大于 /大,您也应该具有epsilon值。

我们知道,为了使a小于b,首先,a必须与b不同。检查这是一个简单的不是平等的,它使用epsilon。
然后,一旦您已经知道a!= b,操作员&lt;就足够了。

As others have stated, there would always be precision errors when dealing with floats.

Thus, you should have an epsilon value even for comparing less than / greater than.

We know that in order for a to be less than b, firstly, a must be different from b. Checking this is a simple NOT equals, which uses the epsilon.
Then, once you already know a != b, the operator < is sufficient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文