|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.Number | +--org.znerd.math.RealNumber | +--org.znerd.math.RationalNumber
Rational number. It is a specialisation of RealNumber
that offers
narrowed numeric computations for rational 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
pow(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 this
Fields inherited from class org.znerd.math.RealNumber |
MAXIMUM_RADIX |
Constructor Summary | |
protected |
RationalNumber(IntegerNumber[] parts)
Constructs a new RationalNumber with the specified
numerator and denominator. |
protected |
RationalNumber(IntegerNumber[] parts,
String asString)
Constructs a new RationalNumber with the specified
numerator, denominator and textual presentation. |
Method Summary | |
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)
Computes this**n, where n is an integer number. |
RealNumber |
pow(RealNumber n)
Computes thisn, where n is a real number. |
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. |
BigDecimal |
toBigDecimal(int precision)
Converts the value of this number to a BigDecimal with the
specified precision. |
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. |
Methods inherited from class org.znerd.math.RealNumber |
abs,
byteValue,
compareTo,
compareTo,
equals,
fitsByte,
fitsDouble,
fitsFloat,
fitsInt,
fitsLong,
fitsShort,
floatValue,
getSign,
intValue,
longValue,
round,
shortValue,
toBigInteger,
toString |
Methods inherited from class java.lang.Object |
clone,
finalize,
getClass,
hashCode,
notify,
notifyAll,
wait,
wait,
wait |
Constructor Detail |
protected RationalNumber(IntegerNumber[] parts) throws 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.parts == null
or parts.length < 2
or
parts[0] == null
or parts[1] == null
.protected RationalNumber(IntegerNumber[] parts, String asString) throws 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
.parts == null
or parts.length < 2
or
parts[0] == null
or parts[1] == null
.Method Detail |
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)
.n
- the number to compare to, guaranteed to be not null
.protected int compareToImpl2(RealNumber n) throws CanNotCompareException
RationalNumber
just throws a CanNotCompareException
.n
- the number to compare to, guaranteed to be not null
.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
.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
.public RealNumber negate()
null
.public RealNumber invert()
null
.public RealNumber add(RealNumber n)
n
- the number to add to this, not null
.null
.n == null
.public RationalNumber add(RationalNumber n) throws IllegalArgumentException
n
- the number to add to this.null
.public RealNumber subtract(RealNumber n) throws IllegalArgumentException
RealNumber
calls
RealNumber.add(RealNumber)
with n.negate()
as the
argument.n
- the number to subtract from this, not null
.null
.n == null
.public RationalNumber subtract(RationalNumber n) throws IllegalArgumentException
n
- the number to subtract from this.null
.public RealNumber multiply(RealNumber n)
n
- the factor, the number to multiply this by, not null
.n == null
.public RationalNumber multiply(RationalNumber n) throws IllegalArgumentException
n
- the number to multiply this by.null
.public RealNumber divide(RealNumber n) throws IllegalArgumentException, ArithmeticException
RealNumber
calls
RealNumber.multiply(RealNumber)
with n.invert()
as the
argument.n
- the number to divide this by, not null
.null
.n == null
.public RationalNumber divide(RationalNumber n) throws IllegalArgumentException, ArithmeticException
n
- the number to divide this by.null
.public RationalNumber pow(IntegerNumber n) throws IllegalArgumentException
n
- the exponent.null
.public RealNumber pow(RealNumber n) throws IllegalArgumentException
RealNumber
calls
NumberCentral.pow(RealNumber,RealNumber)
.n
- the exponent, not null
.n == null
.public double doubleValue()
double
. This may
involve rounding.double
.public BigDecimal toBigDecimal(int precision) throws IllegalArgumentException
BigDecimal
with the
specified precision. This method uses the ROUND_HALF_UP
rounding mode as defined in BigDecimal
.precision
- the number of digits behind the decimal point.BigDecimal
with the rounded value of this.precision < 0
.public BigDecimal toBigDecimal(int precision, int roundingMode) throws 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.precision
- 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
.precision < 0
.public IntegerNumber trunc()
null
.public final IntegerNumber getNumerator()
null
.public final IntegerNumber getDenominator()
null
.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |