Is floating point addition commutative in C++?

后端 未结 3 887
梦如初夏
梦如初夏 2020-12-01 13:41

For floating point values, is it guaranteed that a + b == b + a ?

I believe this is guaranteed in IEEE754, however the C++ standard does not specify tha

3条回答
  •  时光说笑
    2020-12-01 14:19

    It is not even required that a + b == a + b. One of the subexpressions may hold the result of the addition with more precision than the other one, for example when the use of multiple additions requires one of the subexpressions to be temporarily stored in memory, when the other subexpression can be kept in a register (with higher precision).

    If a + b == a + b is not guaranteed, a + b == b + a cannot be guaranteed. If a + b does not have to return the same value each time, and the values are different, one of them necessarily will not be equal to one particular evaluation of b + a.

提交回复
热议问题