What is a NaN?

Due to hardware limitations and the finite nature of computer arithmetic, certain numeric values cannot be represented properly. These values include infinity, negative infinity, and so on. The limits of floating point arithmetic also cause certain bit patterns to be meaningless or illegal. Such values are collectively known as NaN (“not a number”) values. The ISO Standard recognizes two types of NaN values: a quiet NaN and a signaling NaN. The latter causes a hardware exception to be thrown if a NaN value is used in an expression. A quiet NaN doesn't generate such an exception. The Standard Library defines function templates that identify these values:
using namespace std;
double value1, value2;

This distinction is useful because sometimes you don't need to generate an exception when a NaN is encountered. For example, when you are calculating and displaying a graph, you do not want to generate an error when a variable's value is infinity, you simply ignore it. On the other hand, in some calculations, an exception must be raised, e.g., when a number is divided by zero. In such cases, a signaling NaN is useful.

No comments: