How could I get constants (or parameter
s, I suppose) that are negative and positive infinity in Fortran 2008? I tried the following code:
program inf
use, intrinsic :: ieee_arithmetic
real(8), parameter :: inf_pos = ieee_value(0d0, ieee_positive_inf)
real(8), parameter :: inf_neg = ieee_value(0d0, ieee_negative_inf)
end program inf
However, I get the following errors:
$ gfortran inf.f08
inf.f08:4:22:
real(8) :: inf_pos = ieee_value(0d0, ieee_positive_inf)
1
Error: Function ‘ieee_value’ in initialization expression at (1) must be an intrinsic function
inf.f08:5:22:
real(8) :: inf_neg = ieee_value(0d0, ieee_negative_inf)
1
Error: Function ‘ieee_value’ in initialization expression at (1) must be an intrinsic function
Despite documentation saying otherwise, it seems that gfortran thinks that ieee_value()
isn't intrinsic.
Is there anyway to get what I'm trying to do?
I'll first look at why you can't use ieee_value
to give the value for your desired named constant, then I'll give bad news. Both are interesting (to me).
ieee_value
isn't an intrinsic procedure. It's a procedure in an intrinsic module but as the Fortran 2008 standard notes (Note 13.25):
The types and procedures defined in standard intrinsic modules are not themselves intrinsic.
gfortran is correct to note that ieee_value
may not be used in an initialization (constant) expression.
Now, if you need to initialize a named constant with an "infinite" value there are non-portable options:1.
transfer
;You can work around the non-portable nature of this with your build system and pre-processor.
That all said, you may not need to have an infinite named constant. The IEEE modules easily provide procedures for "is this value infinite?", or "set this value to be infinite". Your compiler may also have "initialize variable to infinity" as a compile-time option.
1 Outside initialization expressions the restrictions on using ieee_value
are much looser.