• Aucun résultat trouvé

DATA TYPE OF ARITHMETIC EXPRESSIONS

Dans le document CRAY® COMPUTER SYSTEMS (Page 172-177)

EXPRESSIONS AND ASSIGNMENT

5.1 ARITHMETIC EXPRESSIONS

5.1.4 DATA TYPE OF ARITHMETIC EXPRESSIONS

The data type of an arithmetic expression containing one or more

arithmetic operators is determined from the data types of the operands.

The data types of arithmetic expressions are given in table 5-3, and those for exponentiation in table 5-4. Each table item represents an expression and a result, and each capital letter represents an operand or result, as follows:

I Integer B Boolean

R Real

L Logical

D Double-precision S Character

C Complex P Pointer In table 5-3, an entry is similar in form to an assignment statement, but also applies to expressions within parentheses. The 0 symbol represents an arithmetic operator. If an arrow is shown, it points to the result type; a square symbol with no arrow indicates the two operands cannot be used in an arithmetic expression. In each row and column, one operand has the same data type throughout, and the other operand changes; the

unchanging operand is indicated in the column head and at the beginning of each row.

Example:

R~IoR

To the right of the arrow, 0 is an operator and I and R are operands of types integer and real. To the left of the arrow, R indicates a real result; the underscored I indicates that integers in such an expression are converted to type real. The preceding entry would apply to the following expression:

CTEMP * 9/5 + 32

This would be treated as CTEMP * 9./5. + 32.

Note that CTEMP+(9/5)+32 is treated as CTEMP+l.+32.

When + or - operates on a single operand, the data type of the resulting expression is the same as the data type of the operand.

In general, conversion of data types is determined by a hierarchy of types, and is always upward within the hierarchy. The hierarchy is as follows:

Complex Double-precision

Real Integer

Because type Boolean is not converted, i t does not fit in the hierarchy;

i t is used only with types integer, real, and pointer. Pointer type is used only with types integer, Boolean, and pointer. Types character and logical are not allowed in arithmetic expressions.

In an expression operating on either a single operand or a pair of

operands, the type and interpretation are independent of the context where the expression appears, and independent of the type of any other operand of a larger expression where the expression appears.

Example:

COMPLEX CPX

CPX

=

(-2.)**0.5 !Illegal

Although, in mathematics, complex numbers derive from operations on real numbers, the above exponentiation is illegal, as shown in

Table 5-3. Use of Data Types with Arithmetic Operations: +, -, *, /

y+-Iox y+-Rox y+-Dox y+-Cox y+-Box y+-Pox y+-Sox

y+-xoI 1+-101 R+-RoI D+-DoI C+-CoI 1I+-BoI 4I+-PoI 3. Sol y+-xoR R+-IoR R+-RoR D+-DoR C+-Cos. 2R+-BoR

PoR 3. SoR

y+-xoD D+-IoO D+-RoD D+-DoD C+-CoD

BoD

PoD

SoD

y+-xoC C+-IoC C+-s.oC C+-DoC C+-CoC

BoC

PoC

SoC

y+-xoB 1I+-IoB 2R+-RoB • DoB

CoB 1B+-Bo B 4I+-PoB 3. SoB

y+-xoI 4I+-IoP • RoP

DoP • CoP 4I+-BoP 4I+-PoP

SoP

y+-xoS 3. loS 3. RoS

DoS

CoS 3. BoS • PoS 3. SoS

Legend:

x One of two operands. A capital letter represents another operand of the indicated data type. Logical is never allowed.

o Arithmetic operator: + -

* /.

Two operands and an operator are an expression.

y Result of arithmetic operation. A letter to the left of the +- symbol represents a result of the indicated data type.

• Error

An underscored operand's data type is converted before computation of the expression.

I Integer B Boolean

R Real P Pointer

D Double-precision S Character

C Complex

1 Integer operation; no conversion is performed on the Boolean item.

2 Real operation; no conversion is performed on the Boolean item.

3 If S is a literal character string and length (S) ~ 8 characters, then the operation is performed with S treated as Hollerith; a non-ANSI warning message is issued. See 5.2.

4 The only allowed expressions are: P+I, P-I, I+P, I-P, P+B, P-B, B+P, B-P, P+P, and P-P. All allowed expressions produce integer results.

Multiplication and division of pointers are not allowed.

The ANSI Fortran Standard does not allow complex and double-precision types to be mixed in arithmetic operations.

Table 5-4. Data Types in Exponentiation: **

y+-I**x y+-R**x y+-D**x y+-C**x

y+-x**I 1+-1**1 R+-R**I D+-D**I C+-C**I y+-x**R R+-I**R R+-R**R D+-D**R C+-C**R y+-x**D D+-I**D D+-R**D D+-D**D lC+-C**D y+-x**C C+-I**C C+-R**C C+-D**C C+-C**C

Types Boolean, logical, character, and pointer cannot be used in exponentiation.

In an expression a**b, if a is negative, b must be of integer type.

Legend:

x One of two operands. A capital letter represents an operand of the indicated data type.

** Exponentiation operator

y Result of exponentiation operation. A letter to the left of the +- symbol represents a result of the indicated type.

• Error

Underscore indicates that the indicated operand's data type is converted before the operation is performed.

I Integer R Real D Double-precision C Complex

1 The double-precision exponent is converted to type real.

The ANSI Fortran Standard does not allow complex and double-precision types to be mixed in exponentiation operations.

I

5.1.4.1 Type conversion

Type conversion Qf operands can occur during an expression's evaluation or when the results of an expression's evaluation are stored into a variable or array element. Type conversion is based on the following two

operations.

(a) Integer-to-real conversion creates a real value from an integer value.

(b) Real-to-integer conversion creates a 64-bit integer value from a real value. The fractional part is truncated.

In the above conversions, the absolute value of the integer must be less than 2**46, or less than 2**63 if 64-bit integer arithmetic is enabled. A warning is issued if the value is determined at compile time to exceed this range, but not for an out-of-range value that occurs at run time.

Integer - Type integer is converted to type real as described in item (a) above. Integer is converted to double-precision by converting to real and adding zeros to extend the precision of the value. For converting integer to complex, the integer is converted to real as described in item (a) above; then the integer value becomes the real portion of a complex value, and zero is the imaginary portion.

Real - Real is converted to integer as described in item (b) above. For converting to double-precision, zeros are added as the least significant portion to extend the real number's prec1s1on. For converting real to complex, the real value becomes the real portion of a complex value and zero is the imaginary portion.

Complex - Complex is converted to integer by converting the real portion of the complex value as described for the real value in item (b) above.

For converting complex to real, the real portion of the complex value becomes the real value. For converting complex to double-precision, zeros are added to extend the precision of the real portion of the complex

value; this extended real portion becomes the double-precision value.

Double-precision - Double-precision is converted to integer by converting the most significant portion as in item (b) above. For converting to real, the most significant portion of the double-precision value is used as the real value; no rounding occurs. For converting to complex, the most significant portion of the double-precision value becomes the real portion of the complex value, and zero is the imaginary portion; no rounding occurs.

The ANSI Fortran Standard does not provide for converting double-precision to complex.

Dans le document CRAY® COMPUTER SYSTEMS (Page 172-177)