6 ms·
As far as I know, floating point add and multiply are commutative (but not associative) in IEEE 754. And I believe C and C++ allow you to detect if IEEE 754 is
by thethirdone 7y ago
As far as I know, floating point add and multiply are commutative (but not associative) in IEEE 754. And I believe C and C++ allow you to detect if IEEE 754 is used by the hardware and if so, execute the floating point expressions as written.
Note: That is of course no longer true under -ffast-math.
- saagarjha 7y agoOops, I meant associative. Sorry about that; I fixed it above.
- cperciva 7y agoAs far as I know, floating point add and multiply are commutative (but not associative) in IEEE 754. Usually, but not always. (+0) + (-0) yields (+0), while (-0) + (+0) yields (-0).
- elcaminocomplex 7y agoCan you cite any references about this? I don’t recall anything like this at all unless perhaps it’s specific to FENV_ACCESS and non-default rounding modes.
- thethirdone 7y agoIn a sister comment, I quoted IEEE 754-2008 Section 6.3 which shows that 0 is commutative in the newer standard. The older revision has similar language.
- saagarjha 7y agoI can't reproduce this: https://godbolt.org/z/PK4gvZ https://godbolt.org/z/PK4gvZ
- tlb 7y agoIt works if the compiler can't do constant folding, like so: https://godbolt.org/z/nQDrSW https://godbolt.org/z/nQDrSW
- excessive 7y agoYour first line of main is adding -0.0 and -0.0, which is not what I think you intended to show.
- tlb 7y agoOops, you're right. https://godbolt.org/z/pAwqUA https://godbolt.org/z/pAwqUA shows it better. Adding a -0 and +0 together in either order gives +0, using the actual x86-64 addsd instruction.
- thethirdone 7y agoIn IEEE 754-1985 and IEEE 754-2008 this is not valid behavior. I have mostly worked on the 2008 revision, so I didn't know if 0 was commutative in the original standard. Thanks for making me look it up. IEEE 754-2008 > Section 6.3 The sign bit > 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. However, x + x = x − (−x) retains the same sign as x even when x is zero. No quote for IEEE 754-1985 because I didn't want to type it out and it is mostly the same.
- cperciva 7y agoHuh, interesting. I've definitely seen systems where the sum of two zeroes took the sign of the first value. I assumed they were inheriting 754 behaviour but I guess not.