1

It's said that floating point addition is commutative but not associative.

An example of it being non associative is the following:

(1 + 1e100) + -1e100 = 0, and 1 + (1e100 + -1e100) = 1

But doesn't this also prove that they are not commutative by the following:

1 + 1e100 + -1e100 = 0, and 1e100 + -1e100 + 1 = 1

Sea Erchin
  • 31
  • 3
  • When you says "It's said", where did you see that? – leleogere May 12 '22 at 12:28
  • @leleogere It's mentioned on wikipedia and all over stackoverflow. Eg this answer mentions it's guaranteed to be commutative: https://stackoverflow.com/a/46776601/3901677 – Sea Erchin May 12 '22 at 12:41
  • That's good to know! I think that the problem in you second example is associativity, not commutativity. If I'm not wrong, python computes additions from the left to the right, meaning that in the first computation, it will compute `1 + 1e100` (`=1e100`), and then subtract `1e100`, which gives `0`. In the second example, it starts by computing `1e100 - 1e100` (`=0`), and then add `1`, which gives `1`. This is an associativity problem due to the order the operations are made by python, not a commutativity problem. – leleogere May 12 '22 at 13:17

0 Answers0