Class NumberMath
java.lang.Object
org.codehaus.groovy.runtime.typehandling.NumberMath
- Direct Known Subclasses:
BigDecimalMath,BigIntegerMath,FloatingPointMath,IntegerMath,LongMath
public abstract class NumberMath extends Object
Stateless objects used to perform math on the various Number subclasses.
Instances are required so that polymorphic calls work properly, but each
subclass creates a singleton instance to minimize garbage. All methods
must be thread-safe.
The design goals of this class are as follows:
- Support a 'least surprising' math model to scripting language users. This means that exact, or decimal math should be used for default calculations. This scheme assumes that by default, groovy literals with decimal points are instantiated as BigDecimal objects rather than binary floating points (Float, Double).
- Do not force the appearance of exactness on a number that is by definition not guaranteed to be exact. In particular this means that if an operand in a NumberMath operation is a binary floating point number, ensure that the result remains a binary floating point number (i.e. never automatically promote a binary floating point number to a BigDecimal). This has the effect of preserving the expectations of binary floating point users and helps performance.
- Provide an implementation that is as close as practical to the Java 1.5 BigDecimal math model which implements precision based floating point decimal math (ANSI X3.274-1996 and ANSI X3.274-1996/AM 1-2000 (section 7.4).
-
Constructor Summary
Constructors Constructor Description NumberMath() -
Method Summary
Modifier and Type Method Description static Numberabs(Number number)protected abstract NumberabsImpl(Number number)static Numberadd(Number left, Number right)abstract NumberaddImpl(Number left, Number right)static Numberand(Number left, Number right)protected NumberandImpl(Number left, Number right)static NumberbitwiseNegate(Number left)protected NumberbitwiseNegateImpl(Number left)static intcompareTo(Number left, Number right)abstract intcompareToImpl(Number left, Number right)protected UnsupportedOperationExceptioncreateUnsupportedException(String operation, Number left)static Numberdivide(Number left, Number right)abstract NumberdivideImpl(Number left, Number right)static NumberMathgetMath(Number left, Number right)Determine which NumberMath instance to use, given the supplied operands.static Numberintdiv(Number left, Number right)protected NumberintdivImpl(Number left, Number right)static booleanisBigDecimal(Number number)static booleanisBigInteger(Number number)static booleanisByte(Number number)static booleanisFloatingPoint(Number number)static booleanisInteger(Number number)static booleanisLong(Number number)static booleanisShort(Number number)static NumberleftShift(Number left, Number right)For this operation, consider the operands independently.protected NumberleftShiftImpl(Number left, Number right)static Numbermod(Number left, Number right)protected NumbermodImpl(Number left, Number right)static Numbermultiply(Number left, Number right)abstract NumbermultiplyImpl(Number left, Number right)static Numberor(Number left, Number right)protected NumberorImpl(Number left, Number right)static NumberrightShift(Number left, Number right)For this operation, consider the operands independently.protected NumberrightShiftImpl(Number left, Number right)static NumberrightShiftUnsigned(Number left, Number right)For this operation, consider the operands independently.protected NumberrightShiftUnsignedImpl(Number left, Number right)static Numbersubtract(Number left, Number right)abstract NumbersubtractImpl(Number left, Number right)static BigDecimaltoBigDecimal(Number n)static BigIntegertoBigInteger(Number n)static NumberunaryMinus(Number left)protected abstract NumberunaryMinusImpl(Number left)static NumberunaryPlus(Number left)protected abstract NumberunaryPlusImpl(Number left)static Numberxor(Number left, Number right)protected NumberxorImpl(Number left, Number right)
-
Constructor Details
-
NumberMath
public NumberMath()
-
-
Method Details
-
abs
-
add
-
subtract
-
multiply
-
divide
-
compareTo
-
or
-
and
-
xor
-
intdiv
-
mod
-
leftShift
For this operation, consider the operands independently. Throw an exception if the right operand (shift distance) is not an integral type. For the left operand (shift value) also require an integral type, but do NOT promote from Integer to Long. This is consistent with Java, and makes sense for the shift operators. -
rightShift
For this operation, consider the operands independently. Throw an exception if the right operand (shift distance) is not an integral type. For the left operand (shift value) also require an integral type, but do NOT promote from Integer to Long. This is consistent with Java, and makes sense for the shift operators. -
rightShiftUnsigned
For this operation, consider the operands independently. Throw an exception if the right operand (shift distance) is not an integral type. For the left operand (shift value) also require an integral type, but do NOT promote from Integer to Long. This is consistent with Java, and makes sense for the shift operators. -
bitwiseNegate
-
unaryMinus
-
unaryPlus
-
isFloatingPoint
-
isInteger
-
isShort
-
isByte
-
isLong
-
isBigDecimal
-
isBigInteger
-
toBigDecimal
-
toBigInteger
-
getMath
Determine which NumberMath instance to use, given the supplied operands. This method implements the type promotion rules discussed in the documentation. Note that by the time this method is called, any Byte, Character or Short operands will have been promoted to Integer. For reference, here is the promotion matrix: bD bI D F L I bD bD bD D D bD bD bI bD bI D D bI bI D D D D D D D F D D D D D D L bD bI D D L L I bD bI D D L I Note that for division, if either operand isFloatingPoint, the result will be floating. Otherwise, the result is BigDecimal -
absImpl
-
addImpl
-
subtractImpl
-
multiplyImpl
-
divideImpl
-
compareToImpl
-
unaryMinusImpl
-
unaryPlusImpl
-
bitwiseNegateImpl
-
orImpl
-
andImpl
-
xorImpl
-
modImpl
-
intdivImpl
-
leftShiftImpl
-
rightShiftImpl
-
rightShiftUnsignedImpl
-
createUnsupportedException
-