浮点乘以零保证产生零吗?
Is floating point multiplication by zero guaranteed to produce zero?
我知道浮点数有舍入误差,但我想知道是否有某些情况不适用该误差,例如乘以零。
对于所有浮点数,零乘以任何数字是否 = 零?
错:
0f * NAN == NAN
0f * INFINITY == NAN
和...
0f * -1f == -0f (negative 0f), with 0f == -0f :-)
(在 Intel 上,VC++,可能在任何使用 IEEE 754-1985 浮点数的平台上)
ideone 上的示例(可能在某些 Intel 兼容平台上使用 GCC)
除了 好的答案外,考虑 OP 的一些中间 post 问题:
I'm wondering if there are certain situations where the (rounding) error does not apply
候选人包括 some_double_y = some_double_x * 1.0
和 some_double_y = some_double_x + 0.0
可能永远不会出现舍入错误。
然而,由于编译器可能会以更高的精度计算 double
,考虑到 FLT_EVAL_METHOD == 2
其中 "evaluate all operations and constants to the range and precision of the long double
type." 在这种情况下,可能存在中间 some_double_x
作为 long double
不同于 0.0
或 1.0
.
的表观 double
值
我知道浮点数有舍入误差,但我想知道是否有某些情况不适用该误差,例如乘以零。
对于所有浮点数,零乘以任何数字是否 = 零?
错:
0f * NAN == NAN
0f * INFINITY == NAN
和...
0f * -1f == -0f (negative 0f), with 0f == -0f :-)
(在 Intel 上,VC++,可能在任何使用 IEEE 754-1985 浮点数的平台上)
ideone 上的示例(可能在某些 Intel 兼容平台上使用 GCC)
除了
I'm wondering if there are certain situations where the (rounding) error does not apply
候选人包括 some_double_y = some_double_x * 1.0
和 some_double_y = some_double_x + 0.0
可能永远不会出现舍入错误。
然而,由于编译器可能会以更高的精度计算 double
,考虑到 FLT_EVAL_METHOD == 2
其中 "evaluate all operations and constants to the range and precision of the long double
type." 在这种情况下,可能存在中间 some_double_x
作为 long double
不同于 0.0
或 1.0
.
double
值