问题描述
就在今天,我遇到了我们正在使用的第三方软件,在他们的示例代码中,有以下内容:
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:
- 是否存在
float == float
给出正确结果的情况? - 在某些情况下
float == float
是可接受的编码吗?
- Are there cases where
float == float
gives the correct result? - 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.
*对于从不的小值.
这篇关于浮点 == 可以吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!