0

I am trying to convert a character string to a real number in Fortran (compiled using gfortran), however, I have found that for a couple of the numbers they appear different when written out as strings and when written out as numbers.

For example, I have the following character strings:

'16072013','22082013','17052013'

When I convert these to real numbers I get the following output:

'16072013.0','22082013.0','17052012.0'

The last number has changed (from 17052013 to 17052012) but I cannot understand why as the same process is used to convert all the numbers. I convert using:

REAL :: daymonthyear
CHARACTER(len=20) :: daymonthyear_c
read(daymonthyear_c,'(f9.0)')daymonthyear

Can anyone explain what I am doing wrong and how I can fix the problem?

1 Answers1

0

You seem to be running into the precision limits of a REAL. Use DOUBLE PRECISION.

Better still would be:

integer, parameter :: dp = selected_real_kind(15, 307)
real(kind=dp) :: daymonthyear
Jack
  • 5,801
  • 1
  • 15
  • 20
  • 1
    This answer should be removed as it does not actually address the problem. It only provides a bandage until the user exceeds 53 bits, – Steve Sep 14 '18 at 17:44
  • 1
    Also technically it doesn't use double precision. It just uses a real kind that may or may not be the default – Ian Bush Sep 14 '18 at 19:21