使用 == 运算符比较两个舍入浮点数是否正确?

发布于 2024-07-07 17:02:12 字数 246 浏览 6 评论 0原文

或者说手术有可能失败吗?

谢谢。

我选择了错误的术语,我真正的意思是四舍五入到 0,而不是截断。

关键是,我需要比较两个双精度数的整数部分,我只是将它们转换为 int,然后使用 ==,但是,正如有人在我之前的问题之一中指出的那样,如果double 无法放入整数。

所以问题是“使用 == 运算符来比较之前四舍五入为 0 的两个双精度数是否正确,或者我应该坚持转换为 int 方法并捕获可能的异常?”

Or is there a chance that the operation will fail?

Thanks.

I chose the wrong term and what I really meant was rounding to 0, not truncation.

The point is, I need to compare the integer part of two doubles and I'm just casting them to int and then using ==, but, as someone pointed out in one of my earlier questions, this could throw an overflow exception if the double can't fit into the integer.

So the question would be 'Is it correct to use the == operator to compare two doubles that have previously been rounded to 0, or should I stick to the casting to int method and catch a possible exception?

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

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

发布评论

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

评论(6

木格 2024-07-14 17:02:12

这是更新后的网站,其中讨论了比较浮点数的几种方法的优缺点。 (您仍然可以在此处查看旧站点< /a>.)

我采用的方法是“相对误差”方法。 找到两个数字之间的差异,将其转换为数字的百分比,如果该百分比足够小,那么您就得到了相等。

Here's the updated site which discusses the pros and cons of several methods of comparing floating point numbers. (You can still view the old site here.)

The method I'd go with is the "relative error" method. Find the difference between the two numbers, convert that to a percentage of the numbers, and if that percentage is sufficiently small, then you've got equality.

奈何桥上唱咆哮 2024-07-14 17:02:12

更糟糕的是,有时,即使对于完全相同的数字,它也会失败。 这是因为某些编译器或处理器将在 CPU 寄存器中使用比在内存中更多的精度位(例如,MSVC 有 3 种不同的浮点行为选项)。 因此,最近计算的值可能不会截断这些位,并且看起来不相等。 切勿在浮点数上使用 ==。

Even worse is that sometimes, even for the exact same number, it will fail. This is because some compilers or processors will use more bits of precision in a CPU register than in memory (MSVC has 3 different floating-point behavior options, for example). Thus a recently-computed value may not have these bits truncated and will appear to be unequal. NEVER use == on floats.

缘字诀 2024-07-14 17:02:12

由于浮点表示的常见问题,它仍然可能失败。 不要截断它们,而是使用代表等效精度的增量。

如果您有两个通常认为相同的浮点数,则它可能会失败,

<块引用>

10.19999999

10.20000001

但是当你截断它们时,它们会给出不同的结果。

<块引用>

10.19

10.20

然而,如果我使用 0.001 的增量来比较差异,我会发现这两个值实际上是相同的。

It can still fail due to the normal problems with floating point representation. Rather than truncating them, use a delta that would represent the equivalent precision.

It can fail in cases where you have two floats that you would normally consider the same,

10.19999999

10.20000001

but when you truncate them they give different results.

10.19

10.20

Whereas, if I had used a delta of 0.001 to compare to the difference, I would have seen that these two values are effectively the same.

维持三分热 2024-07-14 17:02:12

== 与浮点一起使用永远是不正确的。

在浮点上下文中“截断”是什么意思? 您正在调用什么具体的库函数? 结果是什么? 是什么让您相信“截断”值比未截断值更具可比性?

浮点是十进制值的近似值。 浮点数只能精确地表示2的幂。 无论您执行何种浮点运算,所有其他值都会出现一些错误。

但是,如果转换为整数,则可以使用 ==

It's never correct to use == with floating-point.

What does "truncate" mean in a float-point context? What specific library function are you calling? What is the result? What makes you believe that "truncated" values are any more comparable than non-truncated values?

Floating point is an approximation of decimal values. Floating point can only represent powers of two precisely. All other values are subject to some error, no matter what floating-point operations you do.

If, however, you convert to integer, you can use ==.

殊姿 2024-07-14 17:02:12

如果单精度的绝对值小于 2^23 或双精度的绝对值小于 2^52,则可以使用 round() 然后进行比较。
较大的值无法精确存储,这适用于 N == N+1 的情况。

If your absolut value is less than 2^23 for single or 2^52 for double you can use round() and then do the compare.
Larger values can not be precisly stored and this opens for situations where N == N+1.

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