I am trying to get series of numbers starting from zero by increasing it by 0.1 by coding it in Fortran 90. But I am not getting exact values. I read a lot of answers that it is floating point error. I found this answer which was originally written in python and was facing the same issue as mine. Error in generating a set of decimal point numbers with a particular common difference in python I tried to implement this but no help. Edit: I have understood the reason for this behaviour through this: Is floating point math broken? But couldn't find the solution to solve this in fortran as it is solved in python in given answer. The code is as follows:
program float_trial
implicit none
integer :: t = 0
integer :: dt = 1
integer :: a_sec = 10
integer :: timemax = 12
double precision:: exact_t
if ( t<timemax ) then
do
print*, exact_t
t = t+ dt
exact_t = dble(t)/a_sec
if(t>=timemax) then
exit
endif
enddo
endif
end program float_trial
the output:
0.0000000000000000
0.10000000000000001
0.20000000000000001
0.29999999999999999
0.40000000000000002
0.50000000000000000
0.59999999999999998
0.69999999999999996
0.80000000000000004
0.90000000000000002
1.0000000000000000
1.1000000000000001
My expected output is : 0.0, 0.1, 0.2 ......... How can we resolve this in Fortran?