org.znerd.math
Class DigitSet

java.lang.Object
  |
  +--java.lang.Number
        |
        +--org.znerd.math.RealNumber
              |
              +--org.znerd.math.RationalNumber
                    |
                    +--org.znerd.math.DigitSet
Direct Known Subclasses:
BasicDigitSet

public abstract class DigitSet
extends RationalNumber

A set of digits, having a radix and an exponent. Objects of this type are used to implement rounding.

The value of a DigitSet number is the sum of:

digits[0] * radixexponent
digits[1] * radixexponent - 1
digits[2] * radixexponent - 2
...
digits[precision - 1] * radixexponent - (precision - 1)

Version:
$Revision: 1.7 $ $Date: 2002/06/24 21:43:11 $
Author:
Ernst de Haan (znerd@FreeBSD.org)
See Also:
Serialized Form

Fields inherited from class org.znerd.math.RealNumber
MAXIMUM_RADIX
 
Constructor Summary
protected DigitSet(IntegerNumber[] parts, String asString)
          Creates a new DigitSet instance.
 
Method Summary
abstract  int[] getDigits()
          Returns a new array containing all the digits.
abstract  int getExponent()
          Returns the exponent.
abstract  int getPrecision()
          Returns the precision, the total number of digits.
abstract  int getRadix()
          Returns the radix or base.
 DigitSet toPrecision(int precision)
          Returns a digit set with the specified precision.
protected abstract  DigitSet toPrecisionImpl(int precision)
          Returns a digit set with the specified precision, actual implementation.
 
Methods inherited from class org.znerd.math.RationalNumber
add, add, compareTo, compareToImpl, compareToImpl, compareToImpl2, divide, divide, doubleValue, getDenominator, getNumerator, invert, multiply, multiply, negate, pow, pow, subtract, subtract, toBigDecimal, toBigDecimal, trunc
 
Methods inherited from class org.znerd.math.RealNumber
abs, byteValue, compareTo, compareTo, equals, fitsByte, fitsDouble, fitsFloat, fitsInt, fitsLong, fitsShort, floatValue, getSign, intValue, longValue, shortValue, toBigInteger, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

DigitSet

protected DigitSet(IntegerNumber[] parts,
                   String asString)
            throws IllegalArgumentException
Creates a new DigitSet instance.
Parameters:
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 this number, not null.
Throws:
IllegalArgumentException - if asString == null.
Method Detail

getRadix

public abstract int getRadix()
Returns the radix or base. The radix is for example 10 for a decimal number and 16 for a hexadecimal number. A binary number has the smallest radix, 2.
Returns:
the radix, always > 1.

getExponent

public abstract int getExponent()
Returns the exponent.
Returns:
the exponent.

getDigits

public abstract int[] getDigits()
Returns a new array containing all the digits. Every digit in the returned array is an int value between 0 and the radix (i.e. 0 <= n < radix, where n is the digit).
Returns:
a new array containing all the digits, not null

getPrecision

public abstract int getPrecision()
Returns the precision, the total number of digits.
Returns:
the precision, always >= 0.

toPrecision

public final DigitSet toPrecision(int precision)
                           throws IllegalArgumentException
Returns a digit set with the specified precision. If the specified precision is larger than or equal to the current precision, then this digit set (this) will be returned.

No rounding will be performed, only truncation. If the specified precision is smaller than the precision of this DigitSet, then some of the digits will just be removed.

This method calls toPrecisionImpl(int) after checking the preconditions.

Parameters:
precision - the precision, >= 1.
Returns:
a DigitSet that is equal to this number, truncated to the specified precision, never null.
Throws:
IllegalArgumentException - if precision < 1.

toPrecisionImpl

protected abstract DigitSet toPrecisionImpl(int precision)
Returns a digit set with the specified precision, actual implementation. This method is called from toPrecision(int).
Parameters:
precision - the precision, guaranteed to be >= 1.
Returns:
a DigitSet that is equal to this number, truncated to the specified precision.


See http://jump-math.sourceforge.net/.