是否有一个x的浮点值,其中x-x == 0是否为false? [英] Is there a floating point value of x, for which x-x == 0 is false?
问题描述
//可以触发断言吗?
float x = //?
assert(xx == 0)
我的猜测是nan / inf可能是特殊的但是我对简单值发生了什么感兴趣。
编辑:
$ b $如果有人可以引用一个引用(IEEE浮点标准),我很乐意选择一个答案? 解决方案
正如你所暗示的, inf-inf
是 NaN
,它不等于零。同样, NaN - NaN
是 NaN
。但是,对于任何有限的浮点数 x
, x - x == 0.0
在舍入模式下, x - x
的结果可能是负的零,但是负的零比较等于 0.0
in浮点运算)。
编辑:给一个明确的标准引用有点棘手,因为这是规则的一个紧急属性在IEEE-754标准中阐述。具体来说,从第5条中定义的操作被正确舍入的要求出发。减法就是这样一个操作(第5.4.1节算术运算),正确舍入的结果 x - x
是适当符号的零(6.3节,当
和
两个操作数的差值(或者
)的两个操作数的总和为
$ b
与
完全相同的零),除了
roundTowardNegative之外,所有
舍入方向属性的总和(或
差值)的符号应为+0;在
属性下,一个确切的
总和(或差额)的符号应该是-0.
因此, x - x
的结果必须是 +/- 0
,因此必须与 0.0
(5.11节第2段):
lockquote
比较应忽略零符号。 / b>
进一步编辑:这并不是说错误的编译器不能引发该断言。你的问题是模棱两可的没有有限的浮点数 x
,所以 x - x == 0
为false。但是,这不是您发布的代码所检查的内容;它检查C样式语言中的某个表达式是否可以评估为非零值;特别是在某些平台上,在某些(不明智的)编译器优化的情况下,表达式中的变量 x
的两个实例可能有不同的 值,导致断言失败(特别是如果 x
是一些计算的结果,而不是一个常量,可表示的值)。这是这些平台上的数字模型中的一个错误,但这并不意味着它不会发生。
In most cases, I understand that a floating point comparison test should be implemented using over a range of values (abs(x-y) < epsilon), but does self subtraction imply that the result will be zero?
// can the assertion be triggered?
float x = //?;
assert( x-x == 0 )
My guess is that nan/inf might be special cases, but I'm more interested in what happens for simple values.
edit:
I'm happy to pick an answer if someone can cite a reference (IEEE floating point standard)?
As you hinted, inf - inf
is NaN
, which is not equal to zero. Similarly, NaN - NaN
is NaN
. It is true, however, that for any finite floating-point number x
, x - x == 0.0
(depending on the rounding mode, the result of x - x
might be negative zero, but negative zero compares equal to 0.0
in floating-point arithmetic).
Edit: it's a little tricky to give a clear standards reference, because this is an emergent property of the rules set forth in the IEEE-754 standard. Specifically, it follows from the requirement that operations defined in Clause 5 be correctly rounded. Subtraction is such an operation (Section 5.4.1 "Arithmetic operations"), and the correctly-rounded result of x - x
is a zero of the appropriate sign (Section 6.3, paragraph 3):
When the sum of two operands with opposite signs (or the difference of two operands with like signs) is exactly zero, the sign of that sum (or difference) shall be +0 in all rounding-direction attributes except roundTowardNegative; under that attribute, the sign of an exact zero sum (or difference) shall be −0.
So the result of x - x
must be +/- 0
, and therefore must compare equal to 0.0
(Section 5.11, paragraph 2):
Comparisons shall ignore the sign of zero.
Further Edit: That's not to say that a buggy compiler couldn't cause that assert to fire. Your question is ambiguous; there is no finite floating point number x
such that x - x == 0
is false. However, that's not what the code that you posted checks; it checks whether or not a certain expression in a C-style language can evaluate to a non-zero value; in particular, on certain platforms, with certain (ill-conceived) compiler optimizations, the two instances of the variable x
in that expression might have different values, causing the assertion to fail (especially if x
is the result of some computation, instead of a constant, representable value). This is a bug in the numerics model on those platforms, but that doesn't mean that it can't happen.
这篇关于是否有一个x的浮点值,其中x-x == 0是否为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!