public abstract class RationalNumber extends RealNumber
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:
Some
- 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
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:
Concrete subclasses should at least provide an implementations for the following methods:
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
getNumerator() --
Returns the numerator of this fractiongetDenominator() --
Returns the denominator of this fractionIntegerNumber 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 implementationnegate() --
The implementation in this class returns a
fraction with a negated numerator, which is this in an
IntegerNumber implementationtoString() --
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 thisMAXIMUM_RADIXROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_UP| Modifier | Constructor and Description |
|---|---|
protected |
RationalNumber(IntegerNumber[] parts)
Constructs a new
RationalNumber with the specified
numerator and denominator. |
protected |
RationalNumber(IntegerNumber[] parts,
java.lang.String asString)
Constructs a new
RationalNumber with the specified
numerator, denominator and textual presentation. |
| Modifier and Type | Method and Description |
|---|---|
RationalNumber |
add(RationalNumber n)
Computes this+n, where n is a rational number.
|
RealNumber |
add(RealNumber n)
Computes this + n, where n is a real number.
|
protected int |
compareTo(RationalNumber n)
Compares this number with the specified rational number, first level.
|
protected int |
compareToImpl(RationalNumber n)
Compares this number with the specified rational number, second level.
|
protected int |
compareToImpl(RealNumber n)
Compares this number with the specified number, second level.
|
protected int |
compareToImpl2(RealNumber n)
Compares this number with the specified number, third level.
|
RationalNumber |
divide(RationalNumber n)
Computes this/n, where n is a rational number.
|
RealNumber |
divide(RealNumber n)
Computes this/n, where n is a real number.
|
double |
doubleValue()
Returns the value of this number as a
double. |
IntegerNumber |
getDenominator()
Returns the denominator of this fraction.
|
IntegerNumber |
getNumerator()
Returns the numerator of this fraction.
|
RealNumber |
invert()
Computes 1/this.
|
RationalNumber |
multiply(RationalNumber n)
Computes this*n, where n is a rational number.
|
RealNumber |
multiply(RealNumber n)
Computes this * n, where n is a real number.
|
RealNumber |
negate()
Computes -this.
|
RationalNumber |
pow(IntegerNumber n) |
protected RationalNumber |
powImpl(IntegerNumber n) |
RealNumber |
powImpl(RealNumber n)
Computes thisn, where n is a real number,
second level.
|
RationalNumber |
subtract(RationalNumber n)
Computes this-n, where n is a rational number.
|
RealNumber |
subtract(RealNumber n)
Computes this - n, where n is a real number.
|
java.math.BigDecimal |
toBigDecimal(int precision)
Converts the value of this number to a
BigDecimal with the
specified precision. |
java.math.BigDecimal |
toBigDecimal(int precision,
int roundingMode)
Converts the value of this number to a
BigDecimal with the
specified precision and rounding mode. |
IntegerNumber |
trunc()
Rounds to an integer number towards 0.
|
abs, byteValue, compareTo, compareTo, equals, fitsByte, fitsDouble, fitsFloat, fitsInt, fitsLong, fitsShort, floatValue, getSign, intValue, longValue, pow, round, shortValue, toBigInteger, toStringprotected RationalNumber(IntegerNumber[] parts) throws java.lang.IllegalArgumentException
RationalNumber with the specified
numerator and denominator.parts - an array containing the numerator and denonimator, not
null, having at least 2 elements and not having a
null at index 0 or 1.java.lang.IllegalArgumentException - if parts == null or parts.length < 2 or
parts[0] == null or parts[1] == null.protected RationalNumber(IntegerNumber[] parts, java.lang.String asString) throws java.lang.IllegalArgumentException
RationalNumber with the specified
numerator, denominator and textual presentation.parts - an array containing the numerator and denonimator, not
null, having at least 2 elements and not having a
null at index 0 or 1.asString - textual presentation of the number, not null.java.lang.IllegalArgumentException - if parts == null or parts.length < 2 or
parts[0] == null or parts[1] == null.protected final int compareToImpl(RealNumber n) throws CanNotCompareException
RationalNumber
first checks if n instanceof RationalNumber. If so, then it
calls compareTo(RationalNumber). Otherwise, it calls
compareToImpl2(RealNumber).compareToImpl in class RealNumbern - the number to compare to, guaranteed to be not null.CanNotCompareException - if the comparison failed.protected int compareToImpl2(RealNumber n) throws CanNotCompareException
RationalNumber
just throws a CanNotCompareException.n - the number to compare to, guaranteed to be not null.CanNotCompareException - if the comparison failed.protected final int compareTo(RationalNumber n) throws CanNotCompareException
RationalNumber
returns the result of
subtract(n).getSign().n - the number to compare to, guaranteed to be not null.CanNotCompareException - if the comparison failed.protected int compareToImpl(RationalNumber n) throws CanNotCompareException
compareTo(RationalNumber). The
implementation of this method in class RationalNumber just
throws a CanNotCompareException to indicate it does not provide
an optimized algorithm for comparing this integer number with the
argument integer number. Subclasses are encouraged to override this
method.n - the number to compare to, guaranteed to be not null.CanNotCompareException - if the comparison failed.public RealNumber negate()
RealNumbernegate in class RealNumbernull.public RealNumber invert()
RealNumberinvert in class RealNumbernull.public RealNumber add(RealNumber n)
RealNumberadd in class RealNumbern - the number to add to this, not null.null.public RationalNumber add(RationalNumber n) throws java.lang.IllegalArgumentException
n - the number to add to this.java.lang.IllegalArgumentException - if n == null.public RealNumber subtract(RealNumber n) throws java.lang.IllegalArgumentException
RealNumberRealNumber calls
RealNumber.add(RealNumber) with n.negate() as the
argument.subtract in class RealNumbern - the number to subtract from this, not null.null.java.lang.IllegalArgumentException - if n == null.public RationalNumber subtract(RationalNumber n) throws java.lang.IllegalArgumentException
n - the number to subtract from this.java.lang.IllegalArgumentException - if n == null.public RealNumber multiply(RealNumber n)
RealNumbermultiply in class RealNumbern - the factor, the number to multiply this by, not null.public RationalNumber multiply(RationalNumber n) throws java.lang.IllegalArgumentException
n - the number to multiply this by.java.lang.IllegalArgumentException - if n == null.public RealNumber divide(RealNumber n) throws java.lang.IllegalArgumentException, java.lang.ArithmeticException
RealNumberRealNumber calls
RealNumber.multiply(RealNumber) with n.invert() as the
argument.divide in class RealNumbern - the number to divide this by, not null.null.java.lang.IllegalArgumentException - if n == null.java.lang.ArithmeticException - if the value of n is zero.public RationalNumber divide(RationalNumber n) throws java.lang.IllegalArgumentException, java.lang.ArithmeticException
n - the number to divide this by.java.lang.IllegalArgumentException - if n == null.java.lang.ArithmeticException - if the value of n is zero.public final RealNumber powImpl(RealNumber n) throws java.lang.UnsupportedOperationException
RealNumberRealNumber throws an
UnsupportedOperationException.powImpl in class RealNumbern - the exponent, guaranteed not to be null if called by
RealNumber.pow(RealNumber).null.java.lang.UnsupportedOperationException - if this operation is not supported by this class.public final RationalNumber pow(IntegerNumber n)
protected RationalNumber powImpl(IntegerNumber n)
public double doubleValue()
RealNumberdouble. This may
involve rounding.doubleValue in class RealNumberdouble.public java.math.BigDecimal toBigDecimal(int precision)
throws java.lang.IllegalArgumentException
RealNumberBigDecimal with the
specified precision. This method uses the ROUND_HALF_UP
rounding mode as defined in BigDecimal.toBigDecimal in class RealNumberprecision - the number of digits behind the decimal point.BigDecimal with the rounded value of this.java.lang.IllegalArgumentException - if precision < 0.public java.math.BigDecimal toBigDecimal(int precision,
int roundingMode)
throws java.lang.IllegalArgumentException
BigDecimal with the
specified precision and rounding mode.
The implementation of this method in class RationalNumber
first converts both the numerator and the denominator to
BigDecimal objects using IntegerNumber.toBigDecimal().
It then calls BigDecimal.divide(BigDecimal,int,int) and returns
the result of that call.toBigDecimal in class RealNumberprecision - the number of digits behind the decimal point, >= 0.roundingMode - the rounding mode to use, one of the modes defined in class
BigDecimal.BigDecimal with the rounded value of this, never
null.java.lang.IllegalArgumentException - if precision < 0.public IntegerNumber trunc()
RealNumbertrunc in class RealNumbernull.public final IntegerNumber getNumerator()
null.public final IntegerNumber getDenominator()
null.