next up previous contents index
Next: Integer Division Up: Arithmetic Expressions Previous: General Rules

Data Type Conversions

 

If an operator has two operands of the same data type then the result has the same type. If the operands have different data types then an implicit type conversion is applied to one of them to bring it to the type of the other. These conversions always go in the direction which minimises loss of information:

Since there is no way of converting a complex number to double precision type, or vice-versa, without losing significant information, both these conversions are prohibited: an operator cannot have one complex operand and one of double precision type. All other combinations are permitted. These implicit type conversions have the same result as if the appropriate intrinsic function (REAL, DBLE, or CMPLX) had been used. These are described in detail below. Note that the data type of any operation just depends on the two operands involved; the rest of the expression has no influence on it whatever.

Exponentiation is an exception to the type conversion rule: when the exponent is an integer it does not have to be converted to the type of the other operand and the result is evaluated as if by repeated multiplication. But if the exponent has any other data type the calculation is performed by implicit use of the LOG and EXP functions, thus:

The first result will, of course, be computed more rapidly and accurately than the second. If the exponent has a negative value the result is simply the reciprocal of the corresponding positive power, thus:

Note that conversion from real to double precision cannot produce any information not present originally. Thus with a real variable R and a double precision variable D:

 
       R = 1.0 / 3.0 
       D = R
D may end up with a value such as 0.3333333432674408... which is no closer to the value of one third than R was originally.


next up previous contents index
Next: Integer Division Up: Arithmetic Expressions Previous: General Rules

Mario Storti
Wed Nov 4 19:32:56 ART 1998