浮点 == 可以吗?

Is floating-point == ever OK?(浮点 == 可以吗?)
本文介绍了浮点 == 可以吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就在今天,我遇到了我们正在使用的第三方软件,在他们的示例代码中,有以下内容:

Just today I came across third-party software we're using and in their sample code there was something along these lines:

// Defined in somewhere.h
static const double BAR = 3.14;

// Code elsewhere.cpp
void foo(double d)
{
    if (d == BAR)
        ...
}

我知道浮点数及其表示的问题,但这让我想知道是否存在 float == float 会好的情况?我不是在问它什么时候可以起作用,而是问它什么时候有意义并且起作用.

I'm aware of the problem with floating-points and their representation, but it made me wonder if there are cases where float == float would be fine? I'm not asking for when it could work, but when it makes sense and works.

另外,像 foo(BAR) 这样的调用呢?这是否总是比较相等,因为它们都使用相同的 static const BAR?

Also, what about a call like foo(BAR)? Will this always compare equal as they both use the same static const BAR?

推荐答案

有两种方法可以回答这个问题:

There are two ways to answer this question:

  1. 是否存在 float == float 给出正确结果的情况?
  2. 在某些情况下 float == float 是可接受的编码吗?
  1. Are there cases where float == float gives the correct result?
  2. Are there cases where float == float is acceptable coding?

(1) 的答案是:是的,有时.但它会很脆弱,这导致 (2) 的答案:不.不要那样做.你在乞求将来出现奇怪的错误.

The answer to (1) is: Yes, sometimes. But it's going to be fragile, which leads to the answer to (2): No. Don't do that. You're begging for bizarre bugs in the future.

至于 foo(BAR) 形式的调用:在这种特殊情况下,比较将返回 true,但是当您编写 foo 时,您不知道(并且不应该取决于)它是如何被调用的.例如,调用 foo(BAR) 可以,但 foo(BAR * 2.0/2.0) (甚至可能是 foo(BAR * 1.0) 取决于编译器优化了多少东西)会中断.你不应该依赖调用者不执行任何算术!

As for a call of the form foo(BAR): In that particular case the comparison will return true, but when you are writing foo you don't know (and shouldn't depend on) how it is called. For example, calling foo(BAR) will be fine but foo(BAR * 2.0 / 2.0) (or even maybe foo(BAR * 1.0) depending on how much the compiler optimises things away) will break. You shouldn't be relying on the caller not performing any arithmetic!

长话短说,尽管 a == b 在某些情况下会起作用,但您真的不应该依赖它.即使你今天可以保证调用语义,也许下周你将无法保证它们,所以不要让自己痛苦,不要使用 ==.

Long story short, even though a == b will work in some cases you really shouldn't rely on it. Even if you can guarantee the calling semantics today maybe you won't be able to guarantee them next week so save yourself some pain and don't use ==.

在我看来,float == float 永远*不行,因为它几乎无法维护.

To my mind, float == float is never* OK because it's pretty much unmaintainable.

*对于从不的小值.

这篇关于浮点 == 可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)