public abstract class IntegerNumber extends RationalNumber
RationalNumber
by offering narrowed numeric computations for
integer numbers.
Some IntegerNumber
numeric computations are narrowed
compared to their more general RationalNumber
equivalents.
For instance, adding two integer numbers will result in a integer number.
The computations concerned are:
add(IntegerNumber)
subtract(RationalNumber)
multiply(RationalNumber)
getSign()
negate()
add(IntegerNumber)
multiply(IntegerNumber)
integerDivide(IntegerNumber)
longValue()
long
doubleValue()
double
toByteArray()
final
need no further optimization.MAXIMUM_RADIX
ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_UP
Modifier | Constructor and Description |
---|---|
protected |
IntegerNumber(int sign,
java.lang.String asString)
Creates a new
IntegerNumber with the specified sign and the
specified textual representation. |
Modifier and Type | Method and Description |
---|---|
RealNumber |
abs()
Computes |this|.
|
IntegerNumber |
absInteger()
Computes |this| and returns an
IntegerNumber . |
abstract IntegerNumber |
add(IntegerNumber n)
Computes this + n, where n is an integer number.
|
RationalNumber |
add(RationalNumber n)
Computes this+n, where n is a rational number.
|
protected int |
compareTo(IntegerNumber n)
Compares this number with the specified integer number, first level.
|
protected int |
compareToImpl(IntegerNumber n)
Compares this number with the specified integer number, second level.
|
protected int |
compareToImpl(RationalNumber n)
Compares this number with the specified rational number, second level.
|
IntegerNumber |
dec()
Computes this-1.
|
RationalNumber |
divide(RationalNumber n)
Computes this/n, where n is a rational number.
|
IntegerNumber |
fac()
Computes this!.
|
IntegerNumber |
gcd(IntegerNumber n)
Computes the greatest common divisor of this and n.
|
IntegerNumber |
inc()
Computes this+1.
|
abstract IntegerNumber |
integerDivide(IntegerNumber n)
Computes the integer result of this/n, where n is an
integer number.
|
RealNumber |
invert()
Computes 1/this.
|
boolean |
isRelativePrime(IntegerNumber n)
Determines if this and n are relative primes.
|
abstract IntegerNumber |
multiply(IntegerNumber n)
Computes this * n, where n is an integer number.
|
RationalNumber |
multiply(RationalNumber n)
Computes this*n, where n is a rational number.
|
RealNumber |
negate()
Computes -this.
|
IntegerNumber |
negateInteger()
Computes -this and returns an
IntegerNumber . |
RationalNumber |
powImpl(IntegerNumber n) |
IntegerNumber |
remainder(IntegerNumber n)
Computes the remainder of this/n, where n is an
integer number.
|
IntegerNumber |
subtract(IntegerNumber n)
Computes this - n, where n is an integer number.
|
RationalNumber |
subtract(RationalNumber n)
Computes this-n, where n is a rational number.
|
java.math.BigDecimal |
toBigDecimal()
Converts the value of this number to a
BigDecimal . |
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. |
java.math.BigInteger |
toBigInteger()
Converts the value of this number to a
BigInteger . |
abstract byte[] |
toByteArray()
Returns the two's-complement representation of this integer number.
|
IntegerNumber |
trunc()
Rounds to an integer number towards 0.
|
add, compareTo, compareToImpl, compareToImpl2, divide, doubleValue, getDenominator, getNumerator, multiply, pow, powImpl, subtract
byteValue, compareTo, compareTo, equals, fitsByte, fitsDouble, fitsFloat, fitsInt, fitsLong, fitsShort, floatValue, getSign, intValue, longValue, pow, round, shortValue, toString
protected IntegerNumber(int sign, java.lang.String asString)
IntegerNumber
with the specified sign and the
specified textual representation. This constructor can only be called
from subclass constructors, since this class is abstract.sign
- the sign of this number, either -1 if this number is negative, 0 if
this number is 0, or 1 is this number is positive.asString
- textual presentation of this number, not null
.protected int compareToImpl(RationalNumber n) throws CanNotCompareException
RationalNumber
RationalNumber.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.compareToImpl
in class RationalNumber
n
- the number to compare to, guaranteed to be not null
.CanNotCompareException
- if the comparison failed.protected final int compareTo(IntegerNumber n) throws CanNotCompareException
compareToImpl(IntegerNumber)
. If that
method throws a CanNotCompareException
, then it will attempt to
use a fallback comparison algorithm based on toByteArray()
.n
- the number to compare to, guaranteed to be not null
.CanNotCompareException
- if the comparison failed.protected int compareToImpl(IntegerNumber n) throws CanNotCompareException
compareTo(IntegerNumber)
. The
implementation of this method in class IntegerNumber
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 final RealNumber abs()
RealNumber
abs
in class RealNumber
null
and always with a value
>= 0.public final IntegerNumber absInteger()
IntegerNumber
. The returned
value is always >= 0.
The only difference between this method and abs()
is that
this method is explicitly defined to return an
IntegerNumber
. In fact abs()
calls this method.null
and always with a value
>= 0.public final RealNumber negate()
RealNumber
negate
in class RationalNumber
null
.public IntegerNumber negateInteger()
IntegerNumber
.
The only difference between this method and negate()
is
that this method is explicitly defined to return an
IntegerNumber
. In fact negate()
calls this method.null
.public RealNumber invert() throws java.lang.ArithmeticException
RealNumber
invert
in class RationalNumber
null
.java.lang.ArithmeticException
- if the value of this is zero.public RationalNumber add(RationalNumber n) throws java.lang.IllegalArgumentException
RationalNumber
add
in class RationalNumber
n
- the number to add to this.java.lang.IllegalArgumentException
- if n == null
.public abstract IntegerNumber add(IntegerNumber n) throws java.lang.IllegalArgumentException
n
- the number to add to this, not null
.null
.java.lang.IllegalArgumentException
- if the argument is
null
.public RationalNumber subtract(RationalNumber n) throws java.lang.IllegalArgumentException
RationalNumber
subtract
in class RationalNumber
n
- the number to subtract from this.java.lang.IllegalArgumentException
- if n == null
.public IntegerNumber subtract(IntegerNumber n) throws java.lang.IllegalArgumentException
n
- the number to subtract from this, not null
.null
.java.lang.IllegalArgumentException
- if n == null
.public RationalNumber multiply(RationalNumber n) throws java.lang.IllegalArgumentException
RationalNumber
multiply
in class RationalNumber
n
- the number to multiply this by.java.lang.IllegalArgumentException
- if n == null
.public abstract IntegerNumber multiply(IntegerNumber n) throws java.lang.IllegalArgumentException
n
- the number to multiply this by, not null
.null
.java.lang.IllegalArgumentException
- if n == null
.public RationalNumber divide(RationalNumber n) throws java.lang.IllegalArgumentException, java.lang.ArithmeticException
RationalNumber
divide
in class RationalNumber
n
- the number to divide this by.java.lang.IllegalArgumentException
- if n == null
.java.lang.ArithmeticException
- if the value of n is zero.public RationalNumber powImpl(IntegerNumber n) throws java.lang.IllegalArgumentException
powImpl
in class RationalNumber
java.lang.IllegalArgumentException
public IntegerNumber dec()
public IntegerNumber inc()
public IntegerNumber fac() throws java.lang.ArithmeticException
null
.java.lang.ArithmeticException
- if the value of this number is < 0.public IntegerNumber remainder(IntegerNumber n) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException
n
- the number to divide this by.java.lang.ArithmeticException
- if the value of the argument number is
zero.java.lang.IllegalArgumentException
- if the argument is
null
.public IntegerNumber gcd(IntegerNumber n) throws java.lang.IllegalArgumentException
n
- the other integer number.java.lang.IllegalArgumentException
- if n is null
.public boolean isRelativePrime(IntegerNumber n) throws java.lang.IllegalArgumentException
n
- the other integer number.true
if this and n are relative primes,
false
otherwise.java.lang.IllegalArgumentException
- if n is null
.public java.math.BigInteger toBigInteger()
BigInteger
.toBigInteger
in class RealNumber
BigInteger
with the value of this.public java.math.BigDecimal toBigDecimal()
BigDecimal
. A
precision nor a rounding mode need to be specified for integer numbers.BigDecimal
with the exact value of this.public java.math.BigDecimal toBigDecimal(int precision) throws java.lang.IllegalArgumentException
BigDecimal
with the
specified precision. The precision argument is ignored,
because no rounding is needed.toBigDecimal
in class RationalNumber
precision
- the number of digits behind the decimal point.BigDecimal
with the exact value of this.java.lang.IllegalArgumentException
- if precision < 0public java.math.BigDecimal toBigDecimal(int precision, int roundingMode)
BigDecimal
with the
specified precision and rounding mode. The precision and
roundingMode arguments are ignored, because no rounding is
needed.toBigDecimal
in class RationalNumber
precision
- the number of digits behind the decimal point.roundingMode
- the rounding mode to use, one of the modes defined
in class BigDecimal
.BigDecimal
with the rounded value of this.java.lang.IllegalArgumentException
- if one of the following applies:
public final IntegerNumber trunc()
RealNumber
trunc
in class RationalNumber
null
.public abstract byte[] toByteArray()
public abstract IntegerNumber integerDivide(IntegerNumber n) throws java.lang.ArithmeticException, java.lang.IllegalArgumentException
n
- the number to divide this by.java.lang.ArithmeticException
- if the value of the argument number is
zero.java.lang.IllegalArgumentException
- if the argument is
null
.