Can I call fortran log function with number?

I get some strange results from the log function in gfortran 4.5 (OSX) and gfortran 4.7 (OSX).

The following program gives 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 as follows:

 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 
+4
source share
3 answers
 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.

+7
source

first try changing exp to dexp and log to dlog ?

if this is just your compiler’s problem, it didn’t force double precision sequentially in the code, so try double precision as the compiler flag in gfortran: gfortran -fdefault-real-8 foo.f90

or by adding a macro

 #define REAL DOUBLE PRECISION 
+1
source

I am sure that you continue to simply call a natural journal. The log () in fortran is used essentially for the natural.

-2
source

Source: https://habr.com/ru/post/1413393/


All Articles