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.4 <strong>The</strong> data type real ( real )<br />

1. Definition<br />

An instance x of the data type real is a real algebraic number. <strong>The</strong>re are many ways<br />

to construct a real: either by conversion from double, bigfloat, integer or rational, by<br />

applying one of the arithmetic operators +, −, ∗, / or d √ to real numbers or by using the<br />

⋄-operator to define a real root of a polynomial over real numbers. One may test the<br />

sign of a real number or compare two real numbers by any of the comparison relations<br />

=, ≠, and ≥. <strong>The</strong> outcome of such a test is mathematically exact. We give<br />

consider an example expression to clarify this:<br />

x := ( √ 17 − √ 12) ∗ ( √ 17 + √ 12) − 5<br />

Clearly, the value of x is zero. But if you evaluate x using double arithmetic you obtain<br />

a tiny non-zero value due to rounding errors. If the data type real is used to compute<br />

x then sign(x) yields zero. 1 <strong>The</strong>re is also a non–standard version of the sign function:<br />

the call x.sign(integer q) computes the sign of x under the precondition that |x| ≤ 2 −q<br />

implies x = 0. This version of the sign function allows the user to assist the data type in<br />

the computation of the sign of x, see the example below.<br />

<strong>The</strong>re are several functions to compute approximations of reals. <strong>The</strong> calls x.to bigfloat( )<br />

and x.get bigfloat error( ) return bigfloats xnum and xerr such that |xnum − x| ≤<br />

xerr. <strong>The</strong> user may set a bound on xerr. More precisely, after the call<br />

x.improve approximation to(integer q) the data type guarantees xerr ≤ 2 −q . One can<br />

also ask for double approximations of a real number x. <strong>The</strong> calls x.to double( ) and<br />

x.get double error( ) return doubles xnum and xerr such that |xnum − x| ≤ xerr. Note<br />

that xerr = ∞ is possible.<br />

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

2. Types<br />

typedef polynomial Polynomial<br />

the polynomial type.<br />

3. Creation<br />

reals may be constructed from data types double, bigfloat, long, int and integer. <strong>The</strong><br />

default constructor real( ) initializes the real to zero.<br />

4. Operations<br />

double x.to double( ) returns the current double approximation of x.<br />

bigfloat x.to bigfloat( ) returns the current bigfloat approximation of x.

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

Saved successfully!

Ooh no, something went wrong!