I’m getting some strange results from the log function in gfortran 4.5 (OSX) and gfortran 4.7 (OSX).
The following program gives the unexpected results:
program test_log
real(8) :: e = 2.7182818284590451_8
write(*,*) 'log(2.7..)', log(2.7182818284590451_8)
write(*,*) 'log(e)', log(e)
end program test_log
I run it like this:
gfortran-mp-4.5 ./test.f90 && ./a.out
log(2.7..) 1.6249753165355076
log(e) 1.0000000000000000
I expected both to be 1.0
Update:
program test_log
real(8) :: e = 2.7182818284590451_8
real(8) :: e2 = 2.7182818284590451D0
real(8) :: e3 = exp(1.0)
write(*,*) 'log(2.7..)', log(2.7182818284590451_8)
write(*,*) 'log(e)', log(e)
write(*,*) 'log(e2)', log(e2)
write(*,*) 'log(e3)', log(e3)
end program test_log
gives
gfortran-mp-4.5 ./test.f90 && ./a.out
log(2.7..) 1.6249753165355076
log(e) 1.0000000000000000
log(e2) 1.0000000000000000
log(e3) 1.0188423211430429
Uses selected_real_kind instead of relying on 8 being the value for double.
Output with gfortran 4.6 and 4.7 on a MacBook Pro with Snow Leopard is:
The value for e3 is different from 1 because “exp(1.0)” has 1 as a single precision real.
On my new Mac Air with Lion I get incorrect results. I have observed other problems gfortran on this machine.
gfortran 4.6
gfortran 4.7
So the problem isn’t unique. Not sure what it is.
P.S. On the MacAir compiler options change the output. I’m not sure which option or options matter.
P.P.S.
On the MacAir, gfortran 4.6 gives:
with the compiler option -fdefault-real-8 it changes to
It’s some defect in the installation.
Edit: all gfortran versions on both machines are from MacPorts. Is the different the OS version or something else? Works on Snow Leopard, not on Lion.