Graphics Reference
In-Depth Information
However, the assumption that
a
>
b
implies
¬
(
a
≤
b
) does not hold for
NaN
s. Using
infinity arithmetic, the statement
if (a > b) x; else y;
is
not
equivalent to
if (a <= b) y; else x;
NaN
s can therefore slip through range checks of the type illustrated in the following
code.
// Test if val is in the range [min..max]
int NumberInRange(float val, float min, float max)
{
if (val < min || val > max) return 0;
// Here val assumed to be in [min..max] range, but could actually be NaN
...
return 1;
}
The safe way of writing this test is as follows.
// Test if val is in the range [min..max]
int NumberInRange(float val, float min, float max)
{
if (val >= min && val <= max) {
// Here val guaranteed to be in [min..max] range (and not be NaN)
...
return 1;
} else return 0;
}
As an example of how a division-by-zero check can be avoided through infinity
arithmetic, consider the problem of intersecting a segment
S
with a plane
P
, as dis-
cussed in Section 5.3.1. Let
P
be given by (
n
·
X
)
=
d
and
S
by the parametric equation
S
(
t
)
=
A
+
t
(
B
−
A
) for 0
≤
t
≤
1. The
t
value for intersection of the segment with
the plane is given by
A
)
(
n
t
=
(
d
−
n
·
·
(
B
−
A
)),