简体   繁体   English

浮点乘以零是否保证产生零?

[英]Is floating point multiplication by zero guaranteed to produce zero?

I understand floating point has rounding errors, but I'm wondering if there are certain situations where the error does not apply, such as multiplication by zero . 我知道浮点数有舍入错误,但我想知道是否存在某些错误不适用的情况,例如乘以零。

Does zero times any number = zero for all floating points ? 对于所有浮点,任何数字的零次是否为零?

False: 假:

0f * NAN == NAN
0f * INFINITY == NAN

and ... 和......

0f * -1f == -0f (negative 0f), with 0f == -0f :-)

(on Intel, VC++, and probably on any platform that uses IEEE 754-1985 floating points) (在Intel,VC ++上,可能在任何使用IEEE 754-1985浮点的平台上)

Example on ideone (that uses GCC on some Intel compatible platform probably) 关于ideone的示例(可能在某些Intel兼容平台上使用GCC)

In addition to @xanatos fine answer, consider some of OP's middle-of-the-post concerns: 除了@xanatos的好答案之外, 还要考虑OP的一些中间问题:

I'm wondering if there are certain situations where the (rounding) error does not apply 我想知道是否存在(舍入)错误不适用的某些情况

Candidates include some_double_y = some_double_x * 1.0 and some_double_y = some_double_x + 0.0 may never incur a rounding error. 候选人包括some_double_y = some_double_x * 1.0some_double_y = some_double_x + 0.0可能永远不会产生舍入错误。

Yet even those are suspect due to a compiler may evaluate double at higher precision considering the FLT_EVAL_METHOD == 2 where "evaluate all operations and constants to the range and precision of the long double type." 然而,即使是那些也是可疑的,因为编译器可能会以更高的精度评估double ,考虑到FLT_EVAL_METHOD == 2 ,其中“评估所有操作和常量到long double类型的范围和精度”。 In that case, an intermediate some_double_x may exist as a long double differing from an apparent double value of 0.0 or 1.0 . 在这种情况下,中间some_double_x可以作为与表观double some_double_x0.01.0不同的long double some_double_x存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM