Infinity and negative infinity

You can also use 2*huge(1.0_wp), where wp is the working precision, as mentioned on comp.lang.fortran. The code

program test ! demonstrate setting and reading infinity
implicit none
integer, parameter :: wp = kind(1.0d0)
real(kind=wp), parameter :: infin = 2*huge(1.0d0)
real(kind=wp) :: x
character (len=20) :: text
print*,infinity(),infin,infin>huge(1.0d0)
text = "Infinity"
read (text,*) x
print*,x
text = "+Inf"
read (text,*) x
print*,x
contains
real(kind=wp) function infinity()
implicit none
real(kind=wp) :: x
x = huge(1.0_wp)
infinity = x + x
end function infinity
end program test

gives output +Inf +Inf T for g95 and the same for gfortran and Intel Fortran, although they output Infinity instead of +Inf. The program also shows that one can read infinity from a string.

1 Like