Graphics Reference
In-Depth Information
(
not-a-number
, sometimes also referred to as
indeterminate
or
indefinite
). Arithmetic
on this extended domain is referred to as
infinity arithmetic
. Infinity arithmetic is
noteworthy in that its use often obviates the need to test for and handle special-case
exceptions such as division-by-zero errors. Relying on infinity arithmetic does, how-
ever, mean paying careful attention to details and making sure the code does exactly
the right thing in every case.
Positive (
INF
) infinity originate either from a
calculation overflow or as a result of an expression limit. Overflow is generated when
a finite result is produced that is larger than the maximum number representable. For
example, overflow can be generated by dividing a very large number by a very small
number:
+
INF
, or just
INF
) and negative (
−
float a = 1.0e20;
float b = 1.0e-20;
floatc=a/b;
// gives c = +INF
floatd=-a/b;
// gives d = -INF
For an expression such as 1/0, the result can be made arbitrarily large by choosing a
corresponding small (positive) number as the denominator. As the expression tends
to infinity as the denominator tends to zero, the expression result is taken to be the
limit
INF
. Similarly, 1/
INF
is zero, as the result can be made arbitrarily close to zero
by choosing a sufficiently large value for the denominator. In some cases, such as for
0/0, no limit exists. In these cases, the result is indeterminate (
NaN
):
float a = 0.0f;
floatb=a/a;
// gives b = NaN (and _not_ 1.0f)
NaN
is also the result of operations such as
√
−
INF
. (There
are in fact several distinct
NaN
codes returned for these different operations, but this
is irrelevant to the discussion here.)
For comparisons, four mutually exclusive relations between IEEE-754 values are
possible: less than, equal to, greater than, and unordered. For any pair of values
exactly one of these relations is true. Both
INF
and
1, (
INF
−
INF
), and 0
·
INF
behave as expected with
respect to comparison operations, and thus for instance
INF
−
=
INF
,
−
INF
=−
INF
,
−
INF
all evaluate to true.
NaN
, however, is considered
unordered, which means that all comparisons involving
NaN
evaluate to false except
for
NaN
INF
<
INF
,
−
INF
<
0, and 0
<
NaN
(which is true). For arithmetic operations, if one or both operands
are
NaN
the result will also be
NaN
.
The introduction of
NaN
s is tricky in the sense that it leads to a trichotomy problem.
Previously it could be assumed that (exactly) one of
a
=
<
=
>
b
,
a
b
,or
a
b
was true.