Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Wrong output of a simple real number

Marius_Z_
Beginner
444 Views

Hello,

I'm havin trouble with the outcome of the following most simple FORTRAN programm:

[fortran]program main

implicit none

real :: a

a=3.*a

write(*,*)a

end program main[/fortran]

the output in the terminal is 7.5000003E-02 instead of the the correct value. I have also tried compiling with double precision with the -r8 command, resulting in a 1 at the end of the output. Furthermore the problem does occur with other simple calculations as long as the multiplied number is of magnitude less than 10⁻2.

I have also tried different version of the Intel composer suit and different computers with Ubuntu or Scientific Linux.

0 Kudos
1 Reply
mecej4
Honored Contributor III
444 Views

Your program uses the variable a before it has been initialized with a value. Therefore, the program may print any value and you may not complain about it.

If, however, you meant to assign 0.025 to a before using the value, your question is a Frequently Asked Question. A short answer is that most floating point numbers (including, in particular, numbers with simple decimal representations, such as 0.1) do not have exact machine representations, and small errors of the magnitudes that you noticed are to be expected. For more details, you may consult, among others, http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html .

0 Kudos
Reply