The real problem is that 0.3 and 0.7 cannot be exactly expressed in binary format.
0.3 => 0.010011001100110011 ....... 0.7 => 0.0101100110011001100 .......
When they are saved, if both are rounded or both are rounded down, adding two numbers will not return to 1.000000000 .....
This is a common mistake in modeling programming. Try to have step sizes that are natural for a computer:
real*8 :: dt=2.0d0**(-5)
The negative powers of the two can be represented exactly on the computer. So it really works:
program negative_powers real*8 :: dt = 2.0d0**(-8) real*8 :: t = 0.0d0 do while (t .ne. 500.0d0) print *, t t = t + dt end do print *, t end program negative_powers
Jack
source share