Floating-Point Computations | 49
Patterns and
Domains
As you can readily determine, these three points are collinear on the liney= 5 *x.
When computing floating-point calculations, however, the errors inherent in
floating-point arithmetic affect the computation. Using floats as the values, the
calculation results in 0.00048828125; using doubles, the computed value is actu-
ally a very small negative number! Now we could introduce a small valueδto
determine≅between two floating-point values. Under this scheme, if |a–b|<δ,
then we consideraandbto be equal. However, it may be the case thatx≅yand
y≅z, but it might not be the case thatx≅z. This breaks the principle of Transitivity
and makes it really challenging to write correct code.
Special Quantities
While all possible 64-bit values could represent valid floating-point numbers, the
IEEE standard defines several values that are interpreted as special numbers (and
are often not able to participate in the standard mathematical computations, such
as ADD or MULT), shown in Table 3-3. These values have been designed to make
it easier to recover from common errors, such as divide by zero, square root of a
negative number, overflow of computations, and underflow of computations.
Note that the values of positive zero and negative zero are also included in this
table, even though they can be used in computations.
These special quantities are the result of computations that go outside the accept-
able bounds. For example, the quantity positive infinity could result from the Java
computationdouble x=1/0.0. As an interesting aside, the Java virtual machine
would throwjava.lang.ArithmeticExceptionif the statement had instead read
double x=1/0, since this expression computes the integer division of two numbers.
Performance
It is commonly accepted that computations over integer values will be more effi-
cient than their floating-point counterparts. Table 3-4 lists the computation times
of 10,000,000 operations on our high-end performance platform. A 1996 Sparc
Ultra-2 machine generated the values in the third column. As you can see, the
performance of individual operations can vary significantly from one platform to
another.
Table 3-3. Special IEEE 754 quantities
Special quantity 64-bit IEEE 754 representation
Positive infinity 0x7ff0000000000000L
Negative infinity 0xfff0000000000000L
Not a number (NaN) 0x7ff0000000000001L through
0x7fffffffffffffffL and
0xfff0000000000001L through
0xffffffffffffffffL
Negative zero 0x8000000000000000
Positive zero 0x0000000000000000
Algorithms in a Nutshell
Algorithms in a Nutshell By Gary Pollice, George T. Heineman, Stanley Selkow ISBN:
9780596516246 Publisher: O'Reilly Media, Inc.
Prepared for Ming Yi, Safari ID: [email protected]
Licensed by Ming Yi
Print Publication Date: 2008/10/21 User number: 594243
© 2009 Safari Books Online, LLC. This PDF is made available for personal use only during the relevant subscription term, subject to the Safari Terms of Service. Any other use