What is NaN in C++?

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

NaN stands for Not a Number. This is an exception that is seen in the case if the output of the expression in a number is undefined. The C++ NaN is used with the floating-point operations.

Syntax

The syntax of the C++ NaN is as follows:

Parameter

The parameter of the C++ NaN is tagp. This function takes tags as the parameter value. This tag represents the implementation-specific-string.

Return Value

The return value of the C++ NaN is as follows:

  • Return value: NaN. When the given string is empty (" ") in the C++ NaN function, then the function returns a NaN value as the return value.

Example

Let u stake an example of C++ NaN to understand this function.

Output:

Explanation In the above example, first, we declared a variable of double type and in this variable, we used the C++ NaN and assigned the value nan 1 to it. Then we created another variable namely test. After that, we copied the variable var to the variable test. And finally, we are printing the output variable using the cout function.

When does NaN occur in C++?

The NaN in C++ can occur in the given conditions below:

  • When the log of a negative number or zero is taken.

  • Square root of the negative number.

  • Division by zero

Let us see these conditions by using an example.

Output:

Explanation: In the above example, we first declared the two variables of integer type. The first variable is x and the second variable is y. And then we assigned the value 36 and -15 to the variable x and y respectively. After that, we use the cout function to return the output and under this function, we use the sqrt function to return the square root of the given variables. Since we know that the C++ NaN function is sued for checking the complex or undefined number in the execution of the code. Here, the square root of the variable x can be simply calculated so the resultant output is the square root of the value given in the variable x that is 6. But in the case of the second variable, the square root of -15 can not be calculated. So, the resultant output is a -NaN value.

How to Check NaN in C++?

There are two methods using which we can check for NaN in C++. The first method is using the compare("==") operator and the second method is using the inbuilt function isnan(). Both these two methods are discussed below with examples and explanations of each.

Using compare (“==”) operator

In the compare method, we check the complexity of a given number by doing a comparison with the number itself. In this method, we check if a number is complex by comparing it with itself. If the result is true, then the number is not complex i.e., real. But if the result is false, then “nan” is returned, i.e. the number is complex. Let us see an example of this method.

Output:

Explanation: In the above example, we first declared two variables. The first variable is x which is of float type and we assigned the value 4 to it using the sqrt function with it. The second variable is y which is also of float type and we assigned the value -1 to it with the sqrt function to it. After that, we know that the C++ NaN returns a NaN value after comparing the is's value with itself. Here, we are comparing the value of the variable by using the built-in function compare (“==”) operator. In the case of the first variable x, the square root of 4 is 2. So, we use the resultant output as the calculated square root of 4. But in the case of the second variable, when we compare the value of the y by using the built-in function compare (“==”) operator the resultant output is a NaN because it is a complex value.

Using Inbuilt Function “isnan()”

We can also check the NaN in C++ by using the in-biult functionisnan(). This function also returns the boolean value according to the comparison between the given number and itself. But if the result of the comparison is false, it means the given number is not complex. And hence the value NaN is returned. Let us see an example of this method. This function is found in the header file namely <cmath>. Let us see an example of this method.

Output:

Explanation: In the above example, we first declared two variables. The first variable is x which is of float type and we assigned the value 4 to the variable x using the sqrt function with it. The second variable is y which is also of float type and we assigned the value -2 to the variable y with the sqrt function to it. After that, we know that the C++ NaN returns a NaN value after comparing the is's value with itself.

Here, we are comparing the value of the variable by using the built-in function isnan. In the case of the first variable x, the square root of 4 is 2. So, we the resultant output is the calculated square root of 4. But in the case of the second variable, when we compare the value of the y by using the built-in function isnan the resultant output is a NaN because it is a complex value (values of type float and long double).

FAQs

Given below are the frequently asked questions that we need to know are discussed below in a short and simple manner to understand the C++ NaN.

Q. Why does the NaN exist?

A: NaN happens because it is an assigned value in programming and neither an error nor an exception.

Q. How is NaN a number?

A: NaN is, by definition, the result of operations on undefined numerical values.

Q. How can we remove a NaN?

A: With the help of the built-in isnan() method, NaN can be eliminated. Before running the code, the programmer can verify the NaN value.

Q. What are the types of NaN?

A: There are two types of NaN: Quiet NaN and Signalling NaN. The first one is Quiet NaN and the second one is Signalling NaN. The Quiet NaN does do not result in any exception when we use this type of NaN with any arithmetic operation. But Signalling NaN, as the name suggests may indicate some exceptions when we use this type of NaN in any arithmetic operation.

Conclusion

  • NaN stands for Not a Number. This is an exception that is seen in the case of the output of the expression in a number is undefined.

  • The syntax of the C++ NaN is: double nan (const char* arg).

  • The parameter of the C++ NaN is tagp.

  • The NaN in C++ can occur in the given three conditions which are when the log of a negative number or zero is taken when we divide a number by zero, and the square root of the negative number.

  • We can check the C++ NaN in two methods. The first one is using the compare operator and the second method is using the isnan() function.

  • There are two types of NaN that are quiet NaN and signaling NaN.