Wrong output of a simple real number

Wrong output of a simple real number

Hello,

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

program main

implicit none

real :: a
a=3.*a
write(*,*)a

end program main

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.

2 post / 0 nuovi
Ultimo contenuto
Per informazioni complete sulle ottimizzazioni del compilatore, consultare l'Avviso sull'ottimizzazione
Ritratto di mecej4

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 .

Accedere per lasciare un commento.