Evaluation of Expressions:

In evaluating expressions we must follow these rules:

1) Evaluate parenthesized subexpressions first ( in case of nested parentheses start with the innermost first)
2) Perform operations according to the precedence:
    a) unary + or unary –
    b) binary * or / or %
    c) binary + or binary –
3) If there is a sequence of two or more binary operators of the same precedence, evaluate them left to right.

For example calculating the expression:

y = 2.45 + ( 5.6 + 7 * x - (3.0 / 7.0 ) * pow( x , 2 ) ) / ( 3.5 + x ) ;

for x = 1.2, we must first replace the 3.0 / 7.0 by 0.428571, then pow(x,2) by 1.44 and we have:


y = 2.45 + ( 5.6 + 7 * x - 0.428571 * 1.44) / ( 3.5 + x );

then we replace 7 * x by 8.4 and 0.428571 * 1.44 by 0.617142 and we have:

y = 2.45 + ( 5.6 + 8.4 – 0.617142 ) / ( 3.5 + x ) ;

then we replace 5.6 + 8.4 by 14 and we have

y = 2.45 + ( 14 – 0.617142) / ( 3.5 + x ) ; then we replace 14 – 0.617142 by 13.3829 and 3.5 + x by 4.7 so we have:

y = 2.45 + 13.3829 / 4.7;

which results in y = 2.45 + 2.84743 and finally y = 5.29743

Jacob Y. Kazakia © 2001 All rights reserved