Infinity in Fortran

What is the safest way to set a variable to +Infinity in Fortran? At the moment I am using:

program test
  implicit none
  print *,infinity()
  real function infinity()
    implicit none
    real :: x
    x = huge(1.)
    infinity = x + x
  end function infinity
end program test

but I am wondering if there is a better way?


  • If your compiler supports ISO TR 15580 IEEE Arithmetic which is a part of so-called Fortran 2003 standard than you can use procedures from ieee_* modules.

    PROGRAM main
      USE ieee_arithmetic
      REAL :: r
      IF (ieee_support_inf(r)) THEN
        r = ieee_value(r,  ieee_negative_inf)
      END IF
      PRINT *, r
    END PROGRAM main