next up previous contents index
Next: The data type bigfloat Up: Number Types and Linear Previous: Integers of Arbitrary Length

     
Rational Numbers ( rational )

Definition

An instance q of type rational is a rational number where the numerator and the denominator are both of type integer.

#include < LEDA/rational.h >

Creation

rational q creates an instance q of type rational.

rational q(integer n) creates an instance q of type rational and initializes it with the integer n.

rational q(integer n, integer d) creates an instance q of type rational and initializes it to the rational number n/d.

rational q(double x) creates an instance q of type rational and initializes it with the value of x.

Operations

The arithmetic operations +, -, *, /, + =, - =, * =, / =, -(unary), + +, - -, the comparison operations <, < =, >, > =, = =, ! = and the stream operations are all available.

void  q.negate() negates q.

void  q.invert() inverts q.

rational  q.inverse() returns the inverse of q.

integer q.numerator() returns the numerator of q.

integer q.denominator() returns the denominator of q.

rational&  q.simplify(integer a) simplifies q by a.
Precondition a divides the numerator and the denominator of q.

rational&  q.normalize() normalizes q.

double  to_float() returns a double floating point approximation of q. If the q is approximable by a normalized, finite floating point number, the error is 3ulps, i.e., three units in the last place.

string q.to_string() returns a string representation of q.

Non-member functions

int  sign(rational q) returns the sign of q.

rational  abs(rational q) returns the absolute value of q.

rational  sqr(rational q) returns the square of q.

integer trunc(rational q) returns the integer with the next smaller absolute value.

rational  pow(rational q, int n) returns the n-th power of q.

rational  pow(rational q, integer a)
    returns the a-th power of q.

integer floor(rational q) returns the next smaller integer.

integer ceil(rational q) returns the next bigger integer.

integer round(rational q) rounds q to the nearest integer.

rational  small_rational_between(rational p, rational q)
    returns a rational number between p and q whose denominator is as small as possible.

rational  small_rational_near(rational p, rational eps)
    returns a rational number between p - eps and p + eps whose denominator is as small as possible.

Implementation

A rational is implemented by two integernumbers which represent the numerator and the denominator. The sign is represented by the sign of the numerator.


next up previous contents index
Next: The data type bigfloat Up: Number Types and Linear Previous: Integers of Arbitrary Length

© Copyright 1995-2002, Algorithmic Solutions Software GmbH. All rights reserved.
2002-10-16