HP 3000 Manuals

Expressions [ HP FORTRAN 77/iX Reference ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Reference

Expressions 

An expression can be a constant, simple or subscripted variable, function
reference, substring, scalar record field reference, or a combination of
operands, joined by arithmetic, character, logical, or relational
operators.  There are four types of expressions:

   *   Arithmetic
   *   Character
   *   Logical
   *   Relational

Arithmetic expressions return a single value of type INTEGER*2,
INTEGER*4, REAL*4, REAL*8, REAL*16, COMPLEX*8, or COMPLEX*16.  Character
expressions return character values.  Relational and logical expressions
evaluate to either true or false (a logical value).

Arithmetic Expressions 

Arithmetic expressions perform arithmetic operations.  An arithmetic
expression can consist of a single operand or of one or more operands
plus arithmetic operators, parentheses, or both.  An arithmetic operand
can be a numeric constant, the symbolic name of a numeric constant, an
array element reference, scalar record field reference, or a function
reference.  As an extension to the ANSI 77 standard, an arithmetic
operand can also be a logical variable or constant, depending upon
compiler directives, as described in Chapter 7 .

The arithmetic operators are:

         Operator              Meaning                                                                                               

           +                 Addition; unary plus (positive or plus sign)

           -                 Subtraction; unary minus (negation or minus
                             sign)

           *                 Multiplication

           /                 Division

           **                Exponentiation

A unary operator affects one operand only.  For example, the unary minus
(also called a minus sign, or sign of negation) designates the expression
following it to be negative.

The following are valid arithmetic expressions:

     a
     -4. + z
     3.145
     SQRT(r + d)
     arr(5,2)*45.5
     num(i)
     .true. .XOR. foundit
     a**2
     (c**4)*d
     total + sum_of_values
     number_of_successes/number_of_tries*100

Multiplication must be specified explicitly.  FORTRAN has no implicit
multiplication that can be indicated by a(b) or ab; the form a*b must be
used.

Hierarchy of Arithmetic Operators.   

The order of evaluation of an arithmetic expression is established by a
precedence among the operators.  This precedence determines the order in
which the operands are to be combined.  The precedence of the arithmetic
operators is:

          Operator                       Rank             Meaning 

             **                        Highest            Exponentiation

            * /                           :               Multiplication and division

            + -                         Lowest            Addition and subtraction,
                                                          unary plus and minus

Expressions within parentheses are evaluated first.  Exponentiation
precedes all arithmetic operations within an expression; multiplication
and division occur before addition and subtraction.

For example, the expression:

     -a**b + c * d + 6

is evaluated in the following order:

     a**b is evaluated to form the operand op1.

     c*d is evaluated to form the operand op2.

     -op1 + op2 + 6 is evaluated to form the result.

If an expression contains two or more operators of the same precedence,
the following rules are applied:

   *   Two or more exponentiation operations are evaluated from right to
       left.

   *   Multiplication and division or addition and subtraction are
       evaluated from left to right.

The expression:

     2**3**a

is evaluated in the following order:

     3**a is evaluated to form op1.

     2**op1 is evaluated.

The expression:

     a/b*c

is evaluated in the following order:

     a/b is evaluated to form op1.

     op1*c is evaluated.

The expression:

     i/j + c**j**d - h*d

is evaluated in the following order:

     j**d is evaluated to form op1.

     c**op1 is evaluated to form op2.

     i/j is evaluated to form op3.

     h*d is evaluated to form op4.

     op3 + op2 is evaluated to form op5.

     op5 - op4 is evaluated.

Parentheses can control the order of evaluation of an expression.  Each
pair of parentheses contains a subexpression that is evaluated according
to the rules stated above.  When parentheses are nested in an expression,
the innermost subexpression is evaluated first.

The expression:

     ((a + b)*c)**d

is evaluated in the following order:

     a+b is evaluated to form op1.

     op1*c is evaluated to form op2.

     op2**d is evaluated.

The expression:

     ((b**2 - 4*a*c)**.5)/(2*a)

is evaluated in the following order:

     The subexpression b**2 - 4*a*c is evaluated to form op1.

     op1**.5 is evaluated to form op2.

     2*a is evaluated to form op3.

     op2/op3 is evaluated.


NOTE The actual order of evaluation may be different from that shown, but the result is the same as if the described order were followed (except when referencing functions that have side effects).
Consecutive Operators. As an extension to the ANSI 77 standard, consecutive operators in arithmetic expressions are allowed if the second operator is a unary plus (+) or minus (-). The expression: A ** - B * C is evaluated in the following order: B is negated to form op1. A**op1 is evaluated to form op2. op2*C is evaluated. The expression: A + - B * - C is evaluated in the following order: C is negated to form op1. B*op1 is evaluated to form op2. op2 is negated to form op3. A+op3 is evaluated. Expressions with Mixed Operands. Integer, real, and complex operands can be intermixed freely in an arithmetic expression. As an extension to the ANSI 77 standard, logical operands can be intermixed with numeric operands. Before an arithmetic operation is performed, the lower type is converted to the higher type. The type of the expression is that of the highest type operand in the expression. Operand types rank from highest to lowest in the following order: Data Type Rank COMPLEX*16 Highest COMPLEX*8 REAL*16 REAL*8 REAL*4 INTEGER*4 INTEGER*2 LOGICAL*4 LOGICAL*2 LOGICAL*1 Lowest An exception to the above is that, if one operand is REAL*8 and the other is COMPLEX*8, the result is COMPLEX*16. Another exception is that, if one operand is REAL*16 and the other is COMPLEX*8 or COMPLEX*16, the result is COMPLEX*16. The conversion precedence for mixed type arithmetic expressions is described in Table 2-4 . For example, if a and b are real variables and i and j are integer variables, then, in the expression a*b-i/j, a is multiplied by b to form the real value op1. Next, i is divided by j with integer division to form the integer value op2. Finally, op2 is converted to real, and subtracted from op1, to produce a real result. Table 2-4. Conversion of Mixed Type Operands --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | | L*1 | L*2 | L*4 | I*2 | I*4 | R*4 | R*8 | R*16 | C*8 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | L*1 | L*1 | L*2 | L*4 | I*2 | I*4 | R*4 | R*8 | R*16 | C*8 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | L*2 | L*2 | L*2 | L*4 | I*2 | I*4 | R*4 | R*8 | R*16 | C*8 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | L*4 | L*4 | L*4 | L*4 | I*4 | I*4 | R*4 | R*8 | R*16 | C*8 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | I*2 | I*2 | I*2 | I*4 | I*2 | I*4 | R*4 | R*8 | R*16 | C*8 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | I*4 | I*4 | I*4 | I*4 | I*4 | I*4 | R*4 | R*8 | R*16 | C*8 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | R*4 | R*4 | R*4 | R*4 | R*4 | R*4 | R*4 | R*8 | R*16 | C*8 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | R*8 | R*8 | R*8 | R*8 | R*8 | R*8 | R*8 | R*8 | R*16 | C*16 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | R*16 | R*16 | R*16 | R*16 | R*16 | R*16 | R*16 | R*16 | R*16 | C*16 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | C*8 | C*8 | C*8 | C*8 | C*8 | C*8 | C*8 | C*16 | C*16 | C*8 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- | | | | | | | | | | | | | C*16 | C*16 | C*16 | C*16 | C*16 | C*16 | C*16 | C*16 | C*16 | C*16 | C*16 | | | | | | | | | | | | | --------------------------------------------------------------------------------------------------------------- Key to symbols in Table 2-4 : Symbol Stands for Data Type L*1 LOGICAL*1 L*2 LOGICAL*2 L*4 LOGICAL*4 I*2 INTEGER*2 I*4 INTEGER*4 R*4 REAL*4 R*8 REAL*8 R*16 REAL*16 C*8 COMPLEX*8 C*16 COMPLEX*16 When any value is raised to an integer power, the operation is performed by repeated multiplications. When any value is raised to a noninteger power, the operation is performed by logarithms and exponentiation. Arithmetic Constant Expressions. An arithmetic constant expression is an arithmetic expression in which each operand is an arithmetic constant, the symbolic name of an arithmetic constant, or an arithmetic constant expression enclosed in parentheses. Variables, array elements, record field references, and function references are not allowed, with the following exception: As an extension to the ANSI 77 standard, some intrinsic functions are allowed in the PARAMETER statement. Character Expressions A character expression performs character operations. Evaluation of a character expression produces a result of type CHARACTER. The simplest form of a character expression is a character constant, the symbolic name of a character constant, a character variable reference, a character array element reference, a character substring reference, a scalar record field reference of type CHARACTER, or a character function reference. More complicated character expressions can be formed by using two or more character operands together with the character operator. The character operator, concatenation, is formed by two slashes, //. Syntax c1 // c2 ----------------------------------------------------------------------------------------------- | | | | | Item | Description/Default | Restrictions | | | | | ----------------------------------------------------------------------------------------------- | | | | | c1, c2 | Character expressions, or | None. | | | character entities as described | | | | above. | | | | | | ----------------------------------------------------------------------------------------------- The result of a concatenation operation is a character string whose value is the value of c1 concatenated on the right with the value of c2. The length of the resulting string is the sum of the lengths of c1 and c2. For example, the value of 'FOOT' // "BALL" is the string 'FOOTBALL'. Parentheses have no effect on the value of a character expression. For example, the expression 'ab'//('CD'//'ef') is the same as the expression 'ab' // 'CD' //'ef' The result of either expression is 'abCDef'. Examples char_string (5:9) 'constant string' string1//string2//'another string' home//'/'//filename Character Constant Expressions. A character constant expression is a character expression in which each operand is a character constant, the symbolic name of a character constant, or a character constant expression enclosed in parentheses. Variables, substrings, array elements, record field references, and function references are not allowed, with the following exception: As an extension to the ANSI 77 standard, some intrinsic functions are allowed in the PARAMETER statement. Relational Expressions Relational expressions compare the values of two arithmetic expressions or two character expressions. Evaluation of a relational expression produces a result of type logical. Syntax op1 relop op2 ----------------------------------------------------------------------------------------------- | | | | | Item | Description/Default | Restrictions | | | | | ----------------------------------------------------------------------------------------------- | | | | | op1, op2 | Expressions | Must be either arithmetic or | | | | character | | | | | ----------------------------------------------------------------------------------------------- | | | | | relop | Relational operator | None | | | | | ----------------------------------------------------------------------------------------------- The relational operators are: Operator Meaning .EQ. Equal .NE. Not equal .LT. Less than .LE. Less than or equal .GT. Greater than .GE. Greater than or equal Each relational expression is evaluated and assigned the logical value true or false depending on whether the relation between the two operands is satisfied (true) or not (false).
NOTE Aggregate record references are not permitted in relational expressions.
Arithmetic Relational Expressions. Arithmetic expressions used as operands in a relational expression are evaluated according to the previously defined rules for arithmetic expressions. If the expressions are of different types, the one with the lower rank is converted to the higher ranking type as specified in Table 2-4 . Once the expressions are evaluated and converted to the same type, they are compared. An arithmetic relational expression is interpreted as having the logical value true if the values of the operands satisfy the relation specified by the operator. If the operands do not satisfy the specified relation, the expression is interpreted as the logical value false. The following are valid arithmetic relational expressions: a .GT. 237 a + b - c .LT. num i + j .GE. z + 1 o .GT. p Expressions of complex data types can be used as operands with .EQ. and .NE. relational operators only. The concept of less than or greater than is not defined for complex numbers. Character Relational Expressions. Character relational expressions compare two operands, each of which is a character expression. The character expressions are evaluated; then the two operands are compared character by character, starting from the left. The initial characters of the two operands are first compared. If the initial character is the same in both operands, the comparison proceeds with the second character of each operand. When unequal characters are encountered, the greater of these two characters in ASCII value is the greater operand. Therefore, the ranking of the operands is determined by the first character position at which the two operands differ. If the operands do not differ at any position, the two operands are equal. For example, when the two expressions 'PEOPLE'and 'PEPPER' are compared, the first expression is considered less than the second. This is determined by the third character O, which is less than P in the ASCII collating sequence. Refer to Appendix D for the ASCII collating sequence. If the operands are of unequal length, the comparison is made as if the shorter string was padded with blanks on the right to the length of the longer string. Examples Notes --------------------------------------------------------------------------------------- IMPLICIT CHARACTER*6 (a-n) All variables beginning with the letters a to n are CHARACTER type. 'the' .LT.'there' 'MAY 23' .GT. 'MAY 21' name .LE. 'PETERSEN' char_stri .GE. char_str2 first .EQ. a_string(2:8) Logical Expressions Logical expressions produce results of type logical with values of true or false. A logical expression can consist of a single operand or one or more operands plus a logical operator. A logical operand can be a logical constant, the symbolic name of a logical constant, a logical variable, a logical array element reference, a scalar record field reference of logical type, or a relational expression. As an extension to the ANSI 77 standard, integer variables or constants can also be used as logical operands, depending upon compiler directives. Refer to chapter 8 for further information. The logical operators are: Operator Meaning .NOT. Logical negation (unary) .AND. Logical AND .OR. Logical inclusive OR .EQV. Logical equivalence .NEQV. Logical nonequivalence (same as .XOR.) .XOR. Logical exclusive OR (same as .NEQV.) The unary operator .NOT. gives the complement (that is, the opposite) of the logical value of the operand immediately following the .NOT. operator. The .AND. operator returns a value of true only if the logical operands on both sides of the .AND. operator evaluate to true. The .OR. operator returns a value of true if one or both of the logical operands on either side of the .OR. operator are true. The .NEQV. and .XOR. operators return a value of true only if one (but not both) of the logical operands on either side of the operator is true. As an extension to the ANSI 77 standard, .XOR. can be used in place of .NEQV. The .EQV. operator returns a value of true if the logical operands on either side of the .EQV. operator are both true or both false. Table 2-5 is a truth table for the logical operators. Table 2-5. Truth Table for Logical Operators ---------------------------------------------------------------------------------------------------------- | | | | | | | | | a | b | .NOT. a | a .AND. b | a .OR. b | a .NEQV. b | a .EQV. b | | | | | | | a .XOR. b | | | | | | | | | | ---------------------------------------------------------------------------------------------------------- | | | | | | | | | True | True | False | True | True | False | True | | | | | | | | | ---------------------------------------------------------------------------------------------------------- | | | | | | | | | True | False | False | False | True | True | False | | | | | | | | | ---------------------------------------------------------------------------------------------------------- | | | | | | | | | False | True | True | False | True | True | False | | | | | | | | | ---------------------------------------------------------------------------------------------------------- | | | | | | | | | False | False | True | False | False | False | True | | | | | | | | | ---------------------------------------------------------------------------------------------------------- The order of evaluation of a logical expression is established by the following precedence of the logical operators: .NOT. highest .AND. .OR. .EQV. .NEQV. lowest .XOR. If there is more than one operator of the same precedence, evaluation occurs from left to right. Examples The expression: a .OR. b .AND. c is evaluated in the following order: b .AND. c is evaluated to form lop1. a .OR. lop1 is evaluated. The expression: z .LT. b .OR. .NOT. k .GT. z is evaluated as follows: k .GT. z is evaluated to form lop1. .NOT. lop1 is evaluated to form lop2. z .LT. b is evaluated to form lop3. lop3 .OR. lop2 is evaluated. The expression: z .AND. d .OR. lsum(q,d) .AND. p .AND. i is evaluated in the following order: z .AND. d is evaluated to form lop1. lsum(q,d) is evaluated to form lop2. lop2 .AND. p is evaluated to form lop3. lop3 .AND. i is evaluated to form lop4. lop1 .OR. lop4 is evaluated. The expression: a .AND. (b .AND. c) is evaluated in the following order: b .AND. c is evaluated to form lop1. a .AND. lop1 is evaluated. As shown in the last example, parentheses can be used to control the order of evaluation of a logical expression. As with arithmetic expressions, the actual order of evaluation may be different from that stated above, but the result is the same as if these rules were followed. Bit Masking Expressions As an extension to the ANSI 77 standard, the logical operators can be used with integer operands to perform bit masking operations. You must be aware of the internal binary representations of the data in order to use the masking operators to produce predictable results. (Refer to Chapter 10 for details on data representation in memory.) A complete truth table is shown in Table 2-6 (a version of Table 2-5 with true = 1 and false = 0). A bit by bit comparison is done of the operands (i and j), and the corresponding bit of the result is set according to the truth table. FORTRAN also supplies these bit masking operations and other bit manipulation operations as intrinsic functions, described in Appendix B . These functions comply with the MIL-STD-1753 extension to the ANSI 77 standard. Table 2-6. Truth Table for Masking Operators ---------------------------------------------------------------------------------------------------------- | | | | | | | | | i | j | .NOT. i | i .AND. j | i .OR. j | i .NEQV. j | i .EQV. j | | | | | | | i .XOR. j | | | | | | | | | | ---------------------------------------------------------------------------------------------------------- | | | | | | | | | 1 | 1 | 0 | 1 | 1 | 0 | 1 | | | | | | | | | ---------------------------------------------------------------------------------------------------------- | | | | | | | | | 1 | 0 | 0 | 0 | 1 | 1 | 0 | | | | | | | | | ---------------------------------------------------------------------------------------------------------- | | | | | | | | | 0 | 1 | 1 | 0 | 1 | 1 | 0 | | | | | | | | | ---------------------------------------------------------------------------------------------------------- | | | | | | | | | 0 | 0 | 1 | 0 | 0 | 0 | 1 | | | | | | | | | ---------------------------------------------------------------------------------------------------------- Examples .AND. returns the logical product of two operands: op1: 0111111111111110 (3276610) op2: 0001011001011001 (572110) result: 0001011001011000 (572010) .NEQV. and .XOR. return the symmetric difference of two operands: op1: 0000000011111111 (25510) op2: 0001011001011001 (572110) result: 0001011010100110 (579810)


MPE/iX 5.0 Documentation