I\'ve been trying to find a standards-compliant way to check for Infinite and NaN values in Fortran 90/95 but it proved harder than I thought.
For Fortran, 1/infinity=0 thus, divide your variable by zero i.e
program test
implicit none
real :: res
integer :: i
do i=1,1000000
res=-log((1.+(10**(-real(i))))-1.)
print*,i,res
if ((1./res)==0.) then
exit
end if
end do
end program
there's your infinity check. No complication necessary.
The simple way without using the ieee_arithmatic
is to do the following.
Infinity: Define your variable infinity = HUGE(dbl_prec_var)
(or, if you have it, a quad precision variable). Then you can simply check to see if your variable is infinity by if(my_var > infinity)
.
NAN: This is even easier. By definition, NAN is not equal to anything, even itself. Simply compare the variable to itself: if(my_var /= my_var)
.
No.
The salient parts of IEEE_ARITHMETIC for generating/checking for NaN's are easy enough to write for gfortran for a particular architecture.
for testing NaN none of the things worked eg.if testing real s2p to see if it is NaN then
if(isnan(s2p))
did not work in gfortran nor did
if(s2p.ne.s2p).
The only thing that worked was
if(.not.s2p<1.and..not.s2p>1)
though to make real sure u may want to add
if(.not.s2p<1.and..not.s2p>1.and..not.s2p==1)
No.
Neither is there a standards-compliant way of checking for infinities or NaNs in Fortran 90/95, nor can there be a standards-compliant way. There is no standards-compliant way of defining either of these quasi-numbers in Fortran 90/95.
I have used:
PROGRAM MYTEST
USE, INTRINSIC :: IEEE_ARITHMETIC, ONLY: IEEE_IS_FINITE
DOUBLE PRECISION :: number, test
number = 'the expression to test'
test = number/number
IF (IEEE_IS_FINITE(test)) THEN
WRITE(*,*) 'We are OK'
ELSE
WRITE(*,*) 'Got a problem'
END IF
WRITE(*,*) number, test
END PROGRAM MYTEST
This will print 'Got a problem' for number = 0.0D0, 1.0D0/0.0D0, 0.0D0/0.0D0, SQRT(-2.0D0), and also for overflows and underflows such as number = EXP(1.0D800) or number = EXP(-1.0D800). Notice that generally, things like number = EXP(1.0D-800) will just set number = 1.0 and produce a warning at compilation time, but the program will print 'We are OK', which I find acceptable.
OL.