Is floating point addition commutative in C++?

后端 未结 3 880
梦如初夏
梦如初夏 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:17

    The C++ standard very specifically does not guarantee IEEE 754. The library does have some support for IEC 559 (which is basically just the IEC's version of the IEEE 754 standard), so you can check whether the underlying implementation uses IEEE 754/IEC 559 though (and when it does, you can depend on what it guarantees, of course).

    For the most part, the C and C++ standards assume that such basic operations will be implemented however the underlying hardware works. For something as common as IEEE 754, they'll let you detect whether it's present, but still don't require it.

提交回复
热议问题