mgilson
mgilson

Reputation: 309821

exponentiation in an arithmetic constant expression

It turns out that the following is illegal in fortran 77:

c
      program main
      real a
      parameter(a=(10.)**(.5))
c
...

The reason is because the parameter statement takes a constant arithmetic expression and exponentiation is illegal except when the exponent is an integer. (Section 6.1.3 and 8.6 http://www.fortran.com/F77_std/rjcnf0001-sh-8.html#sh-8.6 ). Does anybody know if this restriction has be relaxed in newer revisions of the standard? Why would this be illegal in the first place?

Upvotes: 0

Views: 707

Answers (2)

Yes, this has been relaxed. In Fortran 2003 you can even use intrinsic functions like sin or exp.

Upvotes: 3

Phil H
Phil H

Reputation: 20131

My guess would be that integer exponents have a simple implementation that the compiler can call to replace the value as a precompile step. However, non-integer exponents (and negative exponents) could have values which fail to evaluate to reals; the point is not that this example is incalculable, just that the compiler needs to be predictable and it's simpler to just fail those expressions than write a routine that can determine whether a given expression will evaluate to a sensible number or not. Remember, this is being done before compilation, not by compiling the expression, running it and using the value, so the scope is not as wide as general fortran.

If it's a problem for you, then consider macros and F90/F77 rather than f90/f77 files, so that the compiler knows to run the preprocessor.

Or just calculate the value on your calculator to 16dp and use that.

Or let it be a normal variable rather than a parameter. The cost will be minimal.

Upvotes: 0

Related Questions