org.znerd.math
public class Sum extends CompositeNumber
Sum
must be obtained by using one of the
createInstance()
factory methods.MAXIMUM_RADIX
ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_UP
Modifier | Constructor and Description |
---|---|
protected |
Sum(RealNumber a,
RealNumber b)
Constructs a
Sum based on the 2 specified operands. |
Modifier and Type | Method and Description |
---|---|
protected int |
compareToImpl(RealNumber n)
Compares this number with the specified number, second level.
|
static Sum |
createInstance(RealNumber a,
RealNumber b)
Returns a
Sum with the specified operands. |
RealNumber |
getElement(int n)
Returns the nth operand.
|
int |
getElementCount()
Counts the number of operands.
|
RealNumber[] |
getElements()
Returns the operands.
|
java.math.BigDecimal |
toBigDecimal(int precision,
int roundingMode)
Converts the value of this number to a
BigDecimal with the
specified precision and rounding mode. |
IntegerNumber |
trunc()
Rounds to an integer number towards 0.
|
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
protected Sum(RealNumber a, RealNumber b) throws java.lang.IllegalArgumentException, CanNotCompareException
Sum
based on the 2 specified operands.a
- the first operand for the sum, not null
.b
- the second operand for the sum, not null
.java.lang.IllegalArgumentException
- if a == null || b == null
.CanNotCompareException
- if the sign of this sum cannot be determined because the 2 arguments
cannot be compared.public static Sum createInstance(RealNumber a, RealNumber b) throws java.lang.IllegalArgumentException, CanNotCompareException
Sum
with the specified operands.a
- the first operand, not null
.b
- the second operand, not null
.Sum
instance, possibly newly constructed.java.lang.IllegalArgumentException
- if a == null || b == null
.CanNotCompareException
- if the sign of this sum cannot be determined because the 2 arguments
cannot be compared.protected int compareToImpl(RealNumber n) throws java.lang.IllegalArgumentException, CanNotCompareException
RealNumber
RealNumber
throws a CanNotCompareException
. Concrete subclasses are
encouraged to improve this behaviour by overriding this method.
Note that this method does not check if n == null
.
This is already done in RealNumber.compareTo(RealNumber)
.compareToImpl
in class RealNumber
n
- the number to compare to, guaranteed to be not null
.CanNotCompareException
- if the comparison failed.java.lang.IllegalArgumentException
public java.math.BigDecimal toBigDecimal(int precision, int roundingMode) throws java.lang.IllegalArgumentException
BigDecimal
with the
specified precision and rounding mode.toBigDecimal
in class RealNumber
precision
- the number of digits behind the decimal point.roundingMode
- the rounding mode to use, one of the modes defined
in class BigDecimal
.BigDecimal
with the rounded value of this.java.lang.IllegalArgumentException
- if precision<0 or
the rounding mode is not one of the valid rounding modes defined in
class BigDecimal
.public IntegerNumber trunc()
trunc
in class RealNumber
public RealNumber[] getElements()
CompositeNumber
getElements
in class CompositeNumber
public int getElementCount()
CompositeNumber
getElementCount
in class CompositeNumber
public RealNumber getElement(int n) throws java.lang.IndexOutOfBoundsException
CompositeNumber
getElement
in class CompositeNumber
n
- the index of the operand, >= 0 and <
CompositeNumber.getElementCount()
.null
.java.lang.IndexOutOfBoundsException
- if one of the following applies:
n < 0
n >= operandCount