如何比较双数? [英] How to compare double numbers?

查看:281
本文介绍了如何比较双数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  bool AreSame(double a)我知道,当我想检查double == double是否应该写: ,双b)
{
return fabs(a - b)< EPSILON;
}

但是当我想要检查 a > b b>一个

解决方案

没有通用的解决方案来比较包含以前操作错误的浮点数。必须使用的代码是特定于应用程序的。所以,为了得到正确的答案,你必须更具体地描述你的情况。例如,如果您在列表或其他数据结构中对数字进行排序,则不应使用任何容差进行比较。



通常,如果您的程序需要比较两个数字为了秩序,但不能这样做,因为它只有这些数字的近似值,那么你应该重新设计程序,而不是尝试让数字被错误地排序。

底层的问题是使用不正确的数据执行正确的计算通常是不可能的。如果你想计算两个精确数学值 x y 的一些函数,但是你所拥有的唯一数据是一些错误的计算值 x y ,通常不可能计算完全正确的结果。例如,假设你想知道总和是多少,但你只知道 x 是3, y 是4,但是你不知道真正的 x y 是什么。如果你知道 x + y y 大概是大约 x y ,那么您可以通过添加 x y 来计算 x + y的近似值。当被计算的函数具有合理的导数时,作品的作品会稍微改变其输出。当您要计算的函数具有不连续性或较大的导数时,这会失败。例如,如果要使用近似值 x 但是计算 x(在真实域中)的平方根, x 可能由于先前的舍入错误而为负,则计算 sqrt(x)可能会产生异常。同样,比较不平等或秩序是一个不连续的功能:投入的轻微变化可以完全改变答案。

常见的不良建议是与宽容 。这种方法将假阴性(不正确的拒绝数字,如果真正的数学值比较,将满足比较)误报(不正确的接受数字,不会满足比较)。

是否适用可以容忍错误接受取决于应用程序。因此,目前还没有一个通用的解决方案。

设定的宽容程度,甚至是计算的性质,取决于数据,错误和以前的计算。因此,即使与容差进行比较是可以接受的,容差的使用量以及如何计算也取决于应用。没有通用的解决方案。


I know that when I would like to check if double == double I should write:

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

But what when I would like to check if a > b or b > a ?

解决方案

There is no general solution for comparing floating-point numbers that contain errors from previous operations. The code that must be used is application-specific. So, to get a proper answer, you must describe your situation more specifically. For example, if you are sorting numbers in a list or other data structure, you should not use any tolerance for comparison.

Usually, if your program needs to compare two numbers for order but cannot do so because it has only approximations of those numbers, then you should redesign the program rather than try to allow numbers to be ordered incorrectly.

The underlying problem is that performing a correct computation using incorrect data is in general impossible. If you want to compute some function of two exact mathematical values x and y but the only data you have is some incorrectly computed values x and y, it is generally impossible to compute the exactly correct result. For example, suppose you want to know what the sum, x+y, is, but you only know x is 3 and y is 4, but you do not know what the true, exact x and y are. Then you cannot compute x+y.

If you know that x and y are approximately x and y, then you can compute an approximation of x+y by adding x and y. The works when the function being computed has a reasonable derivative: Slightly changing the inputs of a function with a reasonable derivative slightly changes its outputs. This fails when the function you want to compute has a discontinuity or a large derivative. For example, if you want to compute the square root of x (in the real domain) using an approximation x but x might be negative due to previous rounding errors, then computing sqrt(x) may produce an exception. Similarly, comparing for inequality or order is a discontinuous function: A slight change in inputs can change the answer completely.

The common bad advice is to compare with a "tolerance". This method trades false negatives (incorrect rejections of numbers that would satisfy the comparison if the true mathematical values were compared) for false positives (incorrect acceptance of numbers that would not satisfy the comparison).

Whether or not an applicable can tolerate false acceptance depends on the application. Therefore, there is no general solution.

The level of tolerance to set, and even the nature by which it is calculated, depend on the data, the errors, and the previous calculations. So, even when it is acceptable to compare with a tolerance, the amount of tolerance to use and how to calculate it depends on the application. There is no general solution.

这篇关于如何比较双数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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