next up previous contents index
Next: Algebraic Real Numbers ( Up: Number Types and Linear Previous: Rational Numbers ( rational   Contents   Index


Big Floatingpoint Numbers ( bigfloat )

Definition

In general a bigfloat is given by two integers s and e where s is the significant and e is the exponent. The tuple (s, e) represents the real number s*2e. In addition, there are the special bigfloat values NaN (not a number), pZero, nZero (= + 0, - 0), and pInf, nInf ( = + $ \infty$, - $ \infty$). These special values behave as defined by the IEEE floating point standard.

In particular, 5/ + 0 = $ \infty$, -5/ + 0 = - $ \infty$, $ \infty$ + 1 = $ \infty$, 5/$ \infty$ = + 0, + $ \infty$ + (- $ \infty$) = NaN and 0*$ \infty$ = NaN.

Arithmetic on bigfloats uses two parameters. The precision prec of the result (in number of binary digits) and the rounding mode mode. Possible rounding modes are:

Operations +, -, * work as follows. First, the exact result z is computed. If the rounding mode is EXACT then z is the result of the operation. Otherwise, let s be the significant of the result; s is rounded to prec binary places as dictated by mode. Operations / and $ \sqrt{\ \ }$ work accordingly except that EXACT is treated as TO_NEAREST.

The parameters prec and mode are either set directly for a single operation or else they are set globally for every operation to follow. The default values are 53 for prec and TO_NEAREST for mode.

#include < LEDA/bigfloat.h >

Creation

A bigfloat may be constructed from data types double, long, int and integer, without loss of accuracy. In addition, an instance of type bigfloat can be created as follows.

bigfloat x(integer s, integer e) introduces a variable x of type bigfloat and initializes it to s*2e

Operations

The arithmetical operators +, -, *, /, +=, -=, *=, /=, sqrt, the comparison operators <, < =, >, > = , =, ! = and the stream operators << and >> are available. Addition, subtraction, multiplication, division and square root are implemented by the functions add, sub, mul, div and sqrt, respectively. For example, the call

add(x, y, prec, mode, is_exact)
computes the sum of bigfloats x and y with prec binary digits, in rounding mode mode, and returns it. The optional last parameter is_exact is a boolean variable that is set to true if and only if the returned bigfloat exactly equals the sum of x and y. The parameters prec and mode are also optional and have the global default values global_prec and round_mode respectively, that is, the three calls add(x, y, global_prec, round_mode), add(x, y, global_prec), and add(x, y) are all equivalent. The syntax for functions sub, mul, div, and sqrt is analogous.

The operators +, -, *, and / are implemented by their counterparts among the functions add, sub, mul and div. For example, the call x + y is equivalent to add(x,y).

A bigfloat x can be rounded by the call round(x,prec,mode,is_exact). The optional boolean variable is_exact is set to true if and only if the rounding operation did not change the value of x.

bigfloats offer a small set of mathematical functions (e.g. abs, log2, ceil, floor, sign), functions to test for special values, conversions to doubles and integers, functions to access significant and exponent, and functions to set the global precision, the rounding mode and the output mode.

bool isNaN(bigfloat x) returns true if and only if x is in special state NaN

bool isnInf(bigfloat x) returns true if and only if x is in special state nInf

bool ispInf(bigfloat x) returns true if and only if x is in special state pInf

bool isnZero(bigfloat x) returns true if and only if x is in special state nZero

bool ispZero(bigfloat x) returns true if and only if x is in special state pZero

bool isZero(bigfloat x) returns true if and only if ispZero(x) or isnZero(x)

bool isInf(bigfloat x) returns true if and only if ispInf(x) or isnInf(x)

bool isSpecial(bigfloat x) returns true if and only if x is in a special state

long sign(bigfloat x) returns the sign of x.

bigfloat abs(bigfloat x) returns the absolute value of x

bigfloat pow2(integer p) returns 2p

bigfloat sqrt_d(bigfloat x, long p, int d= 2)
    returns $ \sqrt[d]{x}$, with relative error < = 2-p but not necessarily exactly rounded to p binary digits

integer ilog2(bigfloat x) returns the binary logarithm of abs(x), rounded up to the next integer.
Precondition x $ \not=$ 0

integer ceil(bigfloat x) returns x, rounded up to the next integer

integer floor(bigfloat x) returns x, rounded down to the next integer

integer to_integer(bigfloat x, rounding_modes rmode= TO_NEAREST)
    returns the integer value next to x (in the given rounding mode)

double to_double(bigfloat x) returns the double value next to x (in rounding mode TO_NEAREST

ostream& ostream& os << x writes x to output stream os

istream& istream& is >> bigfloat& x
    reads x from input stream is in decimal format

string to_string() returns a string representation of x.

long x.get_significant_length(void)
    returns the length of the significant of x

integer x.get_exponent(void) returns the exponent of x

integer x.get_significant(void) returns the significant of x

long bigfloat::set_precision(long p)
    sets the global precision to p and returns the old precision

rounding_modes bigfloat::set_rounding_mode(rounding_modes m)
    sets the global rounding mode to m and returns the old rounding mode

output_modes bigfloat::set_output_mode(output_modes o_mode)
    sets the output mode to o_mode and returns the old output mode


next up previous contents index
Next: Algebraic Real Numbers ( Up: Number Types and Linear Previous: Rational Numbers ( rational   Contents   Index
LEDA research project
2000-02-09