!/////////////////////////////////////////////////////////////////////////
!//
!// trunc.f90
!//
!// Program illustrating the roundoff error problem
!//
!/////////////////////////////////////////////////////////////////////////


PROGRAM trunc
  IMPLICIT NONE

  !// Declare the variable pi
  REAL                     :: pi
  !// Declare the array Z and Zr where Zr is the relative error
  REAL, DIMENSION(100)     :: Z, Zr
  !// Declare the array S and Sr where Sr is the relative error
  REAL, DIMENSION(100)     :: S, Sr
  !// Declare the counter variable i
  INTEGER                  :: i
  !// Declare the unit number for the file
  INTEGER, PARAMETER       :: lun = 10
  !// Declare the status variable
  INTEGER                  :: res

  !// Initialize pi
  pi = 4. * atan(1.)

  !// Initialize the first element of both Z and S
  ????

  !// Loop through the arrays and calculate the values
  !// using the formulas in equation 2
  DO i = 1, ???
    !!/ Program the calculations here
  END DO

  !// Calculate the relative error for both Z and S
  !// storing the result in the arrays Zr and Sr
  DO i = 1,???
    !!/ Program the calculations here
  END DO

  !// Print the values of Z, Zr, S and Sr to a file
  !// Open the file using the unit number which will be used
  !// to access the file
  OPEN(UNIT=lun, FILE='truncerr.txt',FORM='FORMATTED',IOSTAT=res)

  !// Test that the opening was successful
  IF(res /= 0) THEN
    !// An error has occurred, prionting an error message and
    !// stopping the program
    PRINT *, 'Error in opening the file truncerr.txt, status: ', res
    STOP
  END IF

  !// Write the values to the file
  DO i = 1, 100
    WRITE(UNIT=lun,FMT='(A3,F12.8,A4,F12.8,A3,F12.8,A4,F12.8)',IOSTAT=res) &
                       'Z ', Z(i), 'Zr ', Zr(i), &
                       'S ', S(i), 'Sr ', Sr(i)
  END DO
  !// Close the file
  CLOSE(UNIT=lun)

  !// That is all to it
END PROGRAM trunc
