bigrational multiprecision rational arithmetic
bigrational is a class for doing multiprecision rational arithmetic. It supports for example arithmetic operations, comparisons, and exponentiation.
Constructors/Destructor
bigrational()
initializes with 0.
bigrational(int)
bigrational(long)
bigrational(unsigned long)
bigrational(double)
bigrational(const bigint &)
bigrational(const bigint & n, const bigint & d)
initializes with the rational number .
bigrational( const bigrational &)
bigrational()
Type Checking
Before assigning a bigrational to a bigint it
is often useful to check whether precision is lost, i.e., if the
denominator is not equal to one.int is_bigint(const bigrational & a)
returns 1
, if the
denominator of a is one, 0
otherwise.
Assignments
Let a be of type bigrational . 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(const bigrational & b)
a = b.
double dbl(const bigrational & a)
returns the value of a as a double approximation.
int a.intify(int & i) const
performs the assignment provided that this assignment can be
done without overflow. In that case the function returns 0
,
otherwise it returns 1
and lets the value of i unchanged.
int a.longify(long & i) const
performs the assignment provided that this assignment can be
done without overflow. In that case the function returns 0
,
otherwise it returns 1
and lets the value of i unchanged.
Access Functions
bigint a.numerator() const
returns the numerator of a.
bigint numerator(const bigrational & a)
returns the numerator of a.
bigint a.denominator() const
returns the denominator of a.
bigint denominator(const bigrational & a)
returns the denominator of a.
Arithmetical Operations
The following operators are overloaded and can be used in exactly the same way
as in the programming language C:
(unary) -, ++, --
(binary) +, -, *, /, >>, <<
(bin. with assignment) +=, -=, *=, /=, >>=, <<=
To avoid copying all operators also exist as functions.void add(bigrational & c, const bigrational & a, const bigrational & b)
c = a + b.
void add(bigrational & c, const bigrational & a, long i)
c = a + i.
void add(bigrational & c, long i, const bigrational & a)
c = i + a.
void subtract(bigrational & c, const bigrational & a, const bigrational & b)
c = a - b.
void subtract(bigrational & c, const bigrational & a, long i)
c = a - i.
void subtract(bigrational & c, long i, const bigrational & a)
c = i - a.
void multiply(bigrational & c, const bigrational & a, const bigrational & b)
.
void multiply(bigrational & c, const bigrational & a, long i)
.
void multiply(bigrational & c, long i, const bigrational & a)
.
void divide(bigrational & c, const bigrational & a, const bigrational & b)
,
if
. Otherwise the error_handler
will be invoked.
void divide(bigrational & c, const bigrational & a, long i)
, if
. Otherwise the error_handler
will be invoked.
void divide(bigrational & c, long i, const bigrational & a)
, if
. Otherwise the error_handler
will be invoked.
void a.negate()
a = -a.
void negate(bigrational & a, const bigrational & b)
a = -b.
void a.multiply_by_2()
(done by shifting).
void multiply_by_2(bigrational & c, const bigrational & a)
(done by shifting).
void a.divide_by_2()
(done by shifting).
void divide_by_2(bigrational & c, const bigrational & a)
(done by shifting).
void a.invert()
, if
. Otherwise the error_handler
will be invoked.
void invert(bigrational & a, const bigrational & b)
, if
. Otherwise the error_handler
will be invoked.
bigrational inverse(const bigrational & a)
returns , if
. Otherwise the error_handler
will be invoked.
void inc(bigrational & a)
a = a + 1.
void dec(bigrational & a)
a = a - 1.
void square(bigrational & c, const bigrational & a)
.
void power(bigrational & c, const bigrational & a, const bigint & i)
.
void power(bigrational & c, const bigrational & a, long i)
.
Shift Operations
void shift_left(bigrational & c, const bigrational & a, unsigned int i)
.
void shift_right(bigrational & c, const bigrational & a, unsigned int i)
.
Comparisons
The binary operators == , != , >= , <= , > , < , and the unary operator ! (comparison with zero) are overloaded and can be used in exactly the same way as in the programming language C. Let a be an instance of type bigrational .int a.is_positive() const
returns 1
if a>0, 0
otherwise.
int a.is_negative() const
returns 1
if a<0, 0
otherwise.
int a.is_zero() const
returns 1
if a = 0, 0
otherwise.
int a.is_ge_zero() const
returns 1
if , 0
otherwise.
int a.is_gt_zero() const
returns 1
if a > 0, 0
otherwise.
int a.is_le_zero() const
returns 1
if , 0
otherwise.
int a.is_lt_zero() const
returns 1
if a < 0, 0
otherwise.
int a.is_one() const
returns 1
if a=1, 0
otherwise.
int a.compare(const bigrational & b) const
returns sign.
int a.abs_compare(const bigrational & b) const
returns sign.
Basic Functions
int a.sign() const
returns -1, 0, 1 if a <, =, > 0 respectively.
bigrational abs(const bigrational & a)
returns |a|.
bigint ceil(const bigrational & a)
returns least integer b with .
bigint floor(const bigrational & a)
returns greatest integer b with .
bigint round(const bigrational & a)
returns nearest integer to a.
bigint truncate(const bigrational & a)
returns the integer part b of a, i.e.,
.
Input/Output
Let a be of type bigrational . 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 bigrational have the following format:
without blanks. Note that you have to manage by yourself that successive bigrational s have to be separated by blanks.
int string_to_bigrational(char *s, char *t, bigrational & a)
converts the strings s and t to bigints and sets
, returns the total number of
converted characters.
int bigrational_to_string(const bigrational & a, char *s, char *t)
converts the denominator of a to string s, the numerator of a to
string t and returns the number of used characters.
a.read_from_file(FILE *fp)
reads a from using fread.
a.write_to_file(FILE *fp)
writes a to using fwrite.
a.scan_from_file(FILE *fp)
scans a from using fscanf.
a.print_to_file(FILE *fp)
prints a to using fprintf.
bigint
see LiDIA/src/simple_classes/bigrational_appl.c
Volker Müller, Thomas Papanikolaou
Copyright 1995 by the
LiDIA -Group, Universität des Saarlandes