« previous next»

2.4    Operations and Logical Expressions
 

OPERATIONS

    There are various arithmetic operations that can be used with numeric data.  They are +, -, *, /, and ** (exponential).  The order of priority for these operations are essentially the same as typical mathematical rules:

    1.    Exponents are performed first, from right to left.
    2.    Multiplications and divisions are performed next, from left to right.
    3.    Addition and subtraction are last.  They are also directed from left to right.

Of course, you can use parentheses to enclose subexpressions.   Subexpressions are evaluated first according to the previous priority rules.

    4 * 5 * 2 - 3 ** 2 = 40 - 9 = 31
    4 * (5 * 2 - 3) ** 2 = 4 * 7 ** 2 = 4 * 49 = 196

    A concern with using arithmetic operations is the use of mixed-mode expressions (i.e. combining real and integer quantities).  Mixed-mode expressions should typically be avoided as it is considered bad programming.  When an integer quantity is combined with a real quantity, the integer is converted to its real equivalent and the expression is then evaluated.  For example:

    3 + 4.2 = 7.2
    5.0 / 10 = 0.5

However, this can lead to problems because the conversion only occurs when the real quantity is encountered in the sequence of operations.  the following gives an example of the problems that may occur with mixed-mode expressions.

    3.2 + 9.0 / 5 = 5.0
    3.2 + 9 / 5 = 4.2

The value for 9 / 5 is 1 because it is the division of two integers and the fractional part is truncated.

    Another problem can occur when using real numbers and exponents.  Real exponents should never be used in place of integer exponents.  2.0 ** 3 is evaluated as 2.0 * 2.0 * 2.0 which is 8.0.  However, if the exponent is real it is performed using logarithms:

    2.0 ** 3.0 = e3.0 ln (2.0)

This will not be 8.0 because of roundoff errors.  This also causes problems when raising a negative number to a real exponent.  A negative logarithm is undefined.  (-2.0) ** 2.0 is undefined, while (-2.0) ** 2 is 4.0.    Real exponents are ok to use, but only when integer values cannot be used in its place.
 

LOGICAL EXPRESSIONS

    Logical expressions are expressions that result in either logical constants (.TRUE. or .FALSE.) or logical variables (variables that can take the values of .TRUE. or .FALSE.).  Simple logical expressions consist of the following:

         SYMBOL         MEANING
            <   or .LT.           Is less than
            >   or .GT.           Is greater than
            == or .EQ.           Is equal to
            <= or .LE.           Is less than or equal to
            >= or .GE.           Is greater than or equal to
            /=  or .NE.           Is not equal to

The following expressions can be evaluated with logical results (.TRUE. or .FALSE.)

    A .NE. 6
    C * A >= 3

It is important to realize that many real numbers are not stored exactly; so, be cautious when comparing real values using == or /=.  These operators may result in a .FALSE. result due to precision errors.  It is usually better to use a range of threshold values when analyzing real numbers.

    Character variables can also be compared logically.  Characters are ranked in alphabetic order (with a blank ranked ahead of "a") beginning with the first character.  All of the following statements are true:

    "A" > "B"
    "better" > "between"
    "good year" > "goodyear"

    Compound logical expressions are formed using one of the following operators applied to simple logical expressions:

If exp1 and exp2 are simple logical expression then

 

« previous next»