0

I was reading a book about IEEE standard. and I countered to this part in p43(58):

More generally, NaNs provide a very convenient way for a programmer to handle the possibility of invalid data or other errors in many contexts. Suppose we wish to write a program to compute a function that is not defined for some input values. By setting the output of the function to NaN if the input is invalid or some other error takes place during the computation of the function, the need to return special error messages or codes is avoided.

I tried to return NAN but that doesn't seem to be right. more generally I want to know how to use NAN in practice in code. there isn't much information about this topic out there.

UPDATE:

for clarifying things I have to ask this:

How to define some function like sqrt which for some input like sqrt(-1) is undefined using NAN? I don't see how this can heppen because returning NAN prints nan as output.

as the book say in the same page:

The square root operation provides a good example of the use of NaNs. Before the IEEE standard, an attempt to take the square root of a negative number might resultonly in the printing of an error message and a positive result being returned. The user might not notice that anything had gone wrong. Under the rules of the IEEEstandard, the square root operation is invalid if its argument is negative, and the standard response is to return a NaN

c_nerd
  • 23
  • 4

1 Answers1

1

NaNs are generated by any kind of invalid floating point calculation, such as doing 0/0 or Inf-Inf. This can be an unexpected result of performing calculations using certain values.

There are multiple ways in which this is may be used in programs. For simple, text or console based applications, the result of your calculation may be normally printed as-is using printf and the "%f" format-string. If something went wrong and the calculation resulted in a NaN, printf detects this and prints NaN instead of any actual number.

In other cases, you might want to detect yourself whether the result is NaN. But then there are multiple NaN representations so it is not enough to check if(num == NAN). However, you could use the following idiom:

if(num == num)
    printf("Calculation valid!"); 
else
    printf("Calculation invalid!");

On any C compiler compliant to IEEE754 floating point standard, the expression (num == num) evaluates to false iff num is a float/double containing one of the NaN representations. Another option is to use the isnan(num) standard library function from math.h.

If you want to generate a NAN, you could use nan("") from math.h or strtod("NAN"). Keep in mind that this is only one of various NaN representations!

EDIT: Fixed incorrect attribution to C standard for (num != num); Thanks @chux!

th33lf
  • 2,177
  • 11
  • 15
  • 1
    C also allows `num == num` to be true when `num` is a NaN - it is implementation defined. The "guarantee" one needs is not in C, but IEEE math, something not guaranteed by C. – chux - Reinstate Monica Aug 16 '19 at 11:17
  • @chux Oh yes, thanks for pointing out! The guarantee can be perhaps rephrased as being given by IEEE754 compliant C compilers? – th33lf Aug 16 '19 at 11:24
  • 1
    Perhaps. Compliance to IEEE754 is rarely 100%. Still many C compilers/platforms are very IEEE754-like. NAN is one area that is often marginally supported correctly. `isnan(num)` is the best solution. – chux - Reinstate Monica Aug 16 '19 at 11:28