C and C++ nan :myths and conception


C and C++ nan

The term nan in C and C++ mean ‘Not a Number‘.It is primarily used as a resulting value for some mathematical operations under some diverge condition.Say for instance when calculating a square root of a number,it is a known acknowledgement that we cannot calculate a square root of a negative number;it is unaccustomed.But there are always some hyper-curious people(like me) who just wants to dive into an incomprehensible matter and try to evaluate the square root of negative value.After running the code and looking at the result I found that the output value is ‘nan‘.We know square root of negative value is incalculable and so the output is ‘nan’-Not a number,no doubt here the output make sense in every ways.

Note include <cmath> header file.

Code example:for C++ programmer

cout<< sqrt(-2) ;

Output,

nan

Code example:for C programmer

printf( “\n\n%lf” , sqrt(-2) );

Output in Visual Studio,

-nan(ind)

The existence of nan in C++ makes things simpler.There are many mathematical functions which are only operable under some specified values.In using these functions if any of the supplied argument is not consistent with the function,the compiler happily outputs ‘nan’ as the result.In this way we instantly know that the function cannot be evaluated with the supplied argument.

Another way of looking at ‘nan’ is,functions returning ‘nan’ as a result is a way of telling the programmer that a domain error has occured.A domain error occur when the function cannot be evaluated for the supplied argument(we will see more about domain error in another post).So if we get nan we can be sure that something is wrong with the argument supplied.Well not to mentioned this makes debugging and correcting our code easier.

Here are some more code example of functions yielding an output as ‘nan’.

Code example:for C++ programmer

cout<< log10(-2) << endl ///log10 calculates the base-10 logarithm
<< asin(4) ; ///asin calculates arc sine

Output

nan
nan

Code example:for C programmer

printf(“\n\n%lf”, log10(-2) ) ;
printf(“\n\n%lf”, asin(4) ) ;

Output in Visual Studio,

-nan(ind)

-nan(ind)

log10 cannot be computed for negative value hence log10(-2) gives domain error and yield ‘nan’ .Similarly, asin can be computed only for values in the range [+1,-1],providing all other values will yield ‘nan’ signaling domain error.

Link :C++ cmath log10

Link :C++ cmath asin




nan,nanf and nanl function (C++11)

C++11 supply some nan functions that returns a quite nan for the argument supplied.The functions declaration are shown below.

1 float nanf(const char* x);
2double nan(const char* x);
3 long double nanl(const char* x);

All the functions accept const char* type and returns a quite nan.The only difference between the three functions is:

nanf() returns a float type quite nan
nan() returns double type quite nan and
nanl( ) returns long double quite nan.

Note include <typeinfo> header.

Code example:For C++ programmer

cout<< nanf(“string”) << endl
<< nan(“string”) << endl
<< nanl(“string”) << endl ;

cout<< typeid( nanf(“string”) ).name( ) << endl
<< typeid( nan(“string”) ).name( ) << endl
<< typeid( nanl(“string”) ).name( ) ;

Output

nan
nan
nan
f (means ‘float’)
d (means ‘double’)
e (means ‘long double’)

Code example:For C programmer

char c[] = “New” ;

printf(“%lf”, nan(c) ); //calls the 2nd version
printf(“\n%f”, nanf(c) ); //calls the 1st version
printf(“\n%Lf”, nanl(c) ); //calls the 3rd version

/*printf(“%lf”, nan(2345)); //error! nan accept only string type argument */

Output in Visual Studio,

nan
nan
nan