Fortran natural logarithm error
Asked Answered
C

1

5

New to Fortran (just started today), having trouble with the natural logarithm:

PROGRAM log
IMPLICIT NONE
REAL :: x

PRINT *, "Enter a number:"
READ *, x

x = log (x)

PRINT *, "The natural log of x is:", x

END PROGRAM log

The compiler keeps throwing up the error:

x = log (x)
       1
Error: Symbol at (1) is not appropriate for an expression

Other intrinsic functions work fine. What am I doing wrong?

Corporeal answered 27/10, 2012 at 21:21 Comment(0)
J
9

The problem is that you've shadowed (overridden) the definition of the symbol log - which would normally refer to the standard library mathematical function - with the name of your program, which is also log. If you change the name of the program to, say, logtest:

PROGRAM logtest
...
END PROGRAM logtest

You'll find that the program works as expected.

Jessejessee answered 27/10, 2012 at 21:31 Comment(4)
No, not at all, it's not something that would happen in most other languages (the shadowing, yes, but not with the program name). Most languages don't even have program names.Jessejessee
funny, this had me stumped -- I guess since I haven't used a PROGRAM statement since 1985.. Do they serve a purpose other than taking one symbol out of your namespace?Soften
I think it's only for clarity; the shortest valid fortran is "end". The error message gfortran gives here is a little puzzling though.Quarterdeck
It's like naming loops or conditionals - it's just for clarity and so that the compiler can give you clearer errors when you "end" the wrong thing. But yes, it's a serious limitation that there's only one namespace for symbols.Jessejessee

© 2022 - 2024 — McMap. All rights reserved.