A NaN result occurs when the result of an operation is an undefined or an unrepresentable number.
Consider this equation: y = 1 / x. What is the value of y as x approaches zero from the positive side? It will become an infinitely large positive value. What then is the value of y as x approaches zero from the negative side? It will become an infinitely large negative value. Mathematically, this is called a discontinuity, which cannot be resolved. As we approach zero from either direction, the result is a value that will be infinitely different when we approach from one direction or the other (an infinitely large positive value or an infinitely small negative value). Therefore, division by zero is mathematically undefined. In the computer, the result is NaN.
NaNs also occur when the data types are real, but the result of the computation is a complex number, for example, the square root of a negative number or the logarithm of a negative number....