[comp.lang.c] A simple solution to force evaluation order on non-ANSI compilers.

rbutterworth@orchid.UUCP (04/29/87)

For those people that are justifiably concerned about order of
evaluation of floating point expressions, and don't want the
compiler to optimize the expression, and don't want to have
to resort to asigning temporaries, there is a simple solution.
Put the following function into your library:

        double
    evaluate(value)
        double value;
    {
        return value;
    }

Then you can write your expressions as

    x = 42.000 + evaluate( evaluate(y*3) + evaluate(z+83.7) );

This will guarantee that "y" is multiplied by 3, and "z" is added
to 83.7, and that the two results are added together, and that
that result is added to 42.  It of course doesn't guarantee
anything about whether the "y*" or the "z+" will happen first.

(I said this was simple, I didn't say it was efficient.)

If you move your code to an ANSI compiler, you can scrap the
evaluate() function, and get the efficiency back with
#define evaluate(value) (+(value))