Class RationalNumber

All Implemented Interfaces:
Serializable, Comparable, RoundingModes
Direct Known Subclasses:
BasicRationalNumber, DigitSet, IntegerNumber

public abstract class RationalNumber extends RealNumber
Rational number. It is a specialisation of RealNumber that offers narrowed numeric computations for rational numbers.

All rational values can be written as a fraction x/y, where both x and y are integer numbers.

RationalNumber derivates must obey these rules:

they should be normalized
the fraction properties should always return a fraction that cannot be simplified any further, thus 3/9 is illegal, while 1/3 is legal
the denominator should always be positive
if the fraction is negative, the numerator should be negative and the denominator positive, otherwise both should be positive, thus both -1/-3 and 1/-3 are illegal, while 1/3 and -1/3 are legal

Some RationalNumber numeric computations are narrowed compared to their more general RealNumber equivalents. For instance, adding two rational numbers will result in a rational number. The computations concerned are:

add(RationalNumber)
adding two rational numbers will result in another rational number
subtract(RationalNumber)
subtracting one rational number from another will result in a rational number
multiply(RationalNumber)
multiplying two rational numbers will result in another rational number
divide(RationalNumber)
dividing one rational number by another will result in a rational number
powImpl(IntegerNumber)
raising a rational number to an integer power will result in a rational number

Concrete subclasses should at least provide an implementations for the following methods:

  • getNumerator() -- Returns the numerator of this fraction
  • getDenominator() -- Returns the denominator of this fraction

IntegerNumber implementations based on this class must override the following methods:

  • getSign() -- The implementation in this class returns the sign of the numerator, which is this in an IntegerNumber implementation
  • negate() -- The implementation in this class returns a fraction with a negated numerator, which is this in an IntegerNumber implementation
  • toString() -- The implementation in this class returns a string representation of the numerator and the string representation of the denominator; in an IntegerNumber implementation the numerator is this
Version:
$Revision: 1.13 $ $Date: 2002/10/01 18:56:55 $
Author:
Ernst de Haan (znerd@FreeBSD.org)
See Also: