Re: Conflicts between C standard and 754-2008
As everyone else has noted, Hossam Fahmy is asking about associativity (which is not allowed), not commutativity (which is allowed), so there's not conflict there.
Michel is correct that FP addition (or multiplication) may be non-commutative in the presence of NaNs -- specifically, if you have two NaNs with differing payloads, the result of a*b or a+b my differ from that of b*a or b+a. This does not cause a conflict with 754 either, however.
The clause of 754 at work is 6.2.3 NaN propagation:
"If two or more inputs are NaN, then the payload of the resulting NaN should be identical to the payload of one of the input NaNs if representable in the destination format. This standard does not specify which of the input NaNs will provide the payload."
So (1) we're in a should clause, and (2) even if it were a shall clause, 754 still wouldn't specify *which* NaN payload propagates, so even on an implementation that always propagated the payload of the first argument, (a+b) --> (b+a) would be allowed.
- Steve
On Jan 6, 2011, at 5:20 AM, Michel Hack wrote:
> Hossam Fahmy wrote:
>> and without any parenthesis I assumed that the evaluation will always
>> proceed from left to right and I swapped the second and third term.
>> So we are getting:
>> (z-y)+x = 100
>> and
>> (z+x)-y=0
>
> Without associativity there is no such thing as a "third term", so you
> can't swap 2nd and 3rd terms, and the cited optimization rule does not
> apply.
>
> What would be allowed is replacing z-y+x with x+(z-y).
>
> In any case, in the presence of NaNs FP addition may be non-commutative.
>
> Michel.
> ---Sent: 2011-01-06 13:25:41 UTC