Rational NumbersThe data type rational represents a rational number, i.e., the quotient of two integer numbers. A rational is implemented by two Integers of Arbitrary Length, representing the numerator and denominator. Remark: Rational Numbers are not necessarily normalized, that
is, numerator and denominator may have common factors. Call ExampleThe following program shows how
#include <LEDA/numbers/rational.h>
#include <LEDA/numbers/integer.h>
using namespace leda;
int main() {
integer denominator=1;
int i;
for (i=1;i<=40;i++) {denominator*=i;} //generate denominator that does
//not fit into a double :-)
rational r(1000,denominator); //create a rational r
std::cout << "r=" << r << std::endl;
//operations on and with r
r.normalize();
std::cout << "After r.normalize(): r=" << r << std::endl;
rational s=3.0*r+r.inverse();
std::cout << "\ns=" << s << std::endl;
r.invert();
std::cout << "\nAfter r.invert(): r=" << r << std::endl;
std::cout << "\nsqr(r)=" << sqr(r) << std::endl;
std::cout << "\nceil(r)=" << ceil(r) << std::endl;
return 0;
}
Strengths
Disadvantages
TipsUse rationals if you need exact arithmetic for rational numbers. |
See also:Vectors and Matrices with Integer Entries Vectors and Matrices with Double Entries Functions of numerical analysis Manual Entries: |