In Fortran 90 (using gfortran on Mac OS X), if I assign a value to a variable with double precision without explicitly referencing it, the precision does not βtakeβ. I mean, if I run the following program:
program sample_dp
implicit none
integer, parameter :: sp = kind(1.0)
integer, parameter :: dp = kind(1.0d0)
real(sp) :: a = 0.
real(dp) :: b = 0., c = 0., d = 0.0_dp, e = 0_dp
a = 0.12345678901234567890
b = 0.12345678901234567890
c = DBLE(0.12345678901234567890)
d = 0.12345678901234567890_dp
write(*,101) a, b, c, d
101 format(1x, 'Single precision: ', T27, F17.15, / &
1x, 'Double precisison: ', T27, F17.15, / &
1x, 'Double precision (DBLE): ', T27, F17.15, / &
1x, 'Double precision (_dp): ', T27, F17.15)
end program
I get the result:
Single precision: 0.123456791043282
Double precision: 0.123456791043282
Double precision (DBLE): 0.123456791043282
Double precision (_dp): 0.123456789012346
The result of single precision begins to be rounded in the eighth decimal place, as expected, but only the double precision variable that I explicitly assigned _dp retains all 16 digits of precision. It seems strange, as you would expect (I'm relatively new to Fortran), that a double-precision variable will automatically be double-precision. Is there a better way to assign double precision variables, or do I need to explicitly enter them as above?