Can Fortran help with data file problem?

  • Context: Fortran 
  • Thread starter Thread starter hcelik
  • Start date Start date
  • Tags Tags
    Data File Fortran
Click For Summary

Discussion Overview

The discussion centers around a Fortran code issue where a user is attempting to save numbers between 0 and 1 with increments of 0.01 to a data file. The user encounters a problem where the expected value of 0.315000 is instead recorded as 0.314999. Participants explore potential causes and solutions related to floating-point arithmetic and coding practices.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions the choice of STEP = 1E-3 if the intention is to increment by 0.01, suggesting that this leads to incorrect values.
  • Another participant emphasizes the importance of using real numbers for calculations, recommending the use of RE = (1.0) instead of RE = 1 to avoid potential issues with integer arithmetic.
  • A participant points out that floating-point values are not stored exactly, leading to accumulated errors when adding small increments multiple times.
  • It is suggested that calculating each value of Y independently (Y(i) = (i-1)*STEP) could improve accuracy compared to the current method of accumulating values.
  • One participant argues that using RE = (1.0) instead of RE = 1 will not make a difference, as integer constants are converted to reals accurately in this context.

Areas of Agreement / Disagreement

Participants express differing views on the best approach to handle floating-point arithmetic and the implications of using integer versus real values in calculations. There is no consensus on a single solution to the problem presented.

Contextual Notes

Participants note that floating-point precision issues can lead to inaccuracies, especially when performing many additions of small increments. The discussion highlights the importance of understanding how data types affect calculations in programming.

hcelik
Messages
4
Reaction score
0
Hello guys. I want to save the numbers between 0 and 1 with 0.01 increasement to a dat file. I wrote a code for this using Fortran however, after a point I get 0.314999 instead of 0.315000. Can you help me to solve my problem?


<code>
REAL, DIMENSION(:), ALLOCATABLE :: Y
REAL STEP, JEND, PX, GR, RE, RATIO
INTEGER N, I, J
JEND = 1.00
STEP = 1E-3
N = JEND/STEP+1
RE=1
GR=1
RATIO=GR/RE
PX=-12.00
ALLOCATE (Y(N))
Y(1) = 0.0
OPEN(UNIT=8, FILE='HEATLINE.DAT', STATUS='OLD', FORM='FORMATTED')
DO I = 2, N
Y(I) = Y(I-1)+STEP
END DO
PRINT *, 'N IS:' , N
PRINT *, 'I IS:',I
53 format('VARIABLES = "Y"')

WRITE(8,53)

do i=1,n
write(8,*) Y(I)
end do

CLOSE(8)
END



</code>
 

Attachments

  • Ekran Alıntısı.PNG
    Ekran Alıntısı.PNG
    10.8 KB · Views: 601
Technology news on Phys.org
If you want to increment Y by 0.01 each time, why is STEP = 1E-3?
 
hcelik said:
Hello guys. I want to save the numbers between 0 and 1 with 0.01 increasement to a dat file. I wrote a code for this using Fortran however, after a point I get 0.314999 instead of 0.315000. Can you help me to solve my problem?


<code>
REAL, DIMENSION(:), ALLOCATABLE :: Y
REAL STEP, JEND, PX, GR, RE, RATIO
INTEGER N, I, J
JEND = 1.00
STEP = 1E-3
N = JEND/STEP+1
RE=1
GR=1
RATIO=GR/RE
PX=-12.00
ALLOCATE (Y(N))
Y(1) = 0.0
OPEN(UNIT=8, FILE='HEATLINE.DAT', STATUS='OLD', FORM='FORMATTED')
DO I = 2, N
Y(I) = Y(I-1)+STEP
END DO
PRINT *, 'N IS:' , N
PRINT *, 'I IS:',I
53 format('VARIABLES = "Y"')

WRITE(8,53)

do i=1,n
write(8,*) Y(I)
end do

CLOSE(8)
END



</code>

I humbly suggest that you do all divisions, multiplications , operations with real values when you are dealing with real numbers.

i.e: if RE is a real and you want to declare it

try RE=(1.0) instead of RE=1 ( 1 is an integer so may affect the operation )

Use real numbers when dealing with real numbers, I can humbly suggest.

Good Luck !
 
SteamKing said:
If you want to increment Y by 0.01 each time, why is STEP = 1E-3?

And yes beides you should set STEP=1.*(10.)**-2 for 0.01 increments, else it will increment by 0.001 and print out 1001 values, where it should make 101 values.
 
I assume you already know that floating point values stored in real variables are almost never exact. If you set STEP = 1E-3, the value will not be exactly 0.001, though it should be accurate to about 6 or 7 significant figures.

The problem with your code is that you are adding STEP to itself a very large number of times, so these small errors accumulate.

If you add STEP to itself 10 times, instead of 6 or 7 signifincat digits of accuracy you will only have about 5 or 6 digits. If you add it to itself 100 times, the accuracy reduces to 4 or 5 digits. For 1000 times, 3 or 4 digits, etc.

The best way to avoid this problem is to calculate each value of Y independently of the others. Instead of
Code:
Y(1) = 0.0
do I = 2,N
Y(I) = Y(I-1) + STEP
end do
write something like
Code:
do i = 1,n
Y(i) = (i-1)*STEP
enddo
Then every entry in the Y array will be as accurate (6 or 7 significant figures) as the value of STEP.
 
Last edited:
stallionx said:
Itry RE=(1.0) instead of RE=1

That will make no difference. Integer constants (at least if they have less than about 6 decimal digits) are converted to reals exactly. That certainly applies to constants like 0 and 1. The paretheses ( ) don't have any purpose here either.
 

Similar threads

  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K