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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

5.6 Modular Arithmetic in <strong>LEDA</strong> ( residual )<br />

1. Definition<br />

<strong>The</strong> data type residual provides an implementation of exact integer arithmetic using modular<br />

computation. In contrast to the <strong>LEDA</strong> type integer which offers similar functionality<br />

as residual, the user of residual has to specify for each calculation the maximal bit length<br />

b of the integers she wants to be exactly representable by residuals. This is done by a<br />

call of residual :: set maximal bit length(b) preceding the calculation. <strong>The</strong> set of integers<br />

in the interval [−2 b , 2 b ) is called the current range of numbers representable by residuals.<br />

A residual number x that is outside the current range is said to overflow. As an effect of<br />

its overflow, certain operations cannot be applied to x and the result is undefined. <strong>The</strong>se<br />

critical operations include e.g. all kinds of conversion, sign testing and comparisons. It<br />

is important to realize that for an integer x given by a division-free expression it only<br />

matters whether the final result x does not overflow. This is sometimes useful and hence<br />

overflow is not always checked by default.<br />

Division is available for residuals, but with certain restrictions. Namely, for each division<br />

x/y the user has to guarantee at least one of the following two conditions:<br />

• y.is invertible( ) is true<br />

• x/y is integral and x and y do not overflow.<br />

If the first condition is satisfied, there is an alternative way to do the division x/y. Introducing<br />

the residual variable z = y.inverse( ), the call x/y is equivalent to the call<br />

x ∗ z. <strong>The</strong> latter form is advantageous if several divisions have the same divisor y because<br />

here the time-consuming inversion of y, which is implicit in the division x/y, has to be<br />

performed only once.<br />

If the result of an operation is not integral, the computation will usually proceed without<br />

warning. In such cases the computation produces a nonsensical result that is likely to overflow<br />

but otherwise is a perfect residual. However, the operations mentioned above check<br />

for overflow. Note that the implemented overflow checks are not rigorous, detecting invalidity<br />

only with empirically high probability. Overflow checking can be switched off by calling<br />

set maximal bit length with a second, optional parameter residual ::no overflow check.<br />

#include < <strong>LEDA</strong>/numbers/residual.h >

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

Saved successfully!

Ooh no, something went wrong!