Why does the accuracy of the output data change?

  Kiến thức lập trình

I used a double precision allocatable array to read data from one file as follows.

INTEGER,PARAMETER :: dp = SELECTED_REAL_KIND(15,14)
REAL (KIND=dp), ALLOCATABLE :: arr(:,:)
INTEGER :: nu, i
OPEN (UNIT=3, FILE='filename.dat', STATUS='OLD')
READ (UNIT=3, FMT=*) nu
ALLOCATE (arr(nu,3))
DO i = 1, nu, 1
      READ (UNIT=3, FMT=*) arr(i,:)
END DO
CLOSE (UNIT=3)

After I read the data, I copied these data 10000 times and store them into another allocatable array. Every time when I copied these data, I changed arr(i,3) through adding a constant value. Finally, I wrote all these copied data into another new file as follows.

REAL (KIND=dp) :: inc = 31.494732290178
REAL (KIND=dp), ALLOCATABLE :: brr(:,:)
ALLOCATE (brr(10000*nu,3))
DO i = 1, 10000, 1
     brr(1+(i-1)*nu:i*nu,1:2) = arr(1:nu,1:2)
     brr(1+(i-1)*nu:i*nu,3) = arr(1:nu,3)+DBLE(i)*inc
END DO
OPEN (UNIT=4, FILE=‘new.dat’, STATUS='UNKNOWN')
DO i = 1, 10000*nu, 1
     WRITE (UNIT=4, FMT=*) brr(i,:)
END DO
CLOSE (UNIT=4)

In this new file, every ‘nu’ lines, the 3rd value changes by a constant value; however, I found that the 3rd value does not change by a constant value. The difference between the 3rd value in every ‘nu’ lines changes from 31.494732290178 to 31.794732290178.

I think this is due to the accuracy issue in the variables. Would anyone please give me some suggestion or solutions on this issue?

Thank you in advance.

Kieran

LEAVE A COMMENT