Unsafe Type and Kind Coercions
| SimCon Home | fpt Reference Manual Home |
Type and Kind Coercions
In principle, type and kind coercions could take place in two contexts:
In modern Fortran, only the first class of coercion is permitted. If the interface to a sub-program is visible, or an argument is passed to an intrinsic routine, the compiler has all of the information necessary to make an appropriate coercion. However, this is not done, and instead a diagnostic is issued. In some extended FORTRAN 77 systems, coercions were made. It was possible, for example, to define pi as pi = 4*ATAN(1). This situation is described in the mis-matched arguments page
Integer, Real and Complex Coercions
In standard Fortran, coercions within expressions may be made freely between all integer, real and complex types and kinds. fpt marks some of these coercions with diagnostics (Which are "Notes" - diagnostics of zero severity) because there is a small risk of overflow or underflow. Normally no error occurs.
A special case is the coercion of complex or real objects to integers when the objects are used as array indices. fpt marks all of these situations with warnings because, where the real values have no exact integer representation, they may be truncated towards zero to the next index.
Coercion of Integers to Logicals
The ifort (Intel Fortran) and Compaq Visual Fortran (CVF) compilers allow coercion of integers to logicals. The behaviour is inherited from VMS Fortran. The program shown below compiles and runs on these systems:
PROGRAM t INTEGER*4 i DO WRITE(*,'("i; ",$)') READ(*,*)i IF (i) THEN WRITE(*,'("i is .TRUE.")') ELSE WRITE(*,'("i is .FALSE.")') ENDIF ENDDO END PROGRAM t
Some compilers, for example gfortran and FTN95, will not compile this code.
ifort, CVF and VMS use only the value of the least significant bit of the integer to select .TRUE. (lsb set) or .FALSE. (lsb clear). Other systems which compile the code use zero (.FALSE.) or non-zero (.TRUE.). This is a non-standard usage and different compiler writers have made their own decisions.
fpt marks many kind and type coercions by default. A detailed report is made by the command:
The coercion of integer values to logicals may be corrected systematically by the command:
CORRECT USE OF INTEGER FOR LOGICAL
Copyright ©1995 to 2020 Software Validation Ltd. All rights reserved.