plll  1.0
plll::arithmetic::Rational Class Reference

Represents a rational number as a quotient of two arbitrary precision integers. More...

#include <rational.hpp>

Public Member Functions

const Integerdenominator () const PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Retrieves the denominator of this rational number. More...
 
long getApproxExponent () const PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Returns approximate e such that |x| is approximately $2^e$. More...
 
const Integernumerator () const PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Retrieves the numerator of this rational number. More...
 
Rationaloperator= (const Rational &r)
 Assigns the given rational number r to this rational number. More...
 
template<class A , template< typename, typename > class O>
Rationaloperator= (const expressions::Expression< RationalContext, A, O > &E)
 Assigns the rational expression E to this rational number. More...
 
template<class A , template< typename, typename > class O>
Rationaloperator= (const expressions::Expression< IntegerContext, A, O > &E)
 Assigns the integer expression E to this rational number. More...
 
 Rational ()
 Creates a new rational number representing 0.
 
 Rational (const RationalContext &rc)
 Creates a new rational number representing 0. More...
 
 Rational (const Rational &r)
 Creates a copy of the given rational number. More...
 
 Rational (const Rational &r, const RationalContext &rc)
 Creates a copy of the given rational number. More...
 
template<class A , template< typename, typename > class O>
 Rational (const expressions::Expression< RationalContext, A, O > &E)
 Evaluates the given rational expression into a new rational number. More...
 
 Rational (double d)
 Creates a rational number from the given native floating point value. More...
 
 Rational (double d, const RationalContext &rc)
 Creates a rational number from the given native floating point value. More...
 
 Rational (long double d)
 Creates a rational number from the given native floating point value. More...
 
 Rational (long double d, const RationalContext &rc)
 Creates a rational number from the given native floating point value. More...
 
 Rational (long i)
 Creates a rational number from the given native integer. More...
 
 Rational (long i, const RationalContext &rc)
 Creates a rational number from the given native integer. More...
 
 Rational (unsigned long i)
 Creates a rational number from the given native integer. More...
 
 Rational (unsigned long i, const RationalContext &rc)
 Creates a rational number from the given native integer. More...
 
 Rational (long long i)
 Creates a rational number from the given native integer. More...
 
 Rational (long long i, const RationalContext &rc)
 Creates a rational number from the given native integer. More...
 
 Rational (const Integer &n, const Integer &d)
 Creates a rational number from the given arbitrary precision integer pair of numerator and denominator. More...
 
 Rational (const Integer &i)
 Creates a rational number from the given arbitrary precision integer expression. More...
 
 Rational (const Integer &i, const RationalContext &rc)
 Creates a rational number from the given arbitrary precision integer. More...
 

Static Public Member Functions

static unsigned long precision () PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Retrieves the precision of the current rational number. More...
 
static void setContext (const RationalContext &rc) PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Applies the given rational context to this number. More...
 

Friends

void abs (Rational &r, const Rational &a)
 Takes the absolute value of a and stores the result in r. More...
 
void add (Rational &r, const Rational &a, const Rational &b)
 Adds a and b and stores the result in r. More...
 
void addmul (Rational &r, const Rational &a, const Rational &b)
 Multiplies a and b and adds the result to r. More...
 
int compare (const Rational &a, const Rational &b)
 Compares the two rational numbers. More...
 
int compareAbsValues (const Rational &a, const Rational &b)
 Compares the two rational numbers in absolute value. More...
 
void decrement (Rational &r, const Rational &a)
 Negates a and stores the result in r. More...
 
void div (Rational &r, const Rational &a, const Rational &b)
 Divides a by b and stores the result in r. More...
 
void increment (Rational &r, const Rational &a)
 Increments a by one and stores the result in r. More...
 
bool isNegative (const Rational &r) PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Tests the given rational number for being strictly negative. More...
 
bool isNonNegative (const Rational &r) PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Tests the given rational number for being positive or zero. More...
 
bool isNonPositive (const Rational &r) PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Tests the given rational number for being negative or zero. More...
 
bool isOne (const Rational &r) PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Tests the given plll::arithmetic::Real object for being zero. More...
 
bool isPositive (const Rational &r) PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Tests the given rational number for being strictly positive. More...
 
bool isZero (const Rational &r) PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Tests the given rational number for being zero. More...
 
void makeAbs (Rational &a) PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Makes the operand non-negative. More...
 
void mod (Rational &r, const Rational &a, const Rational &b)
 Takes the remainder of the division of a by b and stores it in r. More...
 
void mul (Rational &r, const Rational &a, const Rational &b)
 Multiplies a with b and stores the result in r. More...
 
void neg (Rational &r, const Rational &a)
 Negates a and stores the result in r. More...
 
std::ostream & operator<< (std::ostream &s, const Rational &r)
 Outputs the rational number on the given output stream.
 
std::istream & operator>> (std::istream &s, Rational &r)
 Reads the rational number from the given input stream.
 
void power (Rational &r, const Rational &a, signed long e)
 Raises a to the power e and stores the result in r. More...
 
void power (Rational &r, const Rational &a, unsigned long e)
 Raises a to the power e and stores the result in r. More...
 
void power (Rational &r, const Rational &a, const Integer &e)
 Raises a to the power e and stores the result in r. More...
 
void setOne (Rational &r)
 Sets the given rational number to one. More...
 
void setZero (Rational &r, bool sign)
 Sets the given rational number to $\pm 0$. More...
 
void shl (Rational &r, const Rational &a, long b)
 Multiplies a by $2^b$ and stores the result in r. More...
 
void shr (Rational &r, const Rational &a, long b)
 Multiplies a by $2^b$ and stores the result in r. More...
 
int sign (const Rational &r) PLLL_INTERNAL_NOTHROW_POSTFIX_INLINE
 Returns the sign of the given rational number. More...
 
void square (Rational &r, const Rational &a)
 Computes the square of a and stores the result in r. More...
 
void sub (Rational &r, const Rational &a, const Rational &b)
 Subtracts b from a and stores the result in r. More...
 
void submul (Rational &r, const Rational &a, const Rational &b)
 Multiplies a and b and subtracts the result from r. More...
 

Detailed Description

Represents a rational number as a quotient of two arbitrary precision integers.

Definition at line 431 of file rational.hpp.

Constructor & Destructor Documentation

plll::arithmetic::Rational::Rational ( const RationalContext rc)
inline

Creates a new rational number representing 0.

Parameters
rcA rational context.

Definition at line 542 of file rational.hpp.

plll::arithmetic::Rational::Rational ( const Rational r)
inline

Creates a copy of the given rational number.

Parameters
rThe rational number to copy.

Definition at line 553 of file rational.hpp.

plll::arithmetic::Rational::Rational ( const Rational r,
const RationalContext rc 
)
inline

Creates a copy of the given rational number.

Parameters
rThe rational number to copy.
rcA rational context.

Definition at line 564 of file rational.hpp.

template<class A , template< typename, typename > class O>
plll::arithmetic::Rational::Rational ( const expressions::Expression< RationalContext, A, O > &  E)
inline

Evaluates the given rational expression into a new rational number.

Parameters
EA rational expression.

Definition at line 575 of file rational.hpp.

plll::arithmetic::Rational::Rational ( double  d)
inlineexplicit

Creates a rational number from the given native floating point value.

Parameters
dThe native floating point value.

Definition at line 607 of file rational.hpp.

plll::arithmetic::Rational::Rational ( double  d,
const RationalContext rc 
)
inlineexplicit

Creates a rational number from the given native floating point value.

Parameters
dThe native floating point value.
rcA rational context.

Definition at line 618 of file rational.hpp.

plll::arithmetic::Rational::Rational ( long double  d)
inlineexplicit

Creates a rational number from the given native floating point value.

Parameters
dThe native floating point value.

Definition at line 628 of file rational.hpp.

plll::arithmetic::Rational::Rational ( long double  d,
const RationalContext rc 
)
inlineexplicit

Creates a rational number from the given native floating point value.

Parameters
dThe native floating point value.
rcA rational context.

Definition at line 639 of file rational.hpp.

plll::arithmetic::Rational::Rational ( long  i)
inlineexplicit

Creates a rational number from the given native integer.

Parameters
iThe native integer.

Definition at line 649 of file rational.hpp.

plll::arithmetic::Rational::Rational ( long  i,
const RationalContext rc 
)
inlineexplicit

Creates a rational number from the given native integer.

Parameters
iThe native integer.
rcA rational context.

Definition at line 661 of file rational.hpp.

plll::arithmetic::Rational::Rational ( unsigned long  i)
inlineexplicit

Creates a rational number from the given native integer.

Parameters
iThe native integer.

Definition at line 672 of file rational.hpp.

plll::arithmetic::Rational::Rational ( unsigned long  i,
const RationalContext rc 
)
inlineexplicit

Creates a rational number from the given native integer.

Parameters
iThe native integer.
rcA rational context.

Definition at line 684 of file rational.hpp.

plll::arithmetic::Rational::Rational ( long long  i)
inlineexplicit

Creates a rational number from the given native integer.

Parameters
iThe native integer.

Definition at line 695 of file rational.hpp.

plll::arithmetic::Rational::Rational ( long long  i,
const RationalContext rc 
)
inlineexplicit

Creates a rational number from the given native integer.

Parameters
iThe native integer.
rcA rational context.

Definition at line 707 of file rational.hpp.

plll::arithmetic::Rational::Rational ( const Integer n,
const Integer d 
)
inlineexplicit

Creates a rational number from the given arbitrary precision integer pair of numerator and denominator.

Parameters
nThe numerator.
dThe denominator.

Definition at line 720 of file rational.hpp.

plll::arithmetic::Rational::Rational ( const Integer i)
inlineexplicit

Creates a rational number from the given arbitrary precision integer expression.

Parameters
iThe arbitrary precision integer.

Definition at line 737 of file rational.hpp.

plll::arithmetic::Rational::Rational ( const Integer i,
const RationalContext rc 
)
inlineexplicit

Creates a rational number from the given arbitrary precision integer.

Parameters
iThe arbitrary precision integer.
rcA rational context.

Definition at line 756 of file rational.hpp.

Member Function Documentation

const Integer& plll::arithmetic::Rational::denominator ( ) const
inline

Retrieves the denominator of this rational number.

Returns
An arithmetic::Integer object representing the denominator.

Definition at line 498 of file rational.hpp.

long plll::arithmetic::Rational::getApproxExponent ( ) const
inline

Returns approximate e such that |x| is approximately $2^e$.

The value of e should be OK up to +-2.

Returns
$\approx \log_2 |x|$.

Definition at line 1419 of file rational.hpp.

const Integer& plll::arithmetic::Rational::numerator ( ) const
inline

Retrieves the numerator of this rational number.

Returns
An arithmetic::Integer object representing the numerator.

Definition at line 488 of file rational.hpp.

Rational& plll::arithmetic::Rational::operator= ( const Rational r)
inline

Assigns the given rational number r to this rational number.

Parameters
rThe rational number to assign to this rational number.
Returns
A reference to this rational number.

Definition at line 775 of file rational.hpp.

template<class A , template< typename, typename > class O>
Rational& plll::arithmetic::Rational::operator= ( const expressions::Expression< RationalContext, A, O > &  E)
inline

Assigns the rational expression E to this rational number.

Parameters
EThe rational expression.
Returns
A reference to this rational number.

Definition at line 789 of file rational.hpp.

template<class A , template< typename, typename > class O>
Rational& plll::arithmetic::Rational::operator= ( const expressions::Expression< IntegerContext, A, O > &  E)
inline

Assigns the integer expression E to this rational number.

Parameters
EThe integer expression.
Returns
A reference to this rational number.

Definition at line 802 of file rational.hpp.

static unsigned long plll::arithmetic::Rational::precision ( )
inlinestatic

Retrieves the precision of the current rational number.

Will return a large number equal to the one returned by RationalContext::getRealPrecision().

Definition at line 586 of file rational.hpp.

static void plll::arithmetic::Rational::setContext ( const RationalContext rc)
inlinestatic

Applies the given rational context to this number.

Does nothing.

Parameters
rcA rational context.

Definition at line 598 of file rational.hpp.

Friends And Related Function Documentation

void abs ( Rational r,
const Rational a 
)
friend

Takes the absolute value of a and stores the result in r.

Parameters
rThe result.
aThe operand.

Definition at line 1285 of file rational.hpp.

void add ( Rational r,
const Rational a,
const Rational b 
)
friend

Adds a and b and stores the result in r.

Parameters
rThe result.
aThe first operand.
bThe second operand.

Definition at line 887 of file rational.hpp.

void addmul ( Rational r,
const Rational a,
const Rational b 
)
friend

Multiplies a and b and adds the result to r.

Parameters
rThe accumulator.
aThe first operand.
bThe second operand.

Definition at line 1256 of file rational-ops.hpp.

int compare ( const Rational a,
const Rational b 
)
friend

Compares the two rational numbers.

Parameters
aThe first operand.
bThe second operand.
Returns
Returns a negative number if $a < b$, zero if $a = b$ and a positive number if $a > b$.

Definition at line 1320 of file rational.hpp.

int compareAbsValues ( const Rational a,
const Rational b 
)
friend

Compares the two rational numbers in absolute value.

Parameters
aThe first operand.
bThe second operand.
Returns
Returns a negative number if $|a| < |b|$, zero if $|a| = |b|$ and a positive number if $|a| > |b|$.

Definition at line 1331 of file rational.hpp.

void decrement ( Rational r,
const Rational a 
)
friend

Negates a and stores the result in r.

Parameters
rThe result.
aThe operand.

Definition at line 1265 of file rational.hpp.

void div ( Rational r,
const Rational a,
const Rational b 
)
friend

Divides a by b and stores the result in r.

Parameters
rThe result.
aThe first operand.
bThe second operand.

Definition at line 1081 of file rational.hpp.

void increment ( Rational r,
const Rational a 
)
friend

Increments a by one and stores the result in r.

Parameters
rThe result.
aThe operand.

Definition at line 1258 of file rational.hpp.

bool isNegative ( const Rational r)
friend

Tests the given rational number for being strictly negative.

Returns
Returns true if and only if the argument is strictly negative.
See also
Predicates

Definition at line 829 of file rational.hpp.

bool isNonNegative ( const Rational r)
friend

Tests the given rational number for being positive or zero.

Returns
Returns true if and only if the argument is positive or zero.
See also
Predicates

Definition at line 824 of file rational.hpp.

bool isNonPositive ( const Rational r)
friend

Tests the given rational number for being negative or zero.

Returns
Returns true if and only if the argument is negative or zero.
See also
Predicates

Definition at line 834 of file rational.hpp.

bool isOne ( const Rational r)
friend

Tests the given plll::arithmetic::Real object for being zero.

Returns
Returns true if and only if the argument is zero.
See also
Predicates

Definition at line 814 of file rational.hpp.

bool isPositive ( const Rational r)
friend

Tests the given rational number for being strictly positive.

Returns
Returns true if and only if the argument is strictly positive.
See also
Predicates

Definition at line 819 of file rational.hpp.

bool isZero ( const Rational r)
friend

Tests the given rational number for being zero.

Returns
Returns true if and only if the argument is zero.
See also
Predicates

Definition at line 809 of file rational.hpp.

void makeAbs ( Rational a)
friend

Makes the operand non-negative.

Parameters
aThe operand.

Definition at line 1298 of file rational.hpp.

void mod ( Rational r,
const Rational a,
const Rational b 
)
friend

Takes the remainder of the division of a by b and stores it in r.

Parameters
rThe result.
aThe first operand.
bThe second operand.

Definition at line 1171 of file rational.hpp.

void mul ( Rational r,
const Rational a,
const Rational b 
)
friend

Multiplies a with b and stores the result in r.

Parameters
rThe result.
aThe first operand.
bThe second operand.

Definition at line 1008 of file rational.hpp.

void neg ( Rational r,
const Rational a 
)
friend

Negates a and stores the result in r.

Parameters
rThe result.
aThe operand.

Definition at line 1272 of file rational.hpp.

void power ( Rational r,
const Rational a,
signed long  e 
)
friend

Raises a to the power e and stores the result in r.

Parameters
rThe result.
aThe base.
eThe exponent.

Definition at line 1361 of file rational.hpp.

void power ( Rational r,
const Rational a,
unsigned long  e 
)
friend

Raises a to the power e and stores the result in r.

Parameters
rThe result.
aThe base.
eThe exponent.

Definition at line 1381 of file rational.hpp.

void power ( Rational r,
const Rational a,
const Integer e 
)
friend

Raises a to the power e and stores the result in r.

Parameters
rThe result.
aThe base.
eThe exponent.
void setOne ( Rational r)
friend

Sets the given rational number to one.

Parameters
rThe rational number variable to set to one.

Definition at line 1313 of file rational.hpp.

void setZero ( Rational r,
bool  sign 
)
friend

Sets the given rational number to $\pm 0$.

Note that the sign is ignored, as there is no distinction between the rational +0 and -0.

Parameters
rThe rational number variable to set to zero.
signThis is ignored. The default value is true.

Definition at line 1306 of file rational.hpp.

void shl ( Rational r,
const Rational a,
long  b 
)
friend

Multiplies a by $2^b$ and stores the result in r.

Parameters
rThe result.
aThe first operand.
bThe second operand.

Definition at line 1194 of file rational.hpp.

void shr ( Rational r,
const Rational a,
long  b 
)
friend

Multiplies a by $2^b$ and stores the result in r.

Parameters
rThe result.
aThe first operand.
bThe second operand.

Definition at line 1226 of file rational.hpp.

int sign ( const Rational r)
friend

Returns the sign of the given rational number.

Returns
Returns a negative value if the value is negative, 0 if it is zero, and a positive value if it is positive.
See also
Predicates

Definition at line 1342 of file rational.hpp.

void square ( Rational r,
const Rational a 
)
friend

Computes the square of a and stores the result in r.

Parameters
rThe result.
aThe operand.

Definition at line 1348 of file rational.hpp.

void sub ( Rational r,
const Rational a,
const Rational b 
)
friend

Subtracts b from a and stores the result in r.

Parameters
rThe result.
aThe first operand.
bThe second operand.

Definition at line 918 of file rational.hpp.

void submul ( Rational r,
const Rational a,
const Rational b 
)
friend

Multiplies a and b and subtracts the result from r.

Parameters
rThe accumulator.
aThe first operand.
bThe second operand.

Definition at line 1262 of file rational-ops.hpp.


The documentation for this class was generated from the following file: