program test_log integer, parameter :: Dbl_K = selected_real_kind (14) real(Dbl_K) :: e = 2.7182818284590451_Dbl_K real(Dbl_K) :: e2 = 2.7182818284590451D0 real(Dbl_K) :: e3 = exp(1.0) real(Dbl_K) :: e4 = exp(1.0_Dbl_K) write(*,*) 'log(2.7..)', log(2.7182818284590451_Dbl_K) write(*,*) 'log(e)', log(e) write(*,*) 'log(e2)', log(e2) write(*,*) 'log(e3)', log(e3) write(*,*) 'log(e4)', log(e4) end program test_log
Uses select_real_kind instead of relying on a value of 8, which is a double value.
Exit with gfortran 4.6 and 4.7 on a MacBook Pro with Snow Leopard:
log(2.7..) 1.0000000000000000 log(e) 1.0000000000000000 log(e2) 1.0000000000000000 log(e3) 0.99999996963214000 log(e4) 1.0000000000000000
The value for e3 is different from 1 because "exp (1.0)" has 1 as the real real precision.
On my new Mac Air with Lion, I get the wrong results. I observed other gfortran problems on this machine.
gfortran 4.6
log(2.7..) 1.6249753165355076 log(e) 1.6249753165355076 log(e2) 1.6249753165355076 log(e3) 1.6438056251294211 log(e4) 1.6438055841322456
gfortran 4.7
log(2.7..) 1.6249753165355076 log(e) 1.0000000000000000 log(e2) 1.0000000000000000 log(e3) 1.0188423211430429 log(e4) 1.0188422801197137
So the problem is not unique. Not sure what it is.
PS Change the output in the MacAir compiler options. I'm not sure which option or options matter.
PPS
program test2_log real:: e = 2.7182818284590451 real:: e2 = 2.7182818284590451D0 real:: e3 = exp(1.0) real:: e4 = exp(1.0) write(*,*) 'log(2.7..)', log(2.7182818284590451) write(*,*) 'log(e)', log(e) write(*,*) 'log(e2)', log(e2) write(*,*) 'log(e3)', log(e3) write(*,*) 'log(e4)', log(e4) end program test2_log
On MacAir gfortran 4.6 gives:
log(2.7..) 1.3489696 log(e) 0.99999994 log(e2) 0.99999994 log(e3) 1.0188423 log(e4) 1.0188423
with the compiler option -fdefault-real-8 it changes to
log(2.7..) 1.6249753165355076 log(e) 1.0000000000000000 log(e2) 1.0000000000000000 log(e3) 1.0188422801197137 log(e4) 1.0188422801197137
This is a defect in the installation.
Edit: all versions of gfortran on both machines are owned by MacPorts. Is there another OS version or something else? Powered by Snow Leopard, not the Lion.