Precision problems of real numbers in Fortran

Posted by saladoil on Stack Overflow See other posts from Stack Overflow or by saladoil
Published on 2012-04-04T17:26:06Z Indexed on 2012/04/04 17:29 UTC
Read the original article Hit count: 184

Filed under:
|
|
|
|

I've been trying to use Fortran for my research project, with the GNU Fortran compiler (gfortran), latest version, but I've been encountering some problems in the way it processes real numbers. If you have for example the code:

program test
implicit none

real :: y = 23.234, z

z = y * 100000
write(*,*) y, z

end program

You'll get as output:

23.23999    2323400.0. 

I find this really strange. Can someone tell me what's exactly happening here? Looking at z I can see that y does retain its precision, so for calculations that shouldn't be a problem I suppose. But why is the output of y not exactly the same as the value that I've specified, and what can I do to make it exactly the same?

© Stack Overflow or respective owner

Related posts about output

Related posts about fortran