C++ <cmath> - nan() Function
The C++ <cmath> nan() function returns a quiet NaN (Not-A-Number) value of type double. The NaN values are used to identify undefined or non-representable values for floating-point elements, such as the square root of negative numbers or the result of 0/0. The argument can be used by library implementations to distinguish different NaN values in a implementation-specific manner.
Similarly, nanf and nanl return NaN values of type float and long double, respectively.
Syntax
double nan(const char* arg); float nanf(const char* arg); long double nanl(const char* arg);
Parameters
arg |
Specify an implementation-specific C-string identifying the contents of a NaN. |
Return Value
Returns a quiet NaN value that corresponds to the identifying string arg or zero if the implementation does not support quiet NaNs.
Example:
The example below illustrates on nan() function.
#include <iostream> #include <cmath> using namespace std; int main (){ float x = nanf("0/0"); double y = nan("1"); long double z = nanl("2"); cout<<"nanf(0/0) = "<<x<<"\n"; cout<<"nan(\"1\") = "<<y<<"\n"; cout<<"nanl(\"2\") = "<<z<<"\n"; return 0; }
The output of the above code will be:
nanf(0/0) = nan nan("1") = nan nanl("2") = nan
❮ C++ <cmath> Library