Class SmallIntegerNumber
java.lang.Object
java.lang.Number
org.znerd.math.RealNumber
org.znerd.math.RationalNumber
org.znerd.math.IntegerNumber
org.znerd.math.SmallIntegerNumber
- All Implemented Interfaces:
Serializable, Comparable, RoundingModes
Implementation of an
IntegerNumber based on a
int value.
Instances must be obtained by using one of the
createInstance() factory methods. Using this approach,
instances can be transparently cached by this class.- Version:
- $Revision: 1.23 $ $Date: 2002/08/19 18:55:33 $
- Author:
- Ernst de Haan (znerd@FreeBSD.org)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionfinal intTheintthisIntegerNumberimplementation is based on.static final SmallIntegerNumberThe maximumIntegerNumberthat can be represented by this class.static final SmallIntegerNumberThe minimumIntegerNumberthat can be represented by this class.static final SmallIntegerNumberCached instance that represents the number minus one, -1.static final SmallIntegerNumberCached instance that represents the number minus two, -2.static final SmallIntegerNumberCached instance that represents the number one, 1.static final SmallIntegerNumberCached instance that represents the number two, 2.static final SmallIntegerNumberCached instance that represents the number zero, 0.Fields inherited from class RealNumber
MAXIMUM_RADIXFields inherited from interface RoundingModes
ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_UP -
Method Summary
Modifier and TypeMethodDescriptionadd(IntegerNumber n) Computes this + n, where n is an integer number.protected intCompares this number with the specified integer number, second level.static SmallIntegerNumbercreateInstance(int n) Returns an instance of aSmallIntegerNumberbased on aintvalue.dec()Computes this-1.doubleReturns the value of this number as adouble.inc()Computes this+1.Computes the integer result of this/n, where n is an integer number.intintValue()Returns the value of this number as anint.longReturns the value of this number as along.Computes this * n, where n is an integer number.Computes -this and returns anIntegerNumber.Computes the remainder of this/n, where n is an integer number.round(int radix, int precision, RoundingMode roundingMode) Rounds to the specified radix, using the specified precision and rounding mode.Converts the value of this number to aBigInteger.byte[]Returns the two's-complement representation of this integer number.Methods inherited from class IntegerNumber
abs, absInteger, add, compareTo, compareToImpl, divide, fac, gcd, invert, isRelativePrime, multiply, negate, powImpl, subtract, subtract, toBigDecimal, toBigDecimal, toBigDecimal, truncMethods inherited from class RationalNumber
add, compareTo, compareToImpl, compareToImpl2, divide, getDenominator, getNumerator, multiply, pow, powImpl, subtractMethods inherited from class RealNumber
byteValue, compareTo, compareTo, equals, fitsByte, fitsDouble, fitsFloat, fitsInt, fitsLong, fitsShort, floatValue, getSign, pow, shortValue, toString
-
Field Details
-
MINIMUM_VALUE
The minimumIntegerNumberthat can be represented by this class. -
MAXIMUM_VALUE
The maximumIntegerNumberthat can be represented by this class. -
MINUS_TWO
Cached instance that represents the number minus two, -2. -
MINUS_ONE
Cached instance that represents the number minus one, -1. -
ZERO
Cached instance that represents the number zero, 0. -
ONE
Cached instance that represents the number one, 1. -
TWO
Cached instance that represents the number two, 2. -
_value
public final int _valueTheintthisIntegerNumberimplementation is based on.
-
-
Method Details
-
createInstance
Returns an instance of aSmallIntegerNumberbased on aintvalue.- Parameters:
n- theintto construct aSmallIntegerNumberfrom.- Returns:
- the (possibly newly constructed)
SmallIntegerNumber.
-
compareToImpl
Description copied from class:IntegerNumberCompares this number with the specified integer number, second level. This method is called fromIntegerNumber.compareTo(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.- Overrides:
compareToImplin classIntegerNumber- 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.
-
round
public DigitSet round(int radix, int precision, RoundingMode roundingMode) throws IllegalArgumentException Description copied from class:RealNumberRounds to the specified radix, using the specified precision and rounding mode. If rounding is not supported by this class, then anUnsupportedOperationExceptionis thrown. The implementation of this method in classRealNumberthrows such an exception.- Overrides:
roundin classRealNumber- Parameters:
radix- the radix, always >= 2 and <=RealNumber.MAXIMUM_RADIX.precision- the precision, always >= 1.roundingMode- the rounding mode, nevernull.- Returns:
- the rounded number, never
null. - Throws:
IllegalArgumentException- ifradix < 2 || radix >.RealNumber.MAXIMUM_RADIX|| precision < 1 || roundingMode == null
-
negateInteger
Description copied from class:IntegerNumberComputes -this and returns anIntegerNumber. The only difference between this method andIntegerNumber.negate()is that this method is explicitly defined to return anIntegerNumber. In factIntegerNumber.negate()calls this method.- Overrides:
negateIntegerin classIntegerNumber- Returns:
- the negative of this, not
null.
-
add
Description copied from class:IntegerNumberComputes this + n, where n is an integer number.- Specified by:
addin classIntegerNumber- Parameters:
n- the number to add to this, notnull.- Returns:
- the sum of this and n, never
null. - Throws:
IllegalArgumentException- if the argument isnull.
-
multiply
Description copied from class:IntegerNumberComputes this * n, where n is an integer number.- Specified by:
multiplyin classIntegerNumber- Parameters:
n- the number to multiply this by, notnull.- Returns:
- the product of this and n, not
null. - Throws:
IllegalArgumentException- ifn == null.
-
dec
Description copied from class:IntegerNumberComputes this-1.- Overrides:
decin classIntegerNumber- Returns:
- this decreased by 1.
-
inc
Description copied from class:IntegerNumberComputes this+1.- Overrides:
incin classIntegerNumber- Returns:
- this increased by 1.
-
integerDivide
public IntegerNumber integerDivide(IntegerNumber n) throws ArithmeticException, IllegalArgumentException Description copied from class:IntegerNumberComputes the integer result of this/n, where n is an integer number.- Specified by:
integerDividein classIntegerNumber- 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.
-
remainder
public IntegerNumber remainder(IntegerNumber n) throws ArithmeticException, IllegalArgumentException Description copied from class:IntegerNumberComputes the remainder of this/n, where n is an integer number.- Overrides:
remainderin classIntegerNumber- 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.
-
longValue
public long longValue()Description copied from class:RealNumberReturns the value of this number as along. This may involve rounding.- Overrides:
longValuein classRealNumber- Returns:
- the numeric value represented by this object after conversion
to type
long.
-
intValue
public int intValue()Description copied from class:RealNumberReturns the value of this number as anint. This may involve rounding.- Overrides:
intValuein classRealNumber- Returns:
- the numeric value represented by this object after conversion
to type
int.
-
doubleValue
public double doubleValue()Description copied from class:RealNumberReturns the value of this number as adouble. This may involve rounding.- Overrides:
doubleValuein classRationalNumber- Returns:
- the numeric value represented by this object after conversion
to type
double.
-
toByteArray
public byte[] toByteArray()Description copied from class:IntegerNumberReturns 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.- Specified by:
toByteArrayin classIntegerNumber- Returns:
- a byte array containing the bits of this integer number.
-
toBigInteger
Description copied from class:IntegerNumberConverts the value of this number to aBigInteger.- Overrides:
toBigIntegerin classIntegerNumber- Returns:
- a
BigIntegerwith the value of this.
-