31.01.2014 Views

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

Version 5.0 The LEDA User Manual

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

5.9 A Floating Point Filter ( floatf )<br />

1. Definition<br />

<strong>The</strong> type floatf provides a clean and efficient way to approximately compute with large<br />

integers. Consider an expression E with integer operands and operators +, −, and ∗,<br />

and suppose that we want to determine the sign of E. In general, the integer arithmetic<br />

provided by our machines does not suffice to evaluate E since intermediate results might<br />

overflow. Resorting to arbitrary precision integer arithmetic is a costly process. An<br />

alternative is to evaluate the expression using floating point arithmetic, i.e., to convert the<br />

operands to doubles and to use floating-point addition, subtraction, and multiplication.<br />

Of course, only an approximation E ′ of the true value E is computed. However, E ′ might<br />

still be able to tell us something about the sign of E. If E ′ is far away from zero (the<br />

forward error analysis carried out in the next section gives a precise meaning to ”far<br />

away”) then the signs of E ′ and E agree and if E ′ is zero then we may be able to conclude<br />

under certain circumstances that E is zero. Again, forward error analysis can be used to<br />

say what ‘certain circumstances’ are.<br />

<strong>The</strong> type floatf encapsulates this kind of approximate integer arithmetic. Any integer (=<br />

object of type integer) can be converted to a floatf; floatfs can be added, subtracted,<br />

multiplied, and their sign can be computed: for any floatf x the function Sign(x) returns<br />

either the sign of x (−1 if x < 0, 0 if x = 0, and +1 if x > 0) or the special value<br />

NO IDEA. If x approximates X, i.e., X is the integer value obtained by an exact<br />

computation, then Sign(x)! = NO IDEA implies that Sign(x) is actually the sign of X<br />

if Sign(x) = NO IDEA then no claim is made about the sign of X.<br />

#include < <strong>LEDA</strong>/numbers/floatf .h ><br />

2. Creation<br />

floatf x; introduces a variable x of type floatf and initializes it with zero.<br />

floatf x(integer i); introduces a variable x of type floatf and initializes it with integer<br />

i.<br />

3. Operations<br />

floatf<br />

floatf<br />

floatf<br />

const floatf & a + const floatf & b<br />

Addition.<br />

const floatf & a − const floatf & b<br />

Subtraction.<br />

const floatf & a ∗ const floatf & b<br />

Multiplication.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!