next up previous contents
Next: bigcomplex Up: Description of the Previous: bigrational

bigfloat


bigfloat multiprecision floating point arithmetic


bigfloat is a class for doing multiprecision arithmetic over . It supports for example arithmetic operations, shift operations, comparisons, the square root function, the exponential function, the logarithmical function, (inverse) trigonometric functions etc.


Before declaring a bigfloat it is possible to set the decimal precision p by

bigfloat::precision(p) .

Then the base digit precision (see bigint ) is

,

where base is the value of the bigint base .

If the precision has not been set, a default precision of t = 5 bigint base digits is used. If the decimal precision is set to p then a bigfloat is a pair where m is a bigint with and e is a machine integer. It represents the number

.

The number m is called the mantissa and the number e is called the exponent .

A complete description of the floating point representation and the algorithms for the basic operations ( +, -, *, / ) of bigfloat

can be found in the book of D. Knuth (see [25]). The more sophisticated algorithms which are used in bigfloat are implemented according to R.P. Brent (see [6], [8], [7]) and J.M. and P.B. Borwein (see [4]). There one can find the analysis of the computational errors made by these algorithms too.

Constructors/Destructor

bigfloat()

bigfloat(int)

bigfloat(long)

bigfloat(unsigned long)

bigfloat(double)

bigfloat(const bigint &)

bigfloat(const bigint &, const bigint &) this constructor initializes a bigfloat with the result of the floating point division of two bigint s. Rounding is performed accordingly to the current rounding mode. For this see the function mode
. bigfloat(const bigfloat &)

bigfloat()

Initialization

static void precision(long p)

sets the global decimal precision to p decimal places. Then the base digit precision is , where base is the bigint base. Thus we have for all bigfloat s , i.e., m is a bigint of length at most t. Whenever necessary, internal computations are done with a higher precision.

static long get_precision() return the current decimal precision.

static void mode(long m)

sets the rounding mode for the normalization routine according to the IEEE standard (see [22]). The following modes are available:

MP_TRUNC : round to zero.

MP_RND : round to nearest. If there are two possibilities to do this, round to the even number of them.

MP_RND_UP round to .

MP_RND_DOWN round to .

static int get_mode() return the current rounding mode.

Type Checking

Before assigning a bigfloat to a machine type (e.g. int) it is often useful to perform a test which checks whether the assignment can be done without overflow. The following functions return 1 if the assignment would be successful, 0 otherwise. No rounding is made.

int is_char(const bigfloat &)

int is_uchar(const bigfloat &)

int is_short(const bigfloat &)

int is_ushort(const bigfloat &)

int is_int(const bigfloat &)

int is_uint(const bigfloat &)

int is_long(const bigfloat &)

int is_ulong(const bigfloat &)

int is_double(const bigfloat &)

Assignments

Let a be of type bigfloat .

The operator = is overloaded. For efficiency reasons, the following functions are also implemented:

void a.assign_zero()

a = 0.

void a.assign_one()

a = 1.

void a.assign(int i)

a = i.

void a.assign(unsigned int i)

a = i.

void a.assign(double d)

a = d.

void a.assign(const bigint & I)

a = I.

void a.assign(const bigint & I, const bigint & J)

(floating point division), if . Otherwise the error_handler will be invoked.

void a.bigintify(bigint & I) const

, i.e., I is the nearest integer to a.

int a.doublify(double & d) const

tries to perform the assignment d = a. If successful, i.e., no overflow occurs, the function returns 0 and assign a to d. Otherwise it returns 1 and leaves the value of d unchanged.

int a.intify(int & i) const

tries to perform the assignment i = a. If successful, i.e., no overflow occurs, the function returns 0 and assign a to i. Otherwise it returns 1 and leaves the value of i unchanged.

int a.longify(long & i) const

tries to perform the assignment i = a. If successful, i.e., no overflow occurs, the function returns 0 and assign a to i. Otherwise it returns 1 and leaves the value of i unchanged.

Access Functions

Let a be of type bigfloat .

long a.exponent()

returns the exponent of a.

bigint a.mantissa()

returns the mantissa of a.

Arithmetical Operations

The following operators are overloaded and can be used in exactly the same way as in C:

(unary) -, ++, --
(binary) +, -, *, /, <<, >>
(binary with assignment) +=, -=, *=, /=, <<=, >>=

To avoid copying, all operators exist also as functions:

void add(bigfloat & c, const bigfloat & a, const bigfloat & b)

c = a + b.

void add(bigfloat & c, const bigfloat & a, long i)

c = a + i.

void add(bigfloat & c, long i, const bigfloat & b)

c = i + b.

void subtract(bigfloat & c, const bigfloat & a, const bigfloat & b)

c = a - b.

void subtract(bigfloat & c, const bigfloat & a, long i)

c = a - i.

void subtract(bigfloat & c, long i, const bigfloat & b)

c = i - b.

void multiply(bigfloat & c, const bigfloat & a, const bigfloat & b)

.

void multiply(bigfloat & c, const bigfloat & a, long i)

.

void multiply(bigfloat & c, long i, const bigfloat & b)

.

void divide(bigfloat & c, const bigfloat & a, const bigfloat & b)

, if . Otherwise the error_handler will be invoked.

void divide(bigfloat & c, const bigfloat & a, long i)

, if . Otherwise the error_handler will be invoked.

void divide(bigfloat & c, long i, const bigfloat & b)

, if . Otherwise the error_handler will be invoked.

void divide(bigfloat & c, long i, long j)

(floating point division), if . Otherwise the error_handler will be invoked.

void negate(bigfloat & a, const bigfloat & b)

a = -b.

void a.negate()

a = -a.

void multiply_pow10(bigfloat & c, const bigfloat & a, long i)

.

void a.invert()

, if . Otherwise the error_handler

will be invoked.

void invert(bigfloat & a, const bigfloat & b)

, if . Otherwise the error_handler

will be invoked.

bigfloat inverse(const bigfloat & a)

returns , if . Otherwise the error_handler will be invoked.

void inc(bigfloat & a)

a = a + 1.

void dec(bigfloat & a)

a = a - 1.

void square(bigfloat & c, const bigfloat & a)

.

void sqrt(bigfloat & c, const bigfloat & a)

c = , where a >= 0 . If a<0 the error_handler will be invoked.

bigfloat sqrt(const bigfloat & a)

void power(bigfloat & c, const bigfloat & a, const bigfloat & b)

c = , where b > 0 . If the error_handler will be invoked.

void power(bigfloat & c, const bigfloat & a, long i)

c = .

bigfloat power(const bigfloat & a, const bigfloat & b)

returns .

Shift Operations

Let a be of type bigfloat .

void a.multiply_by_2()

.

void a.divide_by_2()

.

void shift_left(bigfloat & c, const bigfloat & b, int i)

.

void shift_right(bigfloat & c, const bigfloat & b, int i)

.

Comparisons

Let a be an instance of type bigfloat .

The binary operators == , != , >= , <= , > , < and the unary operator ! (comparison with zero) are overloaded and can be used in exactly the same way as in C.

int a.is_zero()

returns 1 if a = 0, 0 otherwise.

int a.is_approx_zero()

returns 1 if (see Initialization), 0 otherwise.

int a.is_one()

returns 1 if a = 1, 0 otherwise.

int a.is_gt_zero()

returns 1 if a > 0, 0 otherwise.

int a.is_ge_zero()

returns 1 if , 0 otherwise.

int a.is_lt_zero()

returns 1 if a < 0, 0 otherwise.

int a.is_le_zero()

returns 1 if , 0 otherwise.

int a.is_positive()

returns 1 if a > 0, 0 otherwise.

int a.is_negative()

returns 1 if a < 0, 0 otherwise.

int a.abs_compare(const bigfloat & b) const

returns .

int a.compare(const bigfloat & b) const

returns .

Basic Functions

Let a be of type bigfloat .

int a.bit_length()

returns the length of the mantissa of a in binary digits.

int a.length()

returns the length of the mantissa of a in bigint base digits.

int a.sign()

returns 1 if a > 0, 0 if a = 0 and -1 otherwise.

void a.absolute_value()

a = |a| .

void besselj(bigfloat & c, int n, const bigfloat & a)

c = (Bessel Function of first kind and integer order).

void ceil(bigfloat & c, const bigfloat & a)

c = , i.e., c is the smallest integer which is greater than or equal to a.

void floor(bigfloat & c, const bigfloat & a)

c = , i.e., c is the largest integer which is less than or equal to a.

void round(bigfloat & c, const bigfloat & a)

, i.e., c is the nearest integer to a.

void truncate(bigfloat & c, const bigfloat & a)

.

void exp(bigfloat & c, const bigfloat & a)

c = .

void log(bigfloat & c, const bigfloat & a)

c = (natural logarithm to base e = 2.718...), where a > 0 . If the error_handler will be invoked.

long exponent(const bigfloat & a) returns the exponent of a.

bigint mantissa(const bigfloat & a) returns the mantissa of a.

bigfloat abs(const bigfloat & a)

returns |a| .

bigfloat besselj(int n, const bigfloat & a)

returns (Bessel Function of first kind and integer order).

bigfloat ceil(const bigfloat & a)

returns , i.e., c is the smallest integer which is greater than or equal to a.

bigfloat floor(const bigfloat & a)

returns , i.e., c is the largest integer which is less than or equal to a.

bigfloat exp(const bigfloat & a)

returns .

bigfloat log(const bigfloat & a)

returns (natural logarithm to base e = 2.718...), where a > 0 . If the error_handler will be invoked.

bigfloat round(const bigfloat & a)

returns , i.e., c is the nearest integer to a.

bigfloat truncate(const bigfloat & a)

returns .

(Inverse) Trigonometric Functions

Note that the arguments of the trigonometrical functions are supposed to be in radiants.

void sin(bigfloat & c, const bigfloat & a)

.

void cos(bigfloat & c, const bigfloat & a)

.

void tan(bigfloat & c, const bigfloat & a)

, if , . Otherwise the error_handler will be invoked.

void cot(bigfloat & c, const bigfloat & a)

, if , . Otherwise the error_handler will be invoked.

void asin(bigfloat & c, const bigfloat & a)

, if . Otherwise the error_handler will be invoked.

void acos(bigfloat & c, const bigfloat & a)

, if . Otherwise the error_handler will be invoked.

void atan(bigfloat & c, const bigfloat & a)

.

void atan2(bigfloat & c, const bigfloat & a, const bigfloat & b)

in the range of , i.e., atan2(a,b) calculates the argument (resp. phase) of a, b.

void acot(bigfloat & c, const bigfloat & a)

.

bigfloat sin(const bigfloat & a)

returns .

bigfloat cos(const bigfloat & a)

returns .

bigfloat tan(const bigfloat & a)

returns , if , . Otherwise the error_handler will be invoked.

bigfloat cot(const bigfloat & a)

returns , if , . Otherwise the error_handler will be invoked.

bigfloat asin(const bigfloat & a)

returns , if . Otherwise the error_handler will be invoked.

bigfloat acos(const bigfloat & a)

returns , if . Otherwise the error_handler will be invoked.

bigfloat atan(const bigfloat & a)

returns .

bigfloat atan2(const bigfloat & a, const bigfloat & b)

returns in the range of , i.e., atan2(a,b) calculates the argument (resp. phase) of a, b.

bigfloat acot(const bigfloat & a)

returns .

(Inverse) Hyperbolic Trigonometric Functions

void sinh(bigfloat & c, const bigfloat & a)

.

void cosh(bigfloat & c, const bigfloat & a)

.

void tanh(bigfloat & c, const bigfloat & a)

.

void coth(bigfloat & c, const bigfloat & a)

.

void asinh(bigfloat & c, const bigfloat & a)

.

void acosh(bigfloat & c, const bigfloat & a)

, where .

void atanh(bigfloat & c, const bigfloat & a)

, where .

void acoth(bigfloat & c, const bigfloat & a)

, where .

For efficiency reasons the following functions are implemented:

bigfloat sinh(const bigfloat & a)

returns .

bigfloat cosh(const bigfloat & a)

returns .

bigfloat tanh(const bigfloat & a)

returns .

bigfloat coth(const bigfloat & a)

returns .

bigfloat asinh(const bigfloat & a)

returns .

bigfloat acosh(const bigfloat & a)

returns .

bigfloat atanh(const bigfloat & a)

returns , where .

bigfloat acoth(const bigfloat & a)

returns , where .

Constants

Constants are calculated according to the precision set in the initializtion phase (i.e., bigfloat::precision).

void constant_e(bigfloat & a)


void constant_pi(bigfloat & a)

a =

void constant_euler(bigfloat & a)

a = = 0.5772156649....õ

void constant_catalan(bigfloat & a)

a = G

bigfloat e()

bigfloat pi()

bigfloat euler()

bigfloat catalan()

Input/Output

Let a be of type bigfloat .

istream operator >> and ostream operator << are overloaded. Furthermore, you can use the following member functions for writing to and reading from a file in binary or ASCII format, respectively. Input and output of a bigfloat

are in the following format:

where z has the form x, or where x and y are of type int and l is of type long. Note that you have to manage by yourself that successive bigfloat s have to be separated by blanks.

int a.read_from_file(FILE * fp)

reads a from the ASCII file fp using fread.

int a.write_to_file(FILE * fp)

writes a to the ASCII file fp using fwrite.

int a.scan_from_file(FILE * fp)

scans a form the ASCII file fp .

int a.print_to_file(FILE * fp) const

prints a in ASCII format to the file fp .

int string_to_bigfloat(const char * s, bigfloat & a)

converts the string s to a bigfloat a and returns the number of used characters.

int bigfloat_to_string(const bigfloat & a, char * s)

converts the bigfloat a to a string s and returns the number of used characters. The number of characters allocated for s must be at least a.bit_length ()/3 + 10.


bigint , bigrational , bigcomplex .


The implementation of the transcendental functions is optimized for the sun4 architecture.


The member functions scan_from_file and print_to_file

are included because there are still compilers which do not handle fstreams correctly. In a future release, those functions will disappear.
Storing a double variable d into a bigfloat x (i.e., using the assignment x = d) is done with the precision of the double mantissa. The precision of IEEE double s is 53 bits).


The following program calculates the number to 100 decimal places.

#include <LiDIA/bigfloat.h>

main()
{
  bigfloat::precision(100);
  bigfloat x = sqrt(pi());
  cout << "\n sqrt(pi) = " << x << flush;
}

An extensive example of bigfloat can be found in LiDIA 's installation directory under

src/simple_classes/bigfloat_appl.c .


Thomas Papanikolaou

Copyright 1995 by the LiDIA -Group, Universität des Saarlandes



next up previous contents
Next: bigcomplex Up: Description of the Previous: bigrational



LiDIA Administrator
Thu Aug 10 16:41:08 MET DST 1995