Question

I have a program which loops over one variable and computes a value at each step:

  program cpout

  implicit none

  !declarations
    integer, parameter :: dp = selected_real_kind(15)
                ! kind value for double precision

    real(dp), parameter :: Ru = 8.314472_dp    
    real(dp) :: cp
    integer :: loT, hiT, i
    real(dp) :: iT
    real(dp),dimension(14) :: ic8a
    real(dp) :: ic8t
    real(dp) :: ic8c

    loT = 300
    hiT = 3000

! ic8a is populated using a subroutine call
! I have checked, it reads in reals as it is supposed to

    do i = loT, hiT, 1

      iT = real(i,dp)

      if (iT > ic8t) then
        ic8c = Ru*(ic8a(1) + ic8a(2)*iT + ic8a(3)*(iT**2)
 *                 + ic8a(4)*(iT**3) + ic8a(5)*(iT**4))
      else
        ic8c = Ru*(ic8a(8) + ic8a(9)*iT + ic8a(10)*(iT**2)
 *                 + ic8a(11)*(iT**3) + ic8a(12)*(iT**4))
      end if

    end do

  end program cpout

In my first attempt, I used iT as the integer loop counter, and then used it directly in the formula. This produced a piecewise graph for iT > ic8t. When I added i as the counter, and converted iT to real before using it in the formula, the graph came out smooth as it should. Why should it matter whether iT is real or integer when plugging in to the formula? My compiler is g77.

EDIT: The formula gives some inaccurate values for iT < ic8t as well.

Was it helpful?

Solution

If you just use INTEGER variable i (as you mentioned in your comment) you probably have arithmetic overflow. You can either convert i to REAL as you did or choose an appropriate kind parameter for it. A small example:

PROGRAM ex

  IMPLICIT NONE

  INTEGER, PARAMETER :: long = selected_int_kind(10)

! Here we have arithmetic overflow  
! PRINT *, 2000**3
! But not here
  PRINT *, 2000_long**3

END PROGRAM ex
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top