The purpose of this program is to show you three common programming traps:
Click here to download this program.! ------------------------------------------------------------ ! This program illustrates the following points: ! (1) The exponential trap: ! That is, A**B**C is equal to A**(B**C) rather ! than (A**B)**C. ! (2) The integer division trap: ! That is, 4/6 is ZERO in Fortran rather than ! a real number 0.666666 ! Function REAL() is used to illustrate the ! differences. ! (3) The string truncation trap: ! What if the length assigned to a CHARACTER ! is shorter than the length of the string you ! expect the identifier to have? The third part ! shows you the effect. ! ------------------------------------------------------------ PROGRAM Fortran_Traps IMPLICIT NONE INTEGER, PARAMETER :: A = 2, B = 2, H = 3 INTEGER, PARAMETER :: O = 4, P = 6 CHARACTER(LEN=5), PARAMETER :: M = 'Smith', N = 'TEXAS' CHARACTER(LEN=4), PARAMETER :: X = 'Smith' CHARACTER(LEN=6), PARAMETER :: Y = 'TEXAS' ! The exponential trap WRITE(*,*) "First, the exponential trap:" WRITE(*,*) A, ' ** ', B, ' ** ', H, ' = ', A**B**H WRITE(*,*) '( ', A, ' ** ', B, ' ) **', H, ' = ', (A**B)**H WRITE(*,*) A, ' ** ( ', B, ' ** ', H, ' ) = ', A**(B**H) WRITE(*,*) ! The integer division trap. Intrinsic function REAL() converts ! an integer to a real number WRITE(*,*) "Second, the integer division trap:" WRITE(*,*) WRITE(*,*) O, ' / ', P, ' = ', O/P WRITE(*,*) 'REAL( ', O, ' ) / ', P, ' = ', REAL(O)/P WRITE(*,*) O, ' / REAL( ', P, ' ) = ', O/REAL(P) WRITE(*,*) ! The string truncation trap WRITE(*,*) "Third, the string truncation trap:" WRITE(*,*) 'IS ', M, ' STILL IN ', N, '?' WRITE(*,*) 'IS ', X, ' STILL IN ', Y, '?' END PROGRAM Fortran_Traps
First, the exponential trap: 2 ** 2 ** 3 = 256 ( 2 ** 2 ) **3 = 64 2 ** ( 2 ** 3 ) = 256 Second, the integer division trap: 4 / 6 = 0 REAL( 4 ) / 6 = 0.666666687 4 / REAL( 6 ) = 0.666666687 Third, the string truncation trap: IS Smith STILL IN TEXAS? IS Smit STILL IN TEXAS ?
On the second line, it is easily seen that the original Smith becomes Smit and the original TEXAS becomes TEXAS_, where _ indicates a space.IS Smith STILL IN TEXAS? IS Smit STILL IN TEXAS ?