Class RationalNumber
java.lang.Object
java.lang.Number
org.znerd.math.RealNumber
org.znerd.math.RationalNumber
- All Implemented Interfaces:
Serializable, Comparable, RoundingModes
- Direct Known Subclasses:
BasicRationalNumber, DigitSet, IntegerNumber
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:
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 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 isthisin anIntegerNumberimplementationnegate()-- The implementation in this class returns a fraction with a negated numerator, which isthisin anIntegerNumberimplementationtoString()-- The implementation in this class returns a string representation of the numerator and the string representation of the denominator; in anIntegerNumberimplementation the numerator is this
- Version:
- $Revision: 1.13 $ $Date: 2002/10/01 18:56:55 $
- Author:
- Ernst de Haan (znerd@FreeBSD.org)
- See Also:
-
Field Summary
Fields inherited from class RealNumber
MAXIMUM_RADIXFields inherited from interface RoundingModes
ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_UP -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedRationalNumber(IntegerNumber[] parts) Constructs a newRationalNumberwith the specified numerator and denominator.protectedRationalNumber(IntegerNumber[] parts, String asString) Constructs a newRationalNumberwith the specified numerator, denominator and textual presentation. -
Method Summary
Modifier and TypeMethodDescriptionComputes this+n, where n is a rational number.add(RealNumber n) Computes this + n, where n is a real number.protected final intCompares this number with the specified rational number, first level.protected intCompares this number with the specified rational number, second level.protected final intCompares this number with the specified number, second level.protected intCompares this number with the specified number, third level.Computes this/n, where n is a rational number.divide(RealNumber n) Computes this/n, where n is a real number.doubleReturns the value of this number as adouble.final IntegerNumberReturns the denominator of this fraction.final IntegerNumberReturns the numerator of this fraction.invert()Computes 1/this.Computes this*n, where n is a rational number.Computes this * n, where n is a real number.negate()Computes -this.final RationalNumberpow(IntegerNumber n) protected RationalNumberfinal RealNumberComputes thisn, where n is a real number, second level.Computes this-n, where n is a rational number.Computes this - n, where n is a real number.toBigDecimal(int precision) Converts the value of this number to aBigDecimalwith the specified precision.toBigDecimal(int precision, int roundingMode) Converts the value of this number to aBigDecimalwith the specified precision and rounding mode.trunc()Rounds to an integer number towards 0.Methods inherited from class RealNumber
abs, byteValue, compareTo, compareTo, equals, fitsByte, fitsDouble, fitsFloat, fitsInt, fitsLong, fitsShort, floatValue, getSign, intValue, longValue, pow, round, shortValue, toBigInteger, toString
-
Constructor Details
-
RationalNumber
Constructs a newRationalNumberwith the specified numerator and denominator.- Parameters:
parts- an array containing the numerator and denonimator, notnull, having at least 2 elements and not having anullat index 0 or 1.- Throws:
IllegalArgumentException- ifparts == nullorparts.length < 2orparts[0] == nullorparts[1] == null.
-
RationalNumber
Constructs a newRationalNumberwith the specified numerator, denominator and textual presentation.- Parameters:
parts- an array containing the numerator and denonimator, notnull, having at least 2 elements and not having anullat index 0 or 1.asString- textual presentation of the number, notnull.- Throws:
IllegalArgumentException- ifparts == nullorparts.length < 2orparts[0] == nullorparts[1] == null.
-
-
Method Details
-
compareToImpl
Compares this number with the specified number, second level. The implementation of this method in classRationalNumberfirst checks ifn instanceof RationalNumber. If so, then it callscompareTo(RationalNumber). Otherwise, it callscompareToImpl2(RealNumber).- Overrides:
compareToImplin classRealNumber- Parameters:
n- the number to compare to, guaranteed to be notnull.- Returns:
- -1 if this < n, 0 if this == n, 1 if this > n.
- Throws:
CanNotCompareException- if the comparison failed.
-
compareToImpl2
Compares this number with the specified number, third level. The implementation of this method in classRationalNumberjust throws aCanNotCompareException.- Parameters:
n- the number to compare to, guaranteed to be notnull.- Returns:
- -1 if this < n, 0 if this == n, 1 if this > n.
- Throws:
CanNotCompareException- if the comparison failed.
-
compareTo
Compares this number with the specified rational number, first level. The implementation of this method in classRationalNumberreturns the result ofsubtract(n).getSign().- Parameters:
n- the number to compare to, guaranteed to be notnull.- Returns:
- -1 if this < n, 0 if this == n, 1 if this > n.
- Throws:
CanNotCompareException- if the comparison failed.
-
compareToImpl
Compares this number with the specified rational number, second level. This method is called fromcompareTo(RationalNumber). The implementation of this method in classRationalNumberjust throws aCanNotCompareExceptionto 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.- Parameters:
n- the number to compare to, guaranteed to be notnull.- Returns:
- -1 if this < n, 0 if this == n, 1 if this > n.
- Throws:
CanNotCompareException- if the comparison failed.
-
negate
Description copied from class:RealNumberComputes -this.- Overrides:
negatein classRealNumber- Returns:
- the negative of this, not
null.
-
invert
Description copied from class:RealNumberComputes 1/this.- Overrides:
invertin classRealNumber- Returns:
- the inverse of this, not
null.
-
add
Description copied from class:RealNumberComputes this + n, where n is a real number.- Overrides:
addin classRealNumber- Parameters:
n- the number to add to this, notnull.- Returns:
- the sum of this and n, not
null.
-
add
Computes this+n, where n is a rational number.- Parameters:
n- the number to add to this.- Returns:
- the sum of this and n.
- Throws:
IllegalArgumentException- if n ==null.
-
subtract
Description copied from class:RealNumberComputes this - n, where n is a real number. The implementation of this method in classRealNumbercallsRealNumber.add(RealNumber)withn.negate()as the argument.- Overrides:
subtractin classRealNumber- Parameters:
n- the number to subtract from this, notnull.- Returns:
- this minus n, not
null. - Throws:
IllegalArgumentException- ifn == null.
-
subtract
Computes this-n, where n is a rational number.- Parameters:
n- the number to subtract from this.- Returns:
- this minus n.
- Throws:
IllegalArgumentException- if n ==null.
-
multiply
Description copied from class:RealNumberComputes this * n, where n is a real number.- Overrides:
multiplyin classRealNumber- Parameters:
n- the factor, the number to multiply this by, notnull.- Returns:
- the product of this and n.
-
multiply
Computes this*n, where n is a rational number.- Parameters:
n- the number to multiply this by.- Returns:
- the product of this and n.
- Throws:
IllegalArgumentException- if n ==null.
-
divide
Description copied from class:RealNumberComputes this/n, where n is a real number. The implementation of this method in classRealNumbercallsRealNumber.multiply(RealNumber)withn.invert()as the argument.- Overrides:
dividein classRealNumber- Parameters:
n- the number to divide this by, notnull.- Returns:
- this divided by n, not
null. - Throws:
IllegalArgumentException- ifn == null.ArithmeticException- if the value of n is zero.
-
divide
Computes this/n, where n is a rational number.- Parameters:
n- the number to divide this by.- Returns:
- this divided by n.
- Throws:
IllegalArgumentException- if n ==null.ArithmeticException- if the value of n is zero.
-
powImpl
Description copied from class:RealNumberComputes thisn, where n is a real number, second level. This method may be implemented by subclasses. The implementation of this method in classRealNumberthrows anUnsupportedOperationException.- Overrides:
powImplin classRealNumber- Parameters:
n- the exponent, guaranteed not to benullif called byRealNumber.pow(RealNumber).- Returns:
- this raised to the power of n, never
null. - Throws:
UnsupportedOperationException- if this operation is not supported by this class.
-
pow
-
powImpl
-
doubleValue
public double doubleValue()Description copied from class:RealNumberReturns the value of this number as adouble. This may involve rounding.- Overrides:
doubleValuein classRealNumber- Returns:
- the numeric value represented by this object after conversion
to type
double.
-
toBigDecimal
Description copied from class:RealNumberConverts the value of this number to aBigDecimalwith the specified precision. This method uses theROUND_HALF_UProunding mode as defined inBigDecimal.- Overrides:
toBigDecimalin classRealNumber- Parameters:
precision- the number of digits behind the decimal point.- Returns:
- a
BigDecimalwith the rounded value of this. - Throws:
IllegalArgumentException- ifprecision < 0.
-
toBigDecimal
Converts the value of this number to aBigDecimalwith the specified precision and rounding mode. The implementation of this method in classRationalNumberfirst converts both the numerator and the denominator toBigDecimalobjects usingIntegerNumber.toBigDecimal(). It then callsBigDecimal.divide(BigDecimal,int,int)and returns the result of that call.- Specified by:
toBigDecimalin classRealNumber- Parameters:
precision- the number of digits behind the decimal point, >= 0.roundingMode- the rounding mode to use, one of the modes defined in classBigDecimal.- Returns:
- a
BigDecimalwith the rounded value of this, nevernull. - Throws:
IllegalArgumentException- ifprecision < 0.
-
trunc
Description copied from class:RealNumberRounds to an integer number towards 0.- Overrides:
truncin classRealNumber- Returns:
- this real number truncated to an integer, never
null.
-
getNumerator
Returns the numerator of this fraction. This numerator may be negative.- Returns:
- the numerator, not
null.
-
getDenominator
Returns the denominator of this fraction. The denominator will always be a positive integer number.- Returns:
- the denominator, not
null.
-