Precision of Fortran Variables


narelle
07-02-2007, 11:11 AM
I am relatively new at Fortran 90 and have run into a problem with some code I am writing. I have a SuSe Linux Operating System and am running Intel Fortran Compiler 9.0. What is happening is that I am getting really low precision with all the variables in my code. As an example, consider the simple code:

PROGRAM Precision

IMPLICIT NONE

INTEGER, PARAMETER :: DP = SELECTED_REAL_KIND(P=15)

REAL(KIND = DP) :: precise = 1234.5678901

WRITE(*,*) precise

END PROGRAM Precision

The result I get is: 1234.56787109375

If I change the value of precise to 123456789.01, the answer I get is 123456792.000000.

Clearly this is not correct.

I was advised that using SELECTED_REAL_KIND was the most portable approach to assigning kinds. I tried a few other variations and the precision was just as bad. I must be missing something, but I just can't see what? Any help would be much appreciated.

Narelle

narelle
07-04-2007, 07:14 AM
OK, so from another forum I now know that I have to use:

REAL(KIND = DP) :: precise = 1234.5678901_DP

The last _DP is essential.

Thanks to tim18 on the intel forum. He wrote:

You want consistently typed constants, such as 1234.5678901_DP where DP was declared as you stated. By the rules of Fortran, for over 40 years, a default real constant is not promoted according to context. ifort has options to change that, but you said you wanted portability.