Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Checking for NaNs
Is there a simple way of checking for a NaN? Or do I have to check the
bit pattern and match it the the IEEE specification?

Thanks,

Simon Geard


Report this thread to moderator Post Follow-up to this message
Old Post
Simon Geard
12-21-04 01:57 PM


Re: Checking for NaNs
On Tue, 21 Dec 2004 09:44:27 +0000
Simon Geard <simon@quintic.co.uk> wrote:

> Is there a simple way of checking for a NaN? Or do I have to check the
>
> bit pattern and match it the the IEEE specification?
>
> Thanks,
>
> Simon Geard
>

Functions for dealling with ieee 754 arithmetic are  to be found in a
technical report (a sort of official extension to the standard)
to Fortran 95 and an optional part of the Fortran 2003 standard. If your
compiler supports the TR then you should be able to use the
ieee_arithmetic module which provides the logical elemental function
ieee_is_nan() which returns true if its argument is a NaN and
false otherwise.

If your compiler doesn't support the TR then it probably does have a
nonstandard extension function which tests for NaN. There's a pretty
high chance it's called isnan(). You need to check the documentation to
be sure.

If you are in the latter situation, I strongly recommend you implement a
trivial ieee_is_nan() as a wrapper to isnan() or whatever your
compiler's function is called. Put it in one place in your code
preferably with any other nonstandard routines you have to use. That way
you minimise the portability problem of using a nonstandard function,
and because you used the standard name you will be laughing if and when
your code is compiled on a compiler which supports the ieee stuff.

David

Report this thread to moderator Post Follow-up to this message
Old Post
David Ham
12-21-04 01:57 PM


RE: Checking for NaNs
In a previous article, Simon Geard <simon@quintic.co.uk> wrote:
>Is there a simple way of checking for a NaN? Or do I have to check the
>bit pattern and match it the the IEEE specification?
>
>Thanks,
>
>Simon Geard
>
What I did in one case (external data set gaps had NaNs as fill
rather than the usual impossible , but readable, value (e.g.
-ve or very large) was look at the bytes (4 byte floaating)
they were FF FF FF FF. So I just trapped those out by
reading into a buffer and reading them as integer first
to check.
The error didn't say NaN -- it said something else which I forget
- but it was obviously very upset!

Another source of NaNs (while reading "alien" data sets)
is endian-ness
Chris

Report this thread to moderator Post Follow-up to this message
Old Post
meek@skyway.usask.ca
12-21-04 08:59 PM


Re: Checking for NaNs
Simon Geard wrote:
> Is there a simple way of checking for a NaN? Or do I have to check the
> bit pattern and match it the the IEEE specification?


People have only given general answers because the question does
not include details such as what compiler is used and what machine
or system is used.  The current Fortran standard (F2003) contains a
portable way to do this, but implementations are not yet common.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies."   --  C. A. R. Hoare



Report this thread to moderator Post Follow-up to this message
Old Post
James Giles
12-21-04 08:59 PM


Re: Checking for NaNs
Simon Geard <simon@quintic.co.uk> wrote:
> Is there a simple way of checking for a NaN? Or do I have to check the
> bit pattern and match it the the IEEE specification?
>

Others have pointed to isnan(x) or ieee_is_nan(x), which I also
recommend to use if it is available. If such a thing is not available
then the easiest in my opinion is to test if(x.eq.x). That test should
fail on a NaN, but make sure it is actually carried out and not
optimized away. Put it in a separate function which is compiled
without optimization and check that the compiler doesn't throw it out
even without optimization.

--
Klaus Wacker              wacker@Physik.Uni-Dortmund.DE
Experimentelle Physik V   http://www.physik.uni-dortmund.de/~wacker
Universitaet Dortmund     Tel.: +49 231 755 3587
D-44221 Dortmund          Fax:  +49 231 755 4547

Report this thread to moderator Post Follow-up to this message
Old Post
Klaus Wacker
12-22-04 02:00 PM


Re: Checking for NaNs
Simon:   There is a little test program at:
http://ftp.cac.psu.edu/pub/ger/fortran/hdk/nan.f90

That may help you if your platform does not support
the ISNAN function.

Skip Knoble

On Tue, 21 Dec 2004 09:44:27 +0000, Simon Geard <simon@quintic.co.uk> wrote:

-|Is there a simple way of checking for a NaN? Or do I have to check the
-|bit pattern and match it the the IEEE specification?
-|
-|Thanks,
-|
-|Simon Geard


Herman D. (Skip) Knoble, Research Associate
(a computing professional for 38 years)
Email: SkipKnobleLESS at SPAMpsu dot edu
Web: http://www.personal.psu.edu/hdk
Penn State Information Technology Services
Academic Services and Emerging Technologies
Graduate Education and Research Services
Penn State University
214C Computer Building
University Park, PA 16802-21013
Phone:+1 814 865-0818   Fax:+1 814 863-7049

Report this thread to moderator Post Follow-up to this message
Old Post
Herman D. Knoble
12-22-04 09:04 PM


Re: Checking for NaNs
Skip Knoble said:
>Simon:   There is a little test program at:
>http://ftp.cac.psu.edu/pub/ger/fortran/hdk/nan.f90

Running the program with g95, the results are the same for
Compaq Visual Fortran, except that for case 3, g95 says

Y = Minus Zero
i= 3  Y= -0.0000000

and CVF says

Y = Minus Zero
i=           3  Y=  0.0000000E+00

I don't know enough about IEEE arithmetic to say if either or both
compilers are conforming in this case.


Report this thread to moderator Post Follow-up to this message
Old Post
beliavsky@aol.com
12-22-04 09:04 PM


Re: Checking for NaNs
beliavsky@aol.com wrote:
> Skip Knoble said: 
...
> and CVF says
>
> Y = Minus Zero
> i=           3  Y=  0.0000000E+00
>
> I don't know enough about IEEE arithmetic to say if either or both
> compilers are conforming in this case.

In order to actually print minus zero from CVF, you have to compile
with the /assume:minus0 flag.

--
J. Giles

"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies."   --  C. A. R. Hoare



Report this thread to moderator Post Follow-up to this message
Old Post
James Giles
12-23-04 02:13 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Fortran archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 08:12 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.