org.znerd.math
Class Sum
java.lang.Object
|
+--java.lang.Number
|
+--org.znerd.math.RealNumber
|
+--org.znerd.math.CompositeNumber
|
+--org.znerd.math.Sum
- public class Sum
- extends CompositeNumber
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:
- Serialized Form
Methods inherited from class org.znerd.math.RealNumber |
abs,
add,
byteValue,
compareTo,
compareTo,
divide,
doubleValue,
equals,
fitsByte,
fitsDouble,
fitsFloat,
fitsInt,
fitsLong,
fitsShort,
floatValue,
getSign,
intValue,
invert,
longValue,
multiply,
negate,
pow,
round,
shortValue,
subtract,
toBigDecimal,
toBigInteger,
toString |
Sum
protected Sum(RealNumber a,
RealNumber b)
throws IllegalArgumentException,
CanNotCompareException
- Constructs a
Sum
based on the 2 specified operands.
- Parameters:
a
- the first operand for the sum, not null
.b
- the second operand for the sum, not null
.- Throws:
- IllegalArgumentException - if
a == null || b == null
.- CanNotCompareException - if the sign of this sum cannot be determined because the 2 arguments
cannot be compared.
createInstance
public static Sum createInstance(RealNumber a,
RealNumber b)
throws IllegalArgumentException,
CanNotCompareException
- Returns a
Sum
with the specified operands.
- Parameters:
a
- the first operand, not null
.b
- the second operand, not null
.- Returns:
- the
Sum
instance, possibly newly constructed. - Throws:
- IllegalArgumentException - if
a == null || b == null
.- CanNotCompareException - if the sign of this sum cannot be determined because the 2 arguments
cannot be compared.
compareToImpl
protected int compareToImpl(RealNumber n)
throws IllegalArgumentException,
CanNotCompareException
- Description copied from class: RealNumber
- Compares this number with the specified number, second level.
The implementation of this method in class
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)
.
- Overrides:
- compareToImpl in class RealNumber
- Tags copied from class: RealNumber
- Parameters:
n
- the number to compare to, guaranteed to be not null
.- Returns:
- -1 if this < n,
0 if this == n,
1 if this > n.
- Throws:
- CanNotCompareException - if the comparison failed.
toBigDecimal
public BigDecimal toBigDecimal(int precision,
int roundingMode)
throws IllegalArgumentException
- Converts the value of this number to a
BigDecimal
with the
specified precision and rounding mode.
- Overrides:
- toBigDecimal in class RealNumber
- Parameters:
precision
- the number of digits behind the decimal point.roundingMode
- the rounding mode to use, one of the modes defined
in class BigDecimal
.- Returns:
- a
BigDecimal
with the rounded value of this. - Throws:
- IllegalArgumentException - if precision<0 or
the rounding mode is not one of the valid rounding modes defined in
class
BigDecimal
.
trunc
public IntegerNumber trunc()
- Rounds to an integer number towards 0.
- Overrides:
- trunc in class RealNumber
- Returns:
- this number truncated to an integer.
getElements
public RealNumber[] getElements()
- Description copied from class: CompositeNumber
- Returns the operands.
- Overrides:
- getElements in class CompositeNumber
- Tags copied from class: CompositeNumber
- Returns:
- a new array, containing the operands.
getElementCount
public int getElementCount()
- Description copied from class: CompositeNumber
- Counts the number of operands.
- Overrides:
- getElementCount in class CompositeNumber
- Tags copied from class: CompositeNumber
- Returns:
- the operand count, >= 0.
getElement
public RealNumber getElement(int n)
throws IndexOutOfBoundsException
- Description copied from class: CompositeNumber
- Returns the nth operand.
- Overrides:
- getElement in class CompositeNumber
- Tags copied from class: CompositeNumber
- 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 < 0
n >= operandCount
See http://jump-math.sourceforge.net/.