Using distributive property to optimize.

Robert D. Silverman bs at linus.UUCP
Wed Feb 7 03:51:35 AEST 1990


In article <1020.nlhp3 at oracle.nl> bengsig at oracle.nl (Bjorn Engsig) writes:
:Article <229 at altos86.Altos.COM> by clp at altos86.Altos.COM (Chuck L. Peterson) says:
:[is it valid to change]
:|	n = a - (b + c);
:|To this:
:|	n = a - b - c;
:It was valid according to K&R 1.  It's not in ANSI.
 
Nor should it be. Floating point addition is NOT associative. Presumably
the programmer has a reason to group the computation the way he did.

Note also that even with integer arithmetic, the rearrangement is not
safe. If (say) b is a large negative number and c a large positive
number a - (b+c) may not overflow. However, a-b most certainly can.

Compilers should NEVER rearrange computations.

-- 
Bob Silverman
#include <std.disclaimer>
Internet: bs at linus.mitre.org; UUCP: {decvax,philabs}!linus!bs
Mitre Corporation, Bedford, MA 01730



More information about the Comp.lang.c mailing list