• Aucun résultat trouvé

ARITHMETIC EXPRESSIONS

Dans le document VS FORTRAN (Page 71-75)

You can use arithmetic expressions to specify mathematical rei a t ion s hip s 0 f va r; 0 us kin d s. The val; dar i t hm e tic

express;ons, and how you can combine them, are shown in Figure 12.

Specify all desired computations explicitly, and make certain that no two arithmetic operators appear consecutively.

In addition, be aware that the compiler evaluates arithmetic expressions as follows:

• Operands are evaluated in their order of precedence from highest to lowest.

• -Within the exponentiation precedence leve~, operations are performed right to left. For example, A**B**C is evaluated as (A**(B**C».

• Within all other precedence levels, operations are performed left to right. For example, A+B+C is evaluated as «A+B)+C).

• Parentheses are evaluated as they are in mathematics; they specify the order in which operations are to be performed.

That is, expressions within parentheses are evaluated before the result is used. For example, the expression (CA-B)+C)*E is evaluated as follows:

1. A-B is evaluated, giving resultl

2. resultl+C is evaluated, giving result2

3. result2*E is evaluated, giving the final result

You can use the + and - operators as si gns for an item; when you use them this way, they're evaluated as if they were addition or subtraction operators. That is, A=-B+C is evaluated as"though written A=-CB)+C.

Coding Your Program--Advanced Programming 53

The following EQUIVALENCE statement (where A 1S REAL*4, I is INTEGER*4, and A2 1S DOUBLE PRECISION):

DIMENSION A(10),I(16),A2(5) EQUIVALENCE (A(1),I(7),A2(1»

causes the items to be laid out in storage 8S follows:

Displacement:

o

~ 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64

1 1 1 1 1 1 1 1 1 1 1 1 1 I I 1 1

I array 1(7)

1---1---1---1---1---1---1---1---1---1---1---1---1---1---1---1---1

A ( , 1

A array I~~:i---I---I---I---I---I---I---I---I---I A2(1)

A2 array

1---1---1---1---1---1

(Efficiently laid out--A and A2 begin at a displacement of 24 storage positions from the beginning of the equivalence group)

The following EQUIVALENCE statement (using the same items):

DIMENSION A(10),I(16),A2(5) EQUIVALENCE (A(I),I(6),A2(1»

causes the items to be laid out in storage as follows:

Displacement:

o

4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64

1 I I I 1 I I I 1 I I I I I I 1 1

I array 1(6)

1---1---1---1---1---1---1---1---1---1---1---1---1---1---1---1---1

A array A2array

A(l)

1---1---1---1---1---1---1---1---1---1---1

A2(l)

1---1---1---1---1---1

(Inefficiently laid out--A2 begins at a displacement of 20--not divisible by 8--from the beginning of the equivalence group)

Figure 11. Storage Efficiency and the EQUIVALENCE Statement

54 VS FORTRAN Application Programming: Guide

0)

"·c:'

/'

(,0

(0

Arithtr.et;c

Operation Operator Precedence

Function Evaluation (none) Highest

Exponentiation ** Second

Highest

Multiplication * Third

]

Highest

Division /

Addition +

]

Lowest Subtraction

Figure 12. Arithmetic Operators and Operations

In arithmetic expressions, you can specify the operands as any mix of integer, real, or complex items.

However, you should be careful, when you're specifying

arithmetic expressions, that you define the operands so that you get the precision you want in the result. For example, if you specify:

DOUBLE PRECISION RESULT RESULT = AR3*AR1

AR3 and AR1 (both REAL *4 numbers) are multiplied together, the REAL *4 result is padded with zeros and placed in RESULT. Thus, whi Ie the RESUL Tis the length of a DOUBLE PRECISION item, it has only the precision obtainable using REAL*4 operands

(approximately 7.2 decimal digits).

If you're dividing one operand by another, you should define the operands and the result as REAL items.

If you divide one integer by another, any remainder is ignored, and you get an integer quotient:

DATA 11/10/,12/15/

RESULT=I2/I1

In this case, the expression on the right of the equal sign is evaluated to the integer 1; then the result is converted to a floating point number and stored in RESULT.

You can also perform more complex mathematical operations by using the intrinsic functions that VS FORTRAN provides; see

"Saving Coding Effort with statenlit~nt Functions" for details.

Reference documentation for arithmetic expressions is given in the VS FORTRAN Application Programming: Language Reference manual.

CHARACTER EXPRESSIONS

You specify character expressions by combinations of character items, the character concatenation operator, and optional parentheses.

Coding Your Program--Advanced Programming 55

The simplest form of character expression is simply a character item itself.

You can combine character operands by using the concatenation operator (II) to join one operand to the next. For example:

CHARACTER *12 CHAR

CHARACTER *6 CHAR1,CHAR2

DATA CHAR1/'ABCDEF'I,CHAR2/'GHIJKl'l CHAR

=

CHARIIICHAR2

The concatenation operator (II) specifies that the contents of CHAR2 are to be joined to those of CHAR1 to form the character string:

ABCDEFGHIJKl

which is then assigned to the character variable CHAR.

Reference documentation for charact~r expressions is given in the VS FORTRAN Application Programming: language Reference manual.

RELATIONAL EXPRESSIONS

You can form relational expressions by combining two arithmetic expressions with a relational operator,' or by combining two character expressions with a relational operator.

The relational operators you can use are listed in Figure 13.

Relational

operator Heaning

.GT.

• L

r .

.GE.

.lE.

.EQ.

.NE.

greater than less than

greater than or equal to less than or equal to equal to

not equal to

Figure 13. Relational Operators and their Meanings

You can combine expressions as shown in the following examples:

A.GE.B

If A is greater than or equal to B, this relational expression is evaluated as "true"; otherwise, it is evaluated as "false."

(A+B).lT.(C-B)

If the result of A+B is less than the result of C-B, this

relational expression is evaluated as "true"; otherwise, it is evaluated as "false."

COMPLEX CMPlXl,CMPlX2 (CMPlXl-2).EQ.(CMPlX2+2)

If the result of CMPlXl-l is equal to the result of CMPlX2+2, this relational expression is evaluated as "true"; otherwise, it is evaluated as "false." (The only relational operators you can use with complex arithmetic operands are .EQ. and .NE.)

CHARACTER *5 CHAR4, CHAR5, CHAR6*8 (CHAR41ICHAR5).GT.CHAR6

56 VS FORTRAN Application Programming: Guide

o

o

In this Qxpression, CHAR6 is extended 2 characters to the right with blank characters; CHAR6 is then compared with the

concatenation of CHAR4 and CHARS, according to the EBCDIC collating sequence. If the concatenation of CHAR4 and CHARS evaluates as greater than CHAR6, this relational expression is evaluated as "true"; otherw i se, it i s eva luated as "fal se."

Dans le document VS FORTRAN (Page 71-75)