Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageOn 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
Post Follow-up to this messageIn 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
Post Follow-up to this messageSimon 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
Post Follow-up to this messageSimon 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
Post Follow-up to this messageSimon: 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
Post Follow-up to this messageSkip 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.
Post Follow-up to this messagebeliavsky@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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.