Re: Testing if a real is 'NaN'

From: Eric K. (ejko123_at_yahoo.com)
Date: 05/21/04


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



Relevant Pages

  • Re: cobol data format!!! urgent!!!
    ... misunderstood what "conformance to a standard" means. ... Cobol people 'know' binary floating-point is ... The quotation above is from IEEE 754. ... same as the IBM System/390 format which isn't the same as the Unisys MCP ...
    (comp.lang.cobol)
  • RfD: IEEE-FP 0.5.2
    ... the default format can be any IEEE binary ... restored to *nearly* the same as DPANS94 ... IEEE comparisons reworked ... This is a proposal for an optional Forth 200x word set, ...
    (comp.lang.forth)
  • Re: RfD: IEEE-FP
    ... although not given a name in IEEE. ... >> if the input load is too large for the default format. ... Conversion operations for binary formats 5.4.3.0 ... >> roundToIntegralExact ...
    (comp.lang.forth)
  • Re: RfD: IEEE-FP
    ... PROPOSAL FOR AN OPTIONAL IEEE 754, ... in the binary format of a nan, excluding the quiet bit, ... Note that the intel 80-bit format corresponds to binary64 ... roundTowardZero, need be implemented only if supported by the ...
    (comp.lang.forth)
  • Re: Representing Infinity
    ... >>I'd just be happy with a printed representation of the IEEE values. ... > Because infinities aren't rational numbers. ... So how do you represent the IEEE floating point infinite ...
    (comp.lang.lisp)