Class IntegerNumber
java.lang.Object
java.lang.Number
org.znerd.math.RealNumber
org.znerd.math.RationalNumber
org.znerd.math.IntegerNumber
- All Implemented Interfaces:
Serializable, Comparable, RoundingModes
- Direct Known Subclasses:
BigIntegerNumber, SmallIntegerNumber
An immutable integer number. The base for all integer numbers. It extends
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)- adding two integer numbers will result in another integer number
subtract(RationalNumber)- subtracting one rational number from another will result in an integer number
multiply(RationalNumber)- multiplying two integer numbers will result in another integer number
getSign()- Determines the sign of this number
negate()- Computes -this
add(IntegerNumber)- Computes this+n, where n is an integer number
multiply(IntegerNumber)- Computes this*n, where n is an integer number
integerDivide(IntegerNumber)- Computes the integer result of a division
longValue()- Converts the value of this number to a
long doubleValue()- Converts the value of this number to a
double toByteArray()- Converts the value of this number to a set of bytes that represent the two's complement of this number
final need no further optimization.- Version:
- $Revision: 1.13 $ $Date: 2004/04/15 21:50:12 $
- 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
ConstructorsModifierConstructorDescriptionprotectedIntegerNumber(int sign, String asString) Creates a newIntegerNumberwith the specified sign and the specified textual representation. -
Method Summary
Modifier and TypeMethodDescriptionfinal RealNumberabs()Computes |this|.final IntegerNumberComputes |this| and returns anIntegerNumber.abstract IntegerNumberadd(IntegerNumber n) Computes this + n, where n is an integer number.Computes this+n, where n is a rational number.protected final intCompares this number with the specified integer number, first level.protected intCompares this number with the specified integer number, second level.protected intCompares this number with the specified rational number, second level.dec()Computes this-1.Computes this/n, where n is a rational number.fac()Computes this!.gcd(IntegerNumber n) Computes the greatest common divisor of this and n.inc()Computes this+1.abstract IntegerNumberComputes the integer result of this/n, where n is an integer number.invert()Computes 1/this.booleanDetermines if this and n are relative primes.abstract IntegerNumberComputes this * n, where n is an integer number.Computes this*n, where n is a rational number.final RealNumbernegate()Computes -this.Computes -this and returns anIntegerNumber.Computes the remainder of this/n, where n is an integer number.Computes this - n, where n is an integer number.Computes this-n, where n is a rational number.Converts the value of this number to aBigDecimal.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.Converts the value of this number to aBigInteger.abstract byte[]Returns the two's-complement representation of this integer number.final IntegerNumbertrunc()Rounds to an integer number towards 0.Methods inherited from class RationalNumber
add, compareTo, compareToImpl, compareToImpl2, divide, doubleValue, getDenominator, getNumerator, multiply, pow, powImpl, subtractMethods inherited from class RealNumber
byteValue, compareTo, compareTo, equals, fitsByte, fitsDouble, fitsFloat, fitsInt, fitsLong, fitsShort, floatValue, getSign, intValue, longValue, pow, round, shortValue, toString
-
Constructor Details
-
IntegerNumber
Creates a newIntegerNumberwith the specified sign and the specified textual representation. This constructor can only be called from subclass constructors, since this class is abstract.- Parameters:
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, notnull.
-
-
Method Details
-
compareToImpl
Description copied from class:RationalNumberCompares this number with the specified rational number, second level. This method is called fromRationalNumber.compareTo(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.- Overrides:
compareToImplin classRationalNumber- 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 integer number, first level. This method callscompareToImpl(IntegerNumber). If that method throws aCanNotCompareException, then it will attempt to use a fallback comparison algorithm based ontoByteArray().- 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 integer number, second level. This method is called fromcompareTo(IntegerNumber). The implementation of this method in classIntegerNumberjust 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.
-
abs
Description copied from class:RealNumberComputes |this|. The returned value is always >= 0.- Overrides:
absin classRealNumber- Returns:
- the absolute of this, not
nulland always with a value >= 0.
-
absInteger
Computes |this| and returns anIntegerNumber. The returned value is always >= 0. The only difference between this method andabs()is that this method is explicitly defined to return anIntegerNumber. In factabs()calls this method.- Returns:
- the absolute of this, not
nulland always with a value >= 0.
-
negate
Description copied from class:RealNumberComputes -this.- Overrides:
negatein classRationalNumber- Returns:
- the negative of this, not
null.
-
negateInteger
-
invert
Description copied from class:RealNumberComputes 1/this.- Overrides:
invertin classRationalNumber- Returns:
- the inverse of this, not
null. - Throws:
ArithmeticException- if the value of this is zero.
-
add
Description copied from class:RationalNumberComputes this+n, where n is a rational number.- Overrides:
addin classRationalNumber- Parameters:
n- the number to add to this.- Returns:
- the sum of this and n.
- Throws:
IllegalArgumentException- if n ==null.
-
add
Computes this + n, where n is an integer number.- Parameters:
n- the number to add to this, notnull.- Returns:
- the sum of this and n, never
null. - Throws:
IllegalArgumentException- if the argument isnull.
-
subtract
Description copied from class:RationalNumberComputes this-n, where n is a rational number.- Overrides:
subtractin classRationalNumber- Parameters:
n- the number to subtract from this.- Returns:
- this minus n.
- Throws:
IllegalArgumentException- if n ==null.
-
subtract
Computes this - n, where n is an integer number.- Parameters:
n- the number to subtract from this, notnull.- Returns:
- this minus n, not
null. - Throws:
IllegalArgumentException- ifn == null.
-
multiply
Description copied from class:RationalNumberComputes this*n, where n is a rational number.- Overrides:
multiplyin classRationalNumber- Parameters:
n- the number to multiply this by.- Returns:
- the product of this and n.
- Throws:
IllegalArgumentException- if n ==null.
-
multiply
Computes this * n, where n is an integer number.- Parameters:
n- the number to multiply this by, notnull.- Returns:
- the product of this and n, not
null. - Throws:
IllegalArgumentException- ifn == null.
-
divide
Description copied from class:RationalNumberComputes this/n, where n is a rational number.- Overrides:
dividein classRationalNumber- 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
- Overrides:
powImplin classRationalNumber- Throws:
IllegalArgumentException
-
dec
-
inc
-
fac
Computes this!. The value of this cannot be negative.- Returns:
- the faculty of this, not
null. - Throws:
ArithmeticException- if the value of this number is < 0.
-
remainder
public IntegerNumber remainder(IntegerNumber n) throws ArithmeticException, IllegalArgumentException Computes the remainder of this/n, where n is an integer number.- Parameters:
n- the number to divide this by.- Returns:
- the remainder.
- Throws:
ArithmeticException- if the value of the argument number is zero.IllegalArgumentException- if the argument isnull.
-
gcd
Computes the greatest common divisor of this and n.- Parameters:
n- the other integer number.- Returns:
- the greatest common divisor.
- Throws:
IllegalArgumentException- if n isnull.
-
isRelativePrime
Determines if this and n are relative primes.- Parameters:
n- the other integer number.- Returns:
trueif this and n are relative primes,falseotherwise.- Throws:
IllegalArgumentException- if n isnull.
-
toBigInteger
Converts the value of this number to aBigInteger.- Overrides:
toBigIntegerin classRealNumber- Returns:
- a
BigIntegerwith the value of this.
-
toBigDecimal
Converts the value of this number to aBigDecimal. A precision nor a rounding mode need to be specified for integer numbers.- Returns:
- a
BigDecimalwith the exact value of this.
-
toBigDecimal
Converts the value of this number to aBigDecimalwith the specified precision. The precision argument is ignored, because no rounding is needed.- Overrides:
toBigDecimalin classRationalNumber- Parameters:
precision- the number of digits behind the decimal point.- Returns:
- a
BigDecimalwith the exact value of this. - Throws:
IllegalArgumentException- if precision < 0
-
toBigDecimal
Converts the value of this number to aBigDecimalwith the specified precision and rounding mode. The precision and roundingMode arguments are ignored, because no rounding is needed.- Overrides:
toBigDecimalin classRationalNumber- Parameters:
precision- the number of digits behind the decimal point.roundingMode- the rounding mode to use, one of the modes defined in classBigDecimal.- Returns:
- a
BigDecimalwith the rounded value of this. - Throws:
IllegalArgumentException- if one of the following applies:- precision < 0
- roundingMode does not have a valid value
-
trunc
Description copied from class:RealNumberRounds to an integer number towards 0.- Overrides:
truncin classRationalNumber- Returns:
- this real number truncated to an integer, never
null.
-
toByteArray
public abstract byte[] toByteArray()Returns the two's-complement representation of this integer number. The array is big-endian (i.e., the most significant byte is in the [0] position). The array contains the minimum number of bytes required to represent the number.- Returns:
- a byte array containing the bits of this integer number.
-
integerDivide
public abstract IntegerNumber integerDivide(IntegerNumber n) throws ArithmeticException, IllegalArgumentException Computes the integer result of this/n, where n is an integer number.- Parameters:
n- the number to divide this by.- Returns:
- the integer result of this divided by n.
- Throws:
ArithmeticException- if the value of the argument number is zero.IllegalArgumentException- if the argument isnull.
-