Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Read data file error

turkeydodo
Beginner
1,043 Views
I am trying to read a data file like below which has two columns

0.0 1030.7
1.0 1055.2
2.0 1081.9
...

I defined two double precision arrays to accept the data

REAL*8 AngDat(0:800),GasDat(0:800)

When Iused the following statement to read

do j=0,800
read (GASF,*) AngDat(j),GasDat(j)
enddo

I found that AngDat is no problem while GasDat adding so many random numbers after the 18th digits. Can anybody explain that why the first one is no problem while the second one is not exactly correct by using this "*" format? While, inside the whole array GasDat, I also found that some values are exactly correct while the mostare not. Can anybody explain on that? Is that a defect of IVF?

Because the data format inside the file could change like

0.00000 1030.7
0.0 1030.700000
0.0E5 1030.7
0.0E5 1030.7D0
...

But theygenerally spaced by a space or spaces. These kind of formatis so common in the real practice.Is there any format I can use toread the data correctly? If not, can you guys develop one for that? Or can you guys give a code for that?

0 Kudos
6 Replies
TimP
Honored Contributor III
1,043 Views
REAL*8 corresponds to the KIND set by selected_real_kind(15). In the apparently unlikely event that you need 19 accurate decimal digits in your read and write conversions, and accept the performance consequences, you could specify the corresponding KIND.
0 Kudos
turkeydodo
Beginner
1,043 Views
So "Real*8" does not equal to "double precision"

In last thread I found that in double precision, numbers could be as small as 2.0D-308. So I have to change all real*8 to double precision?
0 Kudos
turkeydodo
Beginner
1,043 Views
So "Real*8" does not equal to "double precision"

In last thread I found that in double precision, numbers could be as small as 2.0D-308. So I have to change all real*8 to double precision?
0 Kudos
mecej4
Honored Contributor III
1,043 Views
Instead of jumping to false conclusions such as this, please read the Fortran documentation. On this platform, Real*8 is the same as double precision.

The answer to your original question has little to do with precision. Nor does is the use of list-directed input the problem.

The numbers in the first column, to the extent that you showed, are small integers, and when stored in REAL*8 IEEE form, are represented exactly. The numbers in the second colum, however, cannot be represented exactly in REAL*8 form. When converted back to decimal, the digits that you see beyond the 16th are of no significance, and you should conclude nothing from them.

Even a "simple" number such as 0.1 cannot be represented exactly in internal IEEE form.
0 Kudos
DavidWhite
Valued Contributor II
1,043 Views
This difficulty in expressing real numbers in the discret world of computing is not unique to Fortran.

Suggest you readhttp://en.wikipedia.org/wiki/Floating_point or similar articles.


David

0 Kudos
profray
Beginner
1,043 Views

program RealEight
implicit none

! Variables
Real*8 xeight,yeight
Real xfour,yfour
! Body of RealEight
print *, 'Hello World'
open(unit=10,file='datain.txt',status="old")
read(10,*) xeight,yeight
read(10,*) xfour, yfour
write(6,*) xeight,yeight
write(6,*) xfour, yfour
end
program RealEight

Input file looked like this:

1.0 2345.678901
2.0 9876.543210

output to the console looked like this
Hello World
1.00000000000000 2345.67890100000
2.000000 9876.543

So, maybe there are some project settings that force the real*8 to pad with zeros.
Rich

0 Kudos
Reply