Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

The best way to get infinity with gfortran

1,923 views
Skip to first unread message

Peng Yu

unread,
Jun 14, 2015, 11:32:01 PM6/14/15
to
Hi,

http://stackoverflow.com/questions/5010538/infinity-in-fortran

The above URL shows the following as a way to generate infinity. I am wondering what is the best way with gfortran.

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


This is mentioned, but gfortran does not support it.

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


This shows up as errors in gfortran.


double precision,parameter :: inf = 1.d0/0.d0


real :: sng
double precision :: dbl1,dbl2

sng = 1.0/0.0
dbl1 = 1.d0/0.d0
dbl2 = -log(0.d0)

if(sng == inf) write(*,*)"sng = inf"
if(dbl1 == inf) write(*,*)"dbl1 = inf"
if(dbl2 == inf) write(*,*)"dbl2 = inf"
read(*,*)

This assumes the number of bits of the number, which is not robust.

PROGRAM infinity
IMPLICIT NONE
INTEGER :: inf
REAL :: infi
EQUIVALENCE (inf,infi) !Stores two variable at the same address
DATA inf/z'7f800000'/ !Hex for +Infinity
WRITE(*,*)infi
END PROGRAM infinity

Regards,
Peng

kargl

unread,
Jun 15, 2015, 12:22:39 AM6/15/15
to
Peng Yu wrote:


>
> This is mentioned, but gfortran does not support it.
>
> 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

gfortran has supported the IEEE modules for awhile now. Please
don't spread mis-information.

% cat mn.f90

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
% gfc -o z mn.f90
% ./z
-Infinity

--
steve

FX

unread,
Jun 15, 2015, 4:50:54 AM6/15/15
to
> The above URL shows the following as a way to generate infinity. I am
> wondering what is the best way with gfortran.

With recent versions of gfortran and other compilers, I recommend using
the IEEE_VALUE intrisic from IEEE_ARITHMETIC. With older compilers, I
would suggest reading it from a string:

$ cat a.f90
program test
implicit none
print *, sqrt(infinity()), erf(infinity()), exp(-infinity())
contains
real function infinity()
implicit none
character(len=3) :: inf = "INF"
read(inf,*) infinity
end function
end program

$ gfortran a.f90 && ./a.out
Infinity 1.00000000 0.00000000

--
FX

Richard Maine

unread,
Jun 15, 2015, 11:24:38 AM6/15/15
to
Hmm. The bit about the characters "INF" reading as an infinity is in the
same version of the standard (f2003) as the IEEE features. I wouldn't
count on it necessarily working in "older" compilers, where "older"
means any compiler not claining full conformance with f2003.

(See my many previous rants about depending on lists of individual
features in lieu of statements of support of a version of the standard
as a whole. I hadn't thought of this particular feature, but it fits the
general mold.)

There really was not a good portable way to guarantee getting an
infinity prior to f2003 (even aside from the matter of there not being a
guarantee that all systems even supported such a thing as an infinity at
all). I took a glance at the cited page on stackoverflow and was
impressed (negatively) at several of the replies giving what I thought
was poor advice, or at least advice that seemed based on a narrow
breadth of experience.

--
Richard Maine
email: last name at domain . net
dimnain: summer-triangle

FX

unread,
Jun 15, 2015, 11:29:06 AM6/15/15
to
> Hmm. The bit about the characters "INF" reading as an infinity is in
> the same version of the standard (f2003) as the IEEE features. I
> wouldn't count on it necessarily working in "older" compilers, where
> "older" means any compiler not claining full conformance with f2003.

Most F95 compilers I have had access to in the past decade supported it,
and I've used it in my own codes to a large extent. Certainly it has
worked for a long time with gfortran and Intel, long before the
availability of the IEEE modules.

--
FX

Peng Yu

unread,
Jun 15, 2015, 7:07:02 PM6/15/15
to
Here is what I have. Why I am not able to compile it with gfortran?

$ cat main.f90

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
$ gfortran -fcheck=all -Wall -ffree-line-length-none -fimplicit-none -o ./main.exe main.f90
main.f90:4.6:

USE ieee_arithmetic
1
Fatal Error: Can't open module file 'ieee_arithmetic.mod' for reading at (1): No such file or directory
$ gfortran --version
GNU Fortran (Homebrew gcc 4.9.2_1 --with-all-languages) 4.9.2
Copyright (C) 2014 Free Software Foundation, Inc.

GNU Fortran comes with NO WARRANTY, to the extent permitted by law.
You may redistribute copies of GNU Fortran
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING


dpb

unread,
Jun 15, 2015, 7:34:04 PM6/15/15
to
On 06/15/2015 6:06 PM, Peng Yu wrote:
> On Sunday, June 14, 2015 at 11:22:39 PM UTC-5, kargl wrote:
...

> Here is what I have. Why I am not able to compile it with gfortran?
>
> $ cat main.f90
>
> 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
...

> USE ieee_arithmetic
> 1
> Fatal Error: Can't open module file 'ieee_arithmetic.mod' for reading at (1): No such file or directory
...

Probably because you haven't compile the module code. I don't know what
gfortran named the module, but to USE any module, the source containing
it must have been already compiled and that output (normally a .mod
file) must be in the search path set up for the compiler.

--

Richard Maine

unread,
Jun 15, 2015, 8:04:48 PM6/15/15
to
dpb <no...@non.net> wrote:

> On 06/15/2015 6:06 PM, Peng Yu wrote:
> > Fatal Error: Can't open module file 'ieee_arithmetic.mod' for reading at
(1): No such file or directory
> ...
> Probably because you haven't compile the module code.

Nah. That's an intrinsic moodule - not user code that you can compile.
And the IEEE intrinsic modules do some very special things that cannot
be done by Fortran code. Sort of like some intrinsic procedures do
things that user-written ones can't do.

More likely because gfortran doesn't appear to have implemented that
module until version 5, while the Op (and me, for that mater) has only
4.9.2.

kargl

unread,
Jun 15, 2015, 8:24:18 PM6/15/15
to
> END PROGRAM main
> $ gfortran -fcheck=all -Wall -ffree-line-length-none -fimplicit-none -o ./main.exe main.f90
> main.f90:4.6:
>
> USE ieee_arithmetic
> 1
> Fatal Error: Can't open module file 'ieee_arithmetic.mod' for reading at (1): No such file or directory
> $ gfortran --version
> GNU Fortran (Homebrew gcc 4.9.2_1 --with-all-languages) 4.9.2

You need a newer version of the compiler.

You can get an infinity with

% cat qw.f90
program foo
real, parameter :: infty = 1. / 0.
print *, infty
end program foo

if you also use the -fno-range-check option

% gfc49 -o z -fno-range-check qw.f90 && ./z
Infinity

% gfc49 -o z qw.f90
qw.f90:2.32:

real, parameter :: infty = 1. / 0.
1
Error: Division by zero at (1)

--
steve

Peng Yu

unread,
Jun 15, 2015, 9:44:34 PM6/15/15
to
Which version of GCC will work?

kargl

unread,
Jun 15, 2015, 10:52:36 PM6/15/15
to
Peng Yu wrote:
>> Peng Yu wrote:
>>
>> > $ gfortran -fcheck=all -Wall -ffree-line-length-none -fimplicit-none -o ./main.exe main.f90
>> > main.f90:4.6:
>> >
>> > USE ieee_arithmetic
>> > 1
>> > Fatal Error: Can't open module file 'ieee_arithmetic.mod' for reading at (1): No such file or directory
>> > $ gfortran --version
>> > GNU Fortran (Homebrew gcc 4.9.2_1 --with-all-languages) 4.9.2
>>
>> You need a newer version of the compiler.
>
> Which version of GCC will work?
>

You need 5.1. Yes, the version number seems confusing.
The GCC project changed how version numbers are done.
5.1 was released on April 22, 2015. Check the gfortran
wiki about different releases.

--
steve

John Harper

unread,
Jun 16, 2015, 6:29:25 PM6/16/15
to
If you don't have the opportunity to update your gfortran, note that the
following program works with gfortran 4.9.0 and (I suspect but can't prove
because of a disaster yesterday) with earlier versions. It also works with
g95 0.94 and Sun Fortran 95 8.7.

program naninf
character(4) :: infnan(3) = (/'Inf ','-Inf','Nan '/)
real infplus,infminus,nan
read(infnan,*) infplus,infminus,nan
print *, infplus,infminus,nan
end program naninf

By the way, infnan must be a variable not a constant. Declaring it by

character(4),parameter :: infnan(3) = (/'Inf ','-Inf','NaN '/).

and using it in an internal read is contrary to all three of the f95, f2003
and f2008 standards, but only g95 complains if you try it. As the
prohibition is not in a Constraint, it's not a bug but a quality-of-
implementation issue for the other compilers.

Of course Inf and NaN in input are an F2003 feature, but gfortran and g95
both allowed it as an f95 extension even when I used the std=f95 option.

FX

unread,
Jun 17, 2015, 3:58:12 AM6/17/15
to
> By the way, infnan must be a variable not a constant. Declaring it by
> character(4),parameter :: infnan(3) = (/'Inf ','-Inf','NaN '/).
> and using it in an internal read is contrary to all three of the f95, f2003
> and f2008 standards, but only g95 complains if you try it.

gfortran does, but only for scalar variables apparently...


$ cat a.f90
program test
implicit none
print *, sqrt(infinity()), erf(infinity()), exp(-infinity())
contains
real function infinity()
implicit none
character(len=3), parameter :: inf = "INF"
read(inf,*) infinity
end function
end program

$ gfortran a.f90
a.f90:8:9:

read(inf,*) infinity
1
Error: UNIT specification at (1) must be an INTEGER expression or a
CHARACTER variable

--
FX
0 new messages