fortran90 - Fortran behavior of a tiny program -
i new in fortran90 (30 minutes ago...) , have program:
program example1 implicit none real (kind=8) :: x,y,z x = 3.d0 y = 2.d-1 z = x + y print *, "y = ", y print *, "x = ", x print *, "z = ", z end program example1
but when run with:
gfortran example1.f90 ./a.out
the output is:
y = 0.20000000000000001 x = 3.0000000000000000 z = 3.2000000000000002
why not 3.2000000000000000 ??? doing wrong? why y has 1 in last digit?? , why z has 2 in last digit?? sorry if dumb question, dont understand doing wrong...
thanks!!
there's absolutely nothing wrong program. issue has real
's inability represent decimals precisely, without error. problem numbers not composed of negative powers of 2
must represented approximately. that's why there small error in 16-th decimal place. more information representation of real
s take @ article on wikipedia. another great article on same subject.
if replace 0.2
0.25
, problem go away, because 0.25
2 ^ -2
.
Comments
Post a Comment