📄 fexpr.gml
字号:
.chap *refid=fexpr Expressions
.*
.np
The following topics are discussed in this chapter.
.begbull
.bull
Arithmetic Expressions
.bull
Character Expressions
.bull
Relational Expressions
.bull
Logical Expressions
.bull
Evaluating Expressions
.bull
Constant Expressions
.endbull
.*
.section Arithmetic Expressions
.*
.np
Arithmetic expressions are used to describe computations
involving operands with numeric data type, arithmetic operators
and left and right parentheses.
The result of the computation is of numeric data type.
.*
.beglevel
.*
.section Arithmetic Operators
.*
.np
The following table lists the arithmetic operators and the
operation they perform.
.* .mbox on 1 12 40
.sr c0=&INDlvl+1
.sr c1=&INDlvl+12
.sr c2=&INDlvl+40
.mbox on &c0 &c1 &c2
Operator Arithmetic Operation
.mbox
** Exponentiation
/ Division
* Multiplication
- Subtraction or Negation
+ Addition or Identity
.mbox off
.np
Some operators can be either binary or unary.
.ix 'binary operator'
.ix operator binary
A
.us binary operator
is one that requires two operands.
.ix 'unary operator'
.ix operator unary
A
.us unary operator
is one that requires one operand.
Each of the operators **, /, and * are binary operators.
The operators + and &minus. can either be binary or unary operators.
The following table describes how each operator is used with their
operands.
.sr c0=&INDlvl+1
.sr c1=&INDlvl+12
.sr c2=&INDlvl+40
.mbox on &c0 &c1 &c2
Operator Arithmetic Operation
.mbox
x ** y x is raised to the power y
x / y x is divided by y
x * y x is multiplied by y
x - y y is subtracted from x
x + y y is added to x
- x x is negated
+ x identity
.mbox off
.np
Arithmetic expressions can contain more than one operator.
It is thus necessary to define rules of evaluation for such expressions.
.ix 'arithmetic operators' precedence
.ix operator precedence
A
.us precedence relation
is defined between operators.
This relation defines the order in which operands are combined and
hence describes the evaluation sequence of an arithmetic expression.
Operands of higher precedence operators are combined using that
operator to form an operand for an operator of lower precedence.
The following rules define the precedence relation among arithmetic
operators.
.autopoint
.point
Exponentiation
.mono (**)
has highest precedence.
.point
Multiplication
.mono (*)
and division (/) have equal precedence but have lower precedence than
exponentiation.
.point
Addition (+) and subtraction (&minus.) have equal precedence but have
lower precedence than multiplication and division.
.endpoint
.np
For example, to evaluate the expression
.millust begin
A-B**4
.millust end
.pc
.id B
is raised to the exponent 4 first and the result is then subtracted from
.id A.
.np
Parentheses can be used to alter the evaluation sequence of an
arithmetic expression.
When a left parenthesis is encountered, the entire expression enclosed
in parentheses is evaluated.
Consider the following expression.
.millust begin
3*(4+5)
.millust end
.pc
We first evaluate the expression in the parentheses, the result being 9.
We now multiply the result by 3 giving a final result of 27.
Now suppose we remove the parentheses.
According to the precedence rules,
.mono *
has precedence over + so we perform the multiplication before the
addition.
The result in this case is 17.
.*
.section Rules for Forming Standard Arithmetic Expressions
.*
.np
.ix 'arithmetic expression' 'primary'
.ix expression primary
The building blocks for arithmetic expressions are called
.us arithmetic primaries.
They are one of the following:
.autopoint
.point
unsigned arithmetic constant
.point
arithmetic symbolic constant
.point
arithmetic variable reference
.point
arithmetic array element reference
.point
arithmetic function reference
.point
( arithmetic expression )
.endpoint
.pc
A grammar for forming arithmetic expressions can be described which
reflects the precedence relation among arithmetic operators.
.np
Exponentiation has highest precedence.
.ix 'arithmetic expression' factor
.ix expression factor
We define a
.us factor
as:
.autopoint
.point
primary
.point
primary ** factor
.endpoint
.pc
A factor is simply a sequence of primaries, each separated by the
exponentiation operator.
Rule (2) specifies that the primaries involving exponentiation
operators are combined from right to left
when evaluating a factor.
.np
Next in the precedence hierarchy are the multiplication and division
operators.
.ix 'arithmetic expression' term
.ix expression term
We define a
.us term
as:
.autopoint
.point
factor
.point
term / factor
.point
term * factor
.endpoint
.pc
A term is simply a sequence of factors, each separated by a
multiplication operator or a division operator.
Rules (2) and (3) imply that in such a sequence, factors are combined
from left to right when evaluating a term.
Factors can be interpreted as the result obtained from evaluating
them.
This implies that all factors are evaluated before any of the
multiplication or division operands are combined.
This interpretation is consistent with the precedence relation
between the exponentiation operator and the division and
multiplication operators.
.np
.ix 'arithmetic expression'
.ix expression arithmetic
An
.us arithmetic expression
can now be defined as follows.
.autopoint
.point
term
.point
+ term
.point
&minus. term
.point
arithmetic expression + term
.point
arithmetic expression &minus. term
.endpoint
.pc
An arithmetic expression is simply a sequence of terms, each separated
by an addition operator or a subtraction operator.
Rules (4) and (5) imply that terms are evaluated from left to right.
Rules (2) and (3) imply that only the first term of an arithmetic
expression can be preceded by a unary + or &minus. operator.
Terms can be interpreted in the same way as factors were interpreted
in the definition of terms.
.np
Note that consecutive operators are not permitted.
For example, the expression
.millust begin
A+-B
.millust end
.pc
is illegal.
However, expressions of the form
.millust begin
A+(-B)
.millust end
.pc
are allowed.
.*
.section Arithmetic Constant Expression
.*
.np
.ix 'arithmetic constant expression'
.ix expression 'arithmetic constant'
An
.us arithmetic constant expression
is an arithmetic expression in which all primaries are one of the
following.
.autopoint
.point
arithmetic constant
.point
symbolic arithmetic constant
.point
( arithmetic constant expression )
.endpoint
.pc
There is a further restriction with the exponentiation operator;
the exponent must be of type INTEGER.
.np
.xt begin
.ix 'intrinsic function' ISIZEOF
.ix ISIZEOF
As an extension to the FORTRAN 77 language, &product supports the use
of the intrinsic function
.kw ISIZEOF
in an arithmetic constant expression.
.exam begin
PARAMETER (INTSIZ = ISIZEOF(INTEGER))
.exam end
.xt end
.np
.ix 'integer constant expression'
.ix expression 'integer constant'
An
.us integer constant expression
is an arithmetic constant expression in which all constants and
symbolic constants are of type INTEGER.
.exam begin
123
-753+2
-(12*13)
.exam end
.np
.ix 'real constant expression'
.ix expression 'real constant'
A
.us real constant expression
is an arithmetic constant expression in which at least one
constant or symbolic constant is of type REAL and all other
constants or symbolic constants are of type REAL or INTEGER.
.exam begin
123.
-753+2.0
-(13E0*12)
.exam end
.np
.ix 'double precision constant expression'
.ix expression 'double precision constant'
A
.us double precision constant expression
is an arithmetic constant expression in which at least one constant or
symbolic constant is of type DOUBLE PRECISION and all other constants
or symbolic constants are of type DOUBLE PRECISION, REAL or INTEGER.
.exam begin
123.4D0
-753D0*2+.5
-(12D0*12.2)
.exam end
.np
.ix 'complex constant expression'
.ix expression 'complex constant'
A
.us complex constant expression
is an arithmetic constant expression in which at least one
constant or symbolic constant is of type COMPLEX and all other
constants or symbolic constants are of type COMPLEX, REAL or
INTEGER.
.exam begin
(123,0)
(-753,12.3)*2
-(12,-12.4)-(1.0,.2)
.exam end
.np
.ix 'double precision complex constant expression'
.ix expression 'double precision complex constant'
.xt begin
A
.us double precision complex constant expression
is an arithmetic constant expression in which at least one constant or
symbolic constant is of type COMPLEX*16 and all other constants or
symbolic constants are of type COMPLEX*16, DOUBLE PRECISION, REAL or
INTEGER.
If there are no constants or symbolic constants of type COMPLEX*16 in
a constant expression, the type of the constant expression will be
COMPLEX*16 if it contains at least one constant or symbolic constant
of type COMPLEX and at least one constant or symbolic constant of type
DOUBLE PRECISION.
&product supports this type of constant expression as an extension of
the FORTRAN 77 language.
.xt end
.keep
.xt begin
.exam begin
(123,0D0)
(-753,12.3D0)*2
-(12D0,-12.4)-(1.0,.2)
.exam end
.xt end
.*
.section Data Type of Arithmetic Expressions
.*
.np
Evaluating an arithmetic expression produces a result which has a
type.
The type of the result is determined by the type of its operands.
.ix 'arithmetic expression' 'type of'
The following table describes the rules for determining the type of
arithmetic expressions.
The letters I, R, D, C and Z stand for INTEGER, REAL, DOUBLE
PRECISION, COMPLEX
.xt and COMPLEX*16
respectively.
An entry in the table represents the data type of the result when the
operands are of the type indicated by the row and column in which the
entry belongs.
The column represents the type of the operand to the right of the
operator, and the row represents the type of the operand to the left
of the operator.
The table is valid for all of the arithmetic operators.
.if &e'&dohelp eq 0 .do begin
.* .box on 4 8 12 16 21 25 29 33 37 41
.sr c0=&INDlvl+1
.sr c1=&INDlvl+5
.sr c2=&INDlvl+9
.sr c3=&INDlvl+13
.sr c4=&INDlvl+18
.sr c5=&INDlvl+22
.sr c6=&INDlvl+26
.sr c7=&INDlvl+30
.sr c8=&INDlvl+34
.sr c9=&INDlvl+38
.cp 15
.box on &c0 &c1 &c2 &c3 &c4 &c5 &c6 &c7 &c8 &c9
\ op \ I*1 \ I*2 \ I*4 \ R \ D \ C \ Z \
.box
\ I*1 \ I*1 \ I*2 \ I*4 \ R \ D \ C \ Z \
\ I*2 \ I*2 \ I*2 \ I*4 \ R \ D \ C \ Z \
\ I*4 \ I*4 \ I*4 \ I*4 \ R \ D \ C \ Z \
\ R \ R \ R \ R \ R \ D \ C \ Z \
\ D \ D \ D \ D \ D \ D \ Z \ Z \
\ C \ C \ C \ C \ C \ Z \ C \ Z \
\ Z \ Z \ Z \ Z \ Z \ Z \ Z \ Z \
.box off
.do end
.el .do begin
.millust begin
+-------+-------+-------+-------+-------+-------+-------+-------+
| op | I*1 | I*2 | I*4 | R | D | C | Z |
+-------+-------+-------+-------+-------+-------+-------+-------+
| I*1 | I*1 | I*2 | I*4 | R | D | C | Z |
| I*2 | I*2 | I*2 | I*4 | R | D | C | Z |
| I*4 | I*4 | I*4 | I*4 | R | D | C | Z |
| R | R | R | R | R | D | C | Z |
| D | D | D | D | D | D | Z | Z |
| C | C | C | C | C | Z | C | Z |
| Z | Z | Z | Z | Z | Z | Z | Z |
+-------+-------+-------+-------+-------+-------+-------+-------+
.millust end
.do end
.autonote Notes:
.setptnt 0 5
.note
I*1 represents the
.xt on
.mono INTEGER*1
.xt off
data type,
I*2 represents the
.xt on
.mono INTEGER*2
.xt off
data type, and
I*4 represents the
.mono INTEGER
.xt on
or
.mono INTEGER*4
.xt off
data type.
.note
The data type of the result obtained by dividing an integer datum by
an integer datum is also of type INTEGER even though the mathematical
result may not be an integer.
.ix 'integer quotient'
This result is called the
.us integer quotient
and is defined as the integer part of the mathematical quotient.
.note
.xt on
&product supports the double precision complex data type (COMPLEX*16)
as an extension of the FORTRAN 77 language.
Combining an operand of type DOUBLE PRECISION with an operand of type
COMPLEX yields a result of type COMPLEX*16.
.xt off
.* .note
.* Operands of type INTEGER*1 and INTEGER*2 are converted to type INTEGER
.* prior to performing the operation.
.* Note that an expression can never have type INTEGER*1 or INTEGER*2.
.endnote
.endlevel
.*
.section Character Expressions
.*
.np
Character expressions are used to describe computations involving
operands of type CHARACTER, the concatenation operator (//) and left
and right parentheses.
The result of the computation is of type CHARACTER.
.*
.beglevel
.*
.section Character Operators
.*
.np
There is only one character operator, namely the concatenation
operator (//).
It requires two operands of type CHARACTER.
If
.id x
is the left operand and
.id y
is the right operand, then the result is
.id y
concatenated to
.id x.
The length of the result is the sum of the lengths of the two
operands.
For example, the result of
.millust begin
'AAAAA'//'BBB'
.millust end
.pc
is the string
.mono AAAAABBB.
.*
.section Rules for Forming Character Expressions
.*
.np
.ix 'character expression' primaries
The building blocks for character expressions are called
.us character primaries.
They are one of the following.
.autopoint
.point
character constant
.point
character symbolic constant
.point
character variable reference
.point
character array element reference
.point
character substring reference
.point
character function reference
.point
( character expression )
.endpoint
.pc
.ix 'character expression'
.us Character expressions
are defined as follows:
.autopoint
.point
character primary
.point
character expression // character primary
.endpoint
.pc
A character expression is simply a sequence of character primaries,
each separated by the concatenation operator (//).
Rule 2 implies that character primaries are combined from left to
right.
Except in a character assignment statement, the operands in a
character expression must not contain operands whose length
specification is (*) unless the operand is a symbolic constant.
.np
Note that, unlike arithmetic expressions, parentheses have no effect
on the result of evaluating a character expression.
For example, the result of the expressions
.millust begin
'A'//'B'//'C'
.millust end
.pc
and
.millust begin
'A'//('B'//'C')
.millust end
.pc
is identically the string
.mono ABC.
.*
.section Character Constant Expressions
.*
.np
.ix 'character constant expression'
A
.us character constant expression
is a character expression in which all primaries are one of the
following.
.autopoint
.point
character constant
.point
symbolic character constant
.point
( character constant expression )
.endpoint
.np
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -