Class DigitSet
java.lang.Object
java.lang.Number
org.znerd.math.RealNumber
org.znerd.math.RationalNumber
org.znerd.math.DigitSet
- All Implemented Interfaces:
Serializable, Comparable, RoundingModes
- Direct Known Subclasses:
BasicDigitSet
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.11 $ $Date: 2002/08/16 21:54:40 $
- 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
ConstructorsModifierConstructorDescriptionprotectedDigitSet(IntegerNumber[] parts, String asString) Creates a newDigitSetinstance. -
Method Summary
Modifier and TypeMethodDescriptionabstract int[]Returns a new array containing all the digits.abstract intReturns the exponent.abstract intReturns the precision, the total number of digits.abstract intgetRadix()Returns the radix or base.final DigitSettoPrecision(int precision) Returns a digit set with the specified precision.protected abstract DigitSettoPrecisionImpl(int precision) Returns a digit set with the specified precision, actual implementation.Methods inherited from class RationalNumber
add, add, compareTo, compareToImpl, compareToImpl, compareToImpl2, divide, divide, doubleValue, getDenominator, getNumerator, invert, multiply, multiply, negate, pow, powImpl, powImpl, subtract, subtract, toBigDecimal, toBigDecimal, truncMethods inherited from class RealNumber
abs, byteValue, compareTo, compareTo, equals, fitsByte, fitsDouble, fitsFloat, fitsInt, fitsLong, fitsShort, floatValue, getSign, intValue, longValue, pow, round, shortValue, toBigInteger, toString
-
Constructor Details
-
DigitSet
Creates a newDigitSetinstance.- Parameters:
parts- an array containing the numerator and denonimator, notnull, having at least 2 elements and not having anullat index 0 or 1.asString- textual presentation of this number, notnull.- Throws:
IllegalArgumentException- ifasString == null.
-
-
Method Details
-
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 anintvalue between 0 (inclusive) and the radix (exclusive).- Returns:
- a new array containing all the digits;
r != null && r[i] >= 0 && r[i] < getRadix(), where r is the return value and i is anintbetween 0 andr.length.
-
getPrecision
public abstract int getPrecision()Returns the precision, the total number of digits.- Returns:
- the precision, always >= 0.
-
toPrecision
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 thisDigitSet, then some of the digits will just be removed. This method callstoPrecisionImpl(int)after checking the preconditions. If that method returns an invalid value, then anInternalErroris thrown.- Parameters:
precision- the precision, >= 1.- Returns:
- a
DigitSetthat is equal to this number, truncated to the specified precision, nevernull. - Throws:
IllegalArgumentException- ifprecision < 1.
-
toPrecisionImpl
Returns a digit set with the specified precision, actual implementation. This method is called fromtoPrecision(int).- Parameters:
precision- the precision, guaranteed to be >= 1 and <getPrecision().- Returns:
- a
DigitSetthat is equal to this number, truncated to the specified precision.
-