浮点数的平等(特例) [英] Equality of floating-point numbers (special case)

查看:88
本文介绍了浮点数的平等(特例)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我确定这个新闻组已经被打死了,但是我不能确认CLC常见问题解答。


请考虑以下代码:


--------


double x = some_value_from_somewhere;

double y = x;


if((x == y)&&((x-y )== 0)){

put(这就是我想要的);

}


- ------


if语句中的条件是否始终无条件地和

可以评估为真值?


我知道我通常不能依靠测试浮点数来获得平等;我只想知道当有问题的变量是否明确地设置为源中的相同值时,这是否也成立。


即使输入这个问题我也觉得有点愚蠢,因为我无法想象

x和y可能会因为任何实现'

标准而变得不相等,但是因为我已被咬了很多次,因为这些东西看起来很明显,但结果却是不同的(因为,回想起来,非常感觉因为合理的原因)我觉得我宁愿问。


谢谢,

罗伯特

Hi guys,

I''m sure this has been beaten to death on this newsgroup, but I can''t
find it in the CLC FAQ.

Consider the following code:

--------

double x = some_value_from_somewhere;
double y = x;

if ((x == y) && ((x - y) == 0)) {
puts("This is what I want");
}

--------

Will the condition in the if statement always, unconditionally, and
portably evaluate to a true value?

I know that I normally cannot rely on testing floats for equality; I
just wonder if this holds also when the variables in question are
explicitly set to the same value in the source.

I feel a bit stupid even typing this question since I can''t imagine how
x and y could possibly come out as unequal by any implementation''s
standard, but since I''ve been bitten quite a few times by things that
seemed obvious but turned out to be different (for, in retrospect, very
sensible reasons) I feel that I''d rather ask.

Thanks,
robert

推荐答案




Robert Latest写道日期:04/19/06 10:06,:


Robert Latest wrote On 04/19/06 10:06,:
大家好,

我'我确定这个新闻组已经被打死了,但我不能在CLC FAQ中找到它。

请考虑以下代码:
--------

double x = some_value_from_somewhere;
double y = x ;

if((x == y)&& ((x - y)== 0)){
put(这就是我想要的);
}

--------

if语句中的条件是否会无条件地,并且可以轻松地评估为真值?
Hi guys,

I''m sure this has been beaten to death on this newsgroup, but I can''t
find it in the CLC FAQ.

Consider the following code:

--------

double x = some_value_from_somewhere;
double y = x;

if ((x == y) && ((x - y) == 0)) {
puts("This is what I want");
}

--------

Will the condition in the if statement always, unconditionally, and
portably evaluate to a true value?




No.标准允许浮点实现

支持不是数字的概念,或NaN。在IEEE

实现中,NaN不等于所有浮点值,

包括它自己(所以`x == y''是假的)和大多数普通的

使用NaN操作数的算术运算产生NaN结果

(所以'x -y''是NaN而'x -y == 0''是假的)。


-
Er ********* @ sun。 com



No. The Standard permits floating-point implementations
that support the notion of "not a number," or NaN. In IEEE
implementations, NaN is unequal to all floating-point values,
including itself (so `x == y'' is false) and most ordinary
arithmetic operations with NaN operands yield a NaN result
(so `x - y'' is NaN and `x - y == 0'' is false).

--
Er*********@sun.com


2006年4月19日星期三10:29:15 -0400,

Eric Sosman< Er*********@sun.com>在Msg写了

。 < 1145456956.867691@news1nwk>
On Wed, 19 Apr 2006 10:29:15 -0400,
Eric Sosman <Er*********@sun.com> wrote
in Msg. <1145456956.867691@news1nwk>
否。标准允许浮点实现
支持非数字的概念。或NaN。在IEEE
实现中,NaN不等于所有浮点值,包括它自己(所以`x == y''是假的)
No. The Standard permits floating-point implementations
that support the notion of "not a number," or NaN. In IEEE
implementations, NaN is unequal to all floating-point values,
including itself (so `x == y'' is false)




有道理。非NaN案例怎么样?


罗伯特



Makes sense. What about the non-NaN case?

robert





Robert Latest写道日期:04/19/06 11:13,:


Robert Latest wrote On 04/19/06 11:13,:
2006年4月19日星期三10:29:15 -0400,
Eric Sosman< Er *** ******@sun.com>在Msg中写道
< 1145456956.867691@news1nwk>
On Wed, 19 Apr 2006 10:29:15 -0400,
Eric Sosman <Er*********@sun.com> wrote
in Msg. <1145456956.867691@news1nwk>
否。标准允许浮点实现
支持非数字的概念。或NaN。在IEEE
实现中,NaN不等于所有浮点值,包括它自己(所以`x == y''是假的)
No. The Standard permits floating-point implementations
that support the notion of "not a number," or NaN. In IEEE
implementations, NaN is unequal to all floating-point values,
including itself (so `x == y'' is false)



有意义。那个非NaN案例怎么样?


Makes sense. What about the non-NaN case?




上线代码是



The up-thread code was

double x = some_value_from_somewhere;
double y = x;

if((x == y)&&((x-y)== 0)){
put(这就是我想要的);
}
double x = some_value_from_somewhere;
double y = x;

if ((x == y) && ((x - y) == 0)) {
puts("This is what I want");
}




puts如果'some_value_from_somewhere''

是NaN,则不会调用(),如果

值是无穷大,我认为它也将保持不变。即使是有限的值,我认为你是实施的左右。
。有些系统计算

中间值比'double''更精确将

保持,舍入或截断当值实际存储为
时。在这样的系统上,结果可能取决于优化器决定做什么:如果`some_value_from_somewhere''

带有额外的精度,那么至少可能是

生成的代码可能会执行类似


compute_high_precision some_value_from_somewhere

store_rounded x

store_rounded y

compare_to y


....因此重新使用超精确的`some_value_from_somewhere''

而不是通常精确的` x'',比较可能

收益不等。


浮点实现有很多怪癖,甚至
$ b今天$ b,难怪C委员会决定不试图通过法令来解决这个问题。因此,C中的F-P

行为的规格非常宽松,并且角落情况倾向于在不同系统上产生不同的答案。我听说过

说(虽然我不足以评估论证的FP专家)没有高级编程语言提供

a即使是IEEE二进制浮点也能正确实现。


-
Er ********* @ sun.com



puts() will not be called if `some_value_from_somewhere''
is a NaN, and I think it will also remain uncalled if the
value is an infinity. Even for finite values I think you are
at the mercy of the implementation. Some systems compute
intermediate values to more precision than `double'' will
hold, rounding or truncating when the value is actually
stored. On such a system, the result could depend on just
what the optimizer decides to do: if `some_value_from_somewhere''
carries extra precision it is at least possible that the
generated code might do something like

compute_high_precision some_value_from_somewhere
store_rounded x
store_rounded y
compare_to y

.... thus re-using the extra-precise `some_value_from_somewhere''
instead of the normally-precise `x'', and the comparison could
yield non-equal.

Floating-point implementations have a lot of quirks, even
today, and it is no wonder that the C committee decided not to
try to iron them out by fiat. Thus, the specifications of F-P
behavior in C are awfully loose, and the corner cases tend to
yield different answers on different systems. I''ve heard it
said (though I''m not enough of an F-P specialist to evaluate
the argument) that no high-level programming language provides
a correct implementation even of IEEE binary floating-point.

--
Er*********@sun.com


这篇关于浮点数的平等(特例)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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