Section 4.13 Special Values
There are two special values that can be represented by floating-point numbers you should be aware of.
The first is infinity, which is represented by the constant
INFINITY
. This is used to represent values that are too large to be stored in a floating-point number. Recall that double
s can store enormous values, so you are likely only to run into INFINITY
if you do some math that results in an answer that approaches infinity like dividing by 0 or trying to calculate \(tan(\frac{\pi}{2})\text{.}\)
The second is Not a Number, which is represented by the constant
NAN
. This is used to represent values that are not real numbers, such as the result of taking the square root of a negative number. NAN
is not equal to any value, including itself, so the only way to check if a value is βNot a Numberβ is to use the isnan
function from the cmath
library.
Once you have a value that is one of these special values, doing more math with it will generally just result in the same special value. Any number +
INFINITY
is INFINITY
. NAN
+ a number is still NAN
. So they are mostly useful as indicators that some bit of math has gone wrong. In this book we will never attempt to calculate either of these or rely on doing anything with these values. So if you ever see nan
or inf
as the result of outputting a floating-point number, go double check your math!
You have attempted of activities on this page.