Re: Testing if a real is 'NaN'
From: Eric K. (ejko123_at_yahoo.com)
Date: 05/21/04
- Next message: TimC: "Re: Interpretion of file name suffixes"
- Previous message: Victor Eijkhout: "Re: Nested Factorization"
- In reply to: Martin Buchmann: "Re: Testing if a real is 'NaN'"
- Next in thread: Martin Buchmann: "Re: Testing if a real is 'NaN'"
- Reply: Martin Buchmann: "Re: Testing if a real is 'NaN'"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 20 May 2004 16:39:00 -0700
Martin_Buchmann@gmx.net (Martin Buchmann) wrote in message news:<1ge357j.128hjz21cpgosgN%Martin_Buchmann@gmx.net>...
> Futhermore, i guess the best way would be to check the calling arguments
> of my function so that the whole thing is avoided at all. An idea which
> came to my mind after posting. Some things are just too simple ;-)
If this is fast and easy to do, then it's certainly the best way to proceed.
For what it's worth, I'll post the basic fragment of my general function
for checking for IEEE infinities. The function is elemental, so you can
apply it to arrays (useful for checking for garbage input). Herman Knoble's
example code also will work, but the advantage of my approach is that
you don't have to worry about magic bit patterns, which can be problematic
when porting codes to machines with different endian conventions. (Endian
issues apply to the ordering of bits within bytes as well as to bytes within
words.) Also, my code is the same for both single and double precision;
just change the declaration of x. You can package this up into a module and
add a generic interface (email me for details if you don't know how to do this).
The following function returns one of 0, 1, or 2, according as its argument
is a normalized real, an infinity, or not-a-number, respectively.
elemental integer function typeof_real(x)
real,intent(in):: x
integer,parameter:: FINITE_REAL=0, INFINITE_REAL=1, NAN_REAL=2
if(exponent(x).gt.maxexponent(x)) then
typeof_real=merge(INFINITE_REAL, NAN_REAL, abs(fraction(x)).eq.0.5)
else
typeof_real=FINITE_REAL
endif
return
end function
You can extend this idea in a similar way to check for denormalized numbers,
too. The test is
if(exponent(x).lt.minexponent(x).and.abs(fraction(x)).ne.0.5) then
! x is denormalized
endif
Again, I make no claim that these functions are foolproof, but in my
experience, optimizers aren't likely to elide the tests and render them
useless. Note also that they apply *only* to IEEE single and double format.
The formats for quadruple-precision numbers are not (yet) standardized.
I'm not sure that the IEEE standard specifies how infinities and NaNs are
represented in the double-extended ("real*10") format, so these functions
may or may not work in that case (I haven't tested).
> P.S.: Oh, Eric i'm sorry that my posting was the cause that you were
> insulted by Mr. Thomas. I don't understand at all why some people abuse
> usenet postings to find compensation for whatever went wrong in their
> lifes.
Don't worry about it. In a technical forum like comp.lang.fortran, it's
pretty easy to spot the cranks and the fools. I just ignore them and advise
you to do the same.
--Eric
- Next message: TimC: "Re: Interpretion of file name suffixes"
- Previous message: Victor Eijkhout: "Re: Nested Factorization"
- In reply to: Martin Buchmann: "Re: Testing if a real is 'NaN'"
- Next in thread: Martin Buchmann: "Re: Testing if a real is 'NaN'"
- Reply: Martin Buchmann: "Re: Testing if a real is 'NaN'"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|