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