fortraninfinity# 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()
contains
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?

Solution

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
IMPLICIT NONE
REAL :: r
IF (ieee_support_inf(r)) THEN
r = ieee_value(r, ieee_negative_inf)
END IF
PRINT *, r
END PROGRAM main
```

- Why am I getting this error? <class 'TypeError'>: wrong type
- GNU Fortran - Function 'dcosd' has no IMPLICIT type
- f2py does not properly import, despite successfully compiling
- Error in Python trying to create a list of a certain length after a call to Fortran to get the length
- How to get command line arguments of unknown length in Fortran?
- implicit real - complex conversion in fortran
- Optimal way to create once, then frequently access to a large array in Fortran
- How do you iterate through an array in Fortran?
- Why Intel Fortran + Intel MPI report warn(error) when using MPI_Bcast?
- Convert c_int to default kind integer
- Program in Fortran print different results with each execution
- How does work the OpenMP "nonmonotonic:dynamic" schedule?
- GFortran error: ld: library not found for -lSystem when trying to compile
- Write unformatted (binary data) to stdout
- How to call Fortran's FINDLOC within numpy's f2py
- QR factorization in Fortran
- Distribution of for loop iterations over triangular matrix with MPI
- Why MPI_REDUCE shows different number at some array locations?
- Conditional compilation in gfortran
- Fortran with Sparse BLAS not flushing memory
- Standard conforming way to get command line arguments in FORTRAN77
- Fortran C interface for program containing non-c-interoperable derived type
- Processing a shared array by a passed index in a subroutine in a parallel loop
- Gfortran type mismatch error despite "-fallow-argument-mismatch" flag
- Removing whitespace in string
- Pointer to OpenBLAS subroutines in fortran
- Valgrind complains reading from a file
- Use FP exception traps (-ffpe-trap/-fpe0) for code linked against SIGFPE-unsafe library (libxml2)
- Reading missing data from a file
- Do most compilers optimize MATMUL(TRANSPOSE(A),B)?