Class Sum
java.lang.Object
java.lang.Number
org.znerd.math.RealNumber
org.znerd.math.CompositeNumber
org.znerd.math.Sum
- All Implemented Interfaces:
Serializable, Comparable, RoundingModes
A sum of two real numbers.
Instances of
Sum must be obtained by using one of the
createInstance() factory methods.- Version:
- $Revision: 1.7 $ $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
ConstructorsModifierConstructorDescriptionprotectedSum(RealNumber a, RealNumber b) Constructs aSumbased on the 2 specified operands. -
Method Summary
Modifier and TypeMethodDescriptionprotected intCompares this number with the specified number, second level.static SumReturns aSumwith the specified operands.getElement(int n) Returns the nth operand.intCounts the number of operands.Returns the operands.toBigDecimal(int precision, int roundingMode) Converts the value of this number to aBigDecimalwith the specified precision and rounding mode.trunc()Rounds to an integer number towards 0.Methods inherited from class RealNumber
abs, add, byteValue, compareTo, compareTo, divide, doubleValue, equals, fitsByte, fitsDouble, fitsFloat, fitsInt, fitsLong, fitsShort, floatValue, getSign, intValue, invert, longValue, multiply, negate, pow, powImpl, round, shortValue, subtract, toBigDecimal, toBigInteger, toString
-
Constructor Details
-
Sum
Constructs aSumbased on the 2 specified operands.- Parameters:
a- the first operand for the sum, notnull.b- the second operand for the sum, notnull.- Throws:
IllegalArgumentException- ifa == null || b == null.CanNotCompareException- if the sign of this sum cannot be determined because the 2 arguments cannot be compared.
-
-
Method Details
-
createInstance
public static Sum createInstance(RealNumber a, RealNumber b) throws IllegalArgumentException, CanNotCompareException Returns aSumwith the specified operands.- Parameters:
a- the first operand, notnull.b- the second operand, notnull.- Returns:
- the
Suminstance, possibly newly constructed. - Throws:
IllegalArgumentException- ifa == null || b == null.CanNotCompareException- if the sign of this sum cannot be determined because the 2 arguments cannot be compared.
-
compareToImpl
Description copied from class:RealNumberCompares this number with the specified number, second level. The implementation of this method in classRealNumberthrows aCanNotCompareException. Concrete subclasses are encouraged to improve this behaviour by overriding this method. Note that this method does not check ifn == null. This is already done inRealNumber.compareTo(RealNumber).- Overrides:
compareToImplin classRealNumber- 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.IllegalArgumentException
-
toBigDecimal
Converts the value of this number to aBigDecimalwith the specified precision and rounding mode.- Specified by:
toBigDecimalin classRealNumber- 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 precision<0 or the rounding mode is not one of the valid rounding modes defined in classBigDecimal.
-
trunc
Rounds to an integer number towards 0.- Overrides:
truncin classRealNumber- Returns:
- this number truncated to an integer.
-
getElements
Description copied from class:CompositeNumberReturns the operands.- Specified by:
getElementsin classCompositeNumber- Returns:
- a new array, containing the operands.
-
getElementCount
public int getElementCount()Description copied from class:CompositeNumberCounts the number of operands.- Specified by:
getElementCountin classCompositeNumber- Returns:
- the operand count, >= 0.
-
getElement
Description copied from class:CompositeNumberReturns the nth operand.- Specified by:
getElementin classCompositeNumber- Parameters:
n- the index of the operand, >= 0 and <CompositeNumber.getElementCount().- Returns:
- the nth operand, not
null. - Throws:
IndexOutOfBoundsException- if one of the following applies:n < 0n >= operandCount
-