4 ms·
"a+b+c" doesn't describe a unique evaluation order. You need some parentheses to disambiguate which changes are due to associativity vs commutativity. a+(b+c)=(
by wtallis 1y ago
"a+b+c" doesn't describe a unique evaluation order. You need some parentheses to disambiguate which changes are due to associativity vs commutativity. a+(b+c)=(c+b)+a should be true of floating point numbers, due to commutativity. a+(b+c)=(a+b)+c may fail due to the lack of associativity.
- adastra22 1y agoIt is not, due to precision. Consider a=1.00000, b=-0.99999, and c=0.00000582618.
- zygentoma 1y agoYou still need to specify an evaluation order …
- immibis 1y agoDoes (1.00000+-0.99999)+0.00000582618 != 0.00000582618+(-0.99999+1.00000) ? This would disprove commutativity. But I think they're equal.
- jcranmer 1y agoNo, the two evaluations will give you exactly the same result: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=eb13978ce3c30a619da6a63209351a9d https://play.rust-lang.org/?version=stable&mode=debug&editio... IEEE 754 operations are nonassociative, but they are commutative (at least if you ignore the effect of NaN payloads).
- dbdr 1y agoIs there a case involving NaN where they are not commutative? Do you mean getting a different bit-level representation of NaN?
- Remnant44 1y agoIn practical use for simd, various min/max operations. On Intel at least, they propagate nan or not based on operand order
- boulos 1y agoAlso all comparisons.
- jcranmer 1y agoIEEE 754 doesn't (usually) distinguish between different NaN encodings for the purposes of semantics--if the result is a NaN, it doesn't specify which NaN the result is. Most hardware vendors implement a form of NaN propagation: when both inputs are NaN, one of the operands is returned, for example, always the left NaN is returned if both are NaN. As a side note: all compilers I'm aware of make almost no guarantees on preserving the value of NaN payloads, hence they consider floating-point operations to be fully commutative, and there's no general way to guarantee that they evaluate in exactly the order you specified.
- imtringued 1y agohttps://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=1440b547fd063bab88a7f2c2a203c8f0 https://play.rust-lang.org/?version=stable&mode=debug&editio... You're supposed to do (a+b) to demonstrate the effect, because floating point subtraction that results in a number near zero is sensitive to rounding (worst case, a non-zero number gets you a zero number), which can introduce a huge error when a and b are very similar numbers.
- jcranmer 1y agoWhen you go from (a + b) + c to a + (b + c), you're invoking the associative property, not the commutative property. The confusion between associativity and commutativity is the entire point of this thread!