News for hackers. Stuff that matters.
Re:Floating point oddity by thethirdone on Monday November 11, 2019 @08:05PM (#21510562)
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.

Re:Floating point oddity by cperciva on Monday November 11, 2019 @08:21PM (#21510650)
As 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).

Re:Floating point oddity by thethirdone on Monday November 11, 2019 @10:05PM (#21511204)
In 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.

Re:Floating point oddity by cperciva on Monday November 11, 2019 @10:48PM (#21511352)
Huh, 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.
Re:Floating point oddity by saagarjha on Monday November 11, 2019 @09:14PM (#21510920)
I can't reproduce this: https://godbolt.org/z/PK4gvZ
Re:Floating point oddity by tlb on Monday November 11, 2019 @09:59PM (#21511168)
It works if the compiler can't do constant folding, like so: https://godbolt.org/z/nQDrSW
Re:Floating point oddity by excessive on Monday November 11, 2019 @10:04PM (#21511199)
Your first line of main is adding -0.0 and -0.0, which is not what I think you intended to show.
Re:Floating point oddity by tlb on Monday November 11, 2019 @10:24PM (#21511272)
Oops, you're right. 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.
Re:Floating point oddity by elcaminocomplex on Monday November 11, 2019 @09:09PM (#21510896)
Can 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.
Re:Floating point oddity by thethirdone on Monday November 11, 2019 @10:08PM (#21511211)
In 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.
Re:Floating point oddity by saagarjha on Monday November 11, 2019 @08:08PM (#21510579)
Oops, I meant associative. Sorry about that; I fixed it above.