by lambdasquirrel · 3y ago · 0 comments
With that said, we abandoned `ab = ba` because it's not useful for e.g. linear algebra. Elementary algebra is a very specific (but very useful) mathematical "DSL" over the reals. It's also not necessarily going to help you learn to reason about the kinds of abstractions you have whilst programming, per se, because we can't reverse `a . b` to `b . a` when we code either.
Make no mistake, this is a knowledge for knowledge's sake endeavor. A liberal arts of the STEM fields, if you will.
The requirement for order independence is literally everywhere … where would we be without being able to add or do set unions or lattice joins without fear of getting the order wrong?! Imagine getting two different waveforms when adding one to the other, depending on the order!
Thanks to BeetleB for pointing out that addition in IEEE floats is indeed commutative (I originally claimed "+0 + -0 = +0 vs. -0 + +0 = -0" which is incorrect).
As for IEEE-754 floats, addition is commutative as long as you don't care about exact bit patterns:
NaN + NaN may return different bit patterns. The result is still NaN, so the only way you can tell this apart is by bit-casting floats to ints or byte arrays.
Multiplication over floats is also commutative modulo the caveat above.
These are not exceptions.
First, I will note that your result above depends on the rounding mode.
Second, IEEE 754 mandates that +0 and -0 are equal (i.e. any equality operator should return True when comparing these two). Therefore both expressions are equal.
NaN has several representations in bits, but they are all "equal" to one another.[1] If an operation gives you NaN, then so will doing it commutatively. It doesn't matter that the underlying bits are the same.
[1] Except for the signaling aspect. But I believe that is preserved in commutative operations.
Specifically, (-0.0) + (+0.0) = (+0.0) + (-0.0) = +0.0 (assuming round-to-nearest-or-even). OTOH, (-0.0) + (-0.0) = -0.0. This has nothing to do with +0.0 == -0.0 for comparison, addition just is commutative outright[0].
0: Pedantically, I'm not sure IEEE-754 requires the specific choice of which NaN you get when you do `some_nan + a_different_nan` versus `a_different_nan + some_nan` to be commutative, but it should.
If a + b results in an overflow, so will b + a.
If so, I agree with you, but not quite in the way you mean (I think). We don't account for it just as fish don't have to account for water. The fact that you can swap two rows in an excel file and have the results be unchanged is a property that we just take for granted. It needs to be pointed out how remarkable it is, because there are situations where this doesn't work. Debits and credits in finance are not commutative because of overdraft limits. I'm sure you know all this, so I'm curious what you really mean.
a = f(c) b = g(d)
can be rearranged if f and g take different input data, and calling independent functions is something seen literally everywhere in programming. Optimizing compilers often take advantage of that.
In the context of linear algebra, it would also be good for folks to know that matrix multiplication (/linear transformation composition) is not commutative.
In FP arithmetic, operations are commutative, not associative.
if a = 0.3 b=0.4 and c=5 as f32 in rust, then the following "well known results" from the article are all false:
(b) a+(b+c) = (a+b)+c is false
(d) a*(b+c) = a*b+a*c is false
(f) (ab)^c = a^c b^c is false
(g) a^b a^c = a^(b+c) is false
(h) (a^b)^c = a^(b*c) is false
https://play.rust-lang.org/?version=stable&mode=debug&editio...