Class NumberMath
- Direct Known Subclasses:
BigDecimalMath,BigIntegerMath,FloatingPointMath,IntegerMath,LongMath
- 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 -
Method Summary
Modifier and TypeMethodDescriptionstatic NumberComputes the absolute value of a number using its specific type's math operations.protected abstract NumberComputes the absolute value of a number.static NumberAdds two numbers using type promotion rules.abstract NumberAdds two numbers.static NumberBitwise AND operation on two integral numbers.protected NumberBitwise AND of two numbers.static NumberbitwiseNegate(Number left) Bitwise negation (complement) of an integral number.protected NumberbitwiseNegateImpl(Number left) Bitwise negation of a number.static intCompares two numbers using type promotion rules.abstract intcompareToImpl(Number left, Number right) Compares two numbers.protected UnsupportedOperationExceptioncreateUnsupportedException(String operation, Number left) Creates an UnsupportedOperationException for an unsupported operation.static NumberDivides two numbers using type promotion rules.abstract NumberdivideImpl(Number left, Number right) Divides two numbers.static NumberMathDetermines which NumberMath instance to use for binary operations on two operands.static NumberInteger division (without remainder) of two numbers.protected NumberintdivImpl(Number left, Number right) Integer division of two numbers.static booleanisBigDecimal(Number number) Tests whether a number is a BigDecimal.static booleanisBigInteger(Number number) Tests whether a number is a BigInteger.static booleanTests whether a number is a Byte.static booleanisFloatingPoint(Number number) Tests whether a number is a floating-point type (Float or Double).static booleanTests whether a number is an Integer.static booleanTests whether a number is a Long.static booleanTests whether a number is a Short.static NumberLeft shift operation on integral numbers.protected NumberleftShiftImpl(Number left, Number right) Left shift of a number.static NumberModulo operation on two numbers (for backwards compatibility).protected NumberModulo of two numbers.static NumberMultiplies two numbers using type promotion rules.abstract NumbermultiplyImpl(Number left, Number right) Multiplies two numbers.static NumberBitwise OR operation on two integral numbers.protected NumberBitwise OR of two numbers.static NumberRemainder operation on two numbers.protected NumberremainderImpl(Number left, Number right) Remainder of two numbers.static NumberrightShift(Number left, Number right) Right shift operation on integral numbers.protected NumberrightShiftImpl(Number left, Number right) Right shift of a number.static NumberrightShiftUnsigned(Number left, Number right) Unsigned right shift operation on integral numbers.protected NumberrightShiftUnsignedImpl(Number left, Number right) Unsigned right shift of a number.static NumberSubtracts two numbers using type promotion rules.abstract NumbersubtractImpl(Number left, Number right) Subtracts two numbers.static BigDecimalConverts a number to a BigDecimal.static BigIntegerConverts a number to a BigInteger.static NumberunaryMinus(Number left) Unary negation of a number.protected abstract NumberunaryMinusImpl(Number left) Negates a number.static NumberUnary plus (identity) operation on a number.protected abstract NumberunaryPlusImpl(Number left) Returns the number as-is (unary plus/identity).static NumberBitwise XOR operation on two integral numbers.protected NumberBitwise XOR of two numbers.
-
Constructor Details
-
NumberMath
public NumberMath()
-
-
Method Details
-
abs
Computes the absolute value of a number using its specific type's math operations.- Parameters:
number- the number (must not be null)- Returns:
- the absolute value using the appropriate NumberMath instance
-
add
Adds two numbers using type promotion rules.- Parameters:
left- the left operandright- the right operand- Returns:
- the sum using the appropriate NumberMath instance
-
subtract
Subtracts two numbers using type promotion rules.- Parameters:
left- the minuendright- the subtrahend- Returns:
- the difference using the appropriate NumberMath instance
-
multiply
Multiplies two numbers using type promotion rules.- Parameters:
left- the first multiplicandright- the second multiplicand- Returns:
- the product using the appropriate NumberMath instance
-
divide
Divides two numbers using type promotion rules.For floating-point operands, performs binary floating-point division. For integral types, delegates to BigDecimal division for exact results.
- Parameters:
left- the dividendright- the divisor- Returns:
- the quotient using the appropriate NumberMath instance
- Throws:
ArithmeticException- if right is zero
-
compareTo
Compares two numbers using type promotion rules.- Parameters:
left- the first numberright- the second number- Returns:
- a negative, zero, or positive int as left is less than, equal to, or greater than right
-
or
Bitwise OR operation on two integral numbers.- Parameters:
left- the first operandright- the second operand- Returns:
- the bitwise OR result
- Throws:
UnsupportedOperationException- if operands are not integral types
-
and
Bitwise AND operation on two integral numbers.- Parameters:
left- the first operandright- the second operand- Returns:
- the bitwise AND result
- Throws:
UnsupportedOperationException- if operands are not integral types
-
xor
Bitwise XOR operation on two integral numbers.- Parameters:
left- the first operandright- the second operand- Returns:
- the bitwise XOR result
- Throws:
UnsupportedOperationException- if operands are not integral types
-
intdiv
Integer division (without remainder) of two numbers.For BigInteger and integral types, performs exact integer division. For floating-point operands, delegates to FloatingPointMath.
- Parameters:
left- the dividendright- the divisor- Returns:
- the integer quotient
- Throws:
UnsupportedOperationException- if operands don't support integer division
-
mod
Modulo operation on two numbers (for backwards compatibility).This method is retained for backwards compatibility
- Parameters:
left- the dividendright- the divisor- Returns:
- the modulo result
-
remainder
Remainder operation on two numbers.Returns the remainder after integer division.
- Parameters:
left- the dividendright- the divisor- Returns:
- the remainder
- Throws:
UnsupportedOperationException- if operands don't support remainder operation
-
leftShift
Left shift operation on integral numbers.The shift distance (right operand) must be an integral type. The value to shift (left operand) must also be integral.
- Parameters:
left- the value to shift (must be integral)right- the shift distance (must be integral)- Returns:
- the left-shifted value
- Throws:
UnsupportedOperationException- if shift distance is floating-point or BigDecimal
-
rightShift
Right shift operation on integral numbers.The shift distance (right operand) must be an integral type. The value to shift (left operand) must also be integral.
- Parameters:
left- the value to shift (must be integral)right- the shift distance (must be integral)- Returns:
- the right-shifted value
- Throws:
UnsupportedOperationException- if shift distance is floating-point or BigDecimal
-
rightShiftUnsigned
Unsigned right shift operation on integral numbers.The shift distance (right operand) must be an integral type. The value to shift (left operand) must also be integral.
- Parameters:
left- the value to shift (must be integral)right- the shift distance (must be integral)- Returns:
- the unsigned right-shifted value
- Throws:
UnsupportedOperationException- if shift distance is floating-point or BigDecimal
-
bitwiseNegate
Bitwise negation (complement) of an integral number.- Parameters:
left- the operand (must be integral)- Returns:
- the bitwise negation
- Throws:
UnsupportedOperationException- if operand is not integral
-
unaryMinus
Unary negation of a number.- Parameters:
left- the operand- Returns:
- the negation
-
unaryPlus
Unary plus (identity) operation on a number.- Parameters:
left- the operand- Returns:
- the same value
-
isFloatingPoint
Tests whether a number is a floating-point type (Float or Double).- Parameters:
number- the number to test- Returns:
- true if the number is Float or Double
-
isInteger
Tests whether a number is an Integer.- Parameters:
number- the number to test- Returns:
- true if the number is an Integer
-
isShort
Tests whether a number is a Short.- Parameters:
number- the number to test- Returns:
- true if the number is a Short
-
isByte
Tests whether a number is a Byte.- Parameters:
number- the number to test- Returns:
- true if the number is a Byte
-
isLong
Tests whether a number is a Long.- Parameters:
number- the number to test- Returns:
- true if the number is a Long
-
isBigDecimal
Tests whether a number is a BigDecimal.- Parameters:
number- the number to test- Returns:
- true if the number is a BigDecimal
-
isBigInteger
Tests whether a number is a BigInteger.- Parameters:
number- the number to test- Returns:
- true if the number is a BigInteger
-
toBigDecimal
Converts a number to a BigDecimal.Handles BigDecimal, BigInteger, integral types, and floating-point types. Floating-point numbers are converted via their string representation to preserve precision.
- Parameters:
n- the number to convert (must not be null)- Returns:
- a BigDecimal representation of the number
-
toBigInteger
Converts a number to a BigInteger.Handles BigInteger, integral types, floating-point types, and BigDecimal. Floating-point numbers are first converted to BigDecimal before extraction.
- Parameters:
n- the number to convert (must not be null)- Returns:
- a BigInteger representation of the number
-
getMath
Determines which NumberMath instance to use for binary operations on two operands.Implements the type 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 IWhere: bD=BigDecimal, bI=BigInteger, D=Double, F=Float, L=Long, I=Integer
Note: Byte, Character, and Short operands are pre-promoted to Integer. For division, if either operand is floating-point, the result is floating-point. Otherwise, the result is BigDecimal.
- Parameters:
left- the left operandright- the right operand- Returns:
- the appropriate NumberMath instance for these operands
-
absImpl
Computes the absolute value of a number.Subclasses must implement this method according to type promotion hierarchy rules.
- Parameters:
number- the operand- Returns:
- the absolute value
-
addImpl
Adds two numbers.Subclasses must implement this method according to type promotion hierarchy rules.
- Parameters:
left- the left operandright- the right operand- Returns:
- the sum
-
subtractImpl
Subtracts two numbers.Subclasses must implement this method according to type promotion hierarchy rules.
- Parameters:
left- the minuendright- the subtrahend- Returns:
- the difference
-
multiplyImpl
Multiplies two numbers.Subclasses must implement this method according to type promotion hierarchy rules.
- Parameters:
left- the first multiplicandright- the second multiplicand- Returns:
- the product
-
divideImpl
Divides two numbers.Subclasses must implement this method according to type promotion hierarchy rules.
- Parameters:
left- the dividendright- the divisor- Returns:
- the quotient
-
compareToImpl
Compares two numbers.Subclasses must implement this method according to type promotion hierarchy rules.
- Parameters:
left- the first numberright- the second number- Returns:
- negative, zero, or positive as left is less than, equal to, or greater than right
-
unaryMinusImpl
Negates a number.Subclasses must implement this method according to type promotion hierarchy rules.
- Parameters:
left- the operand- Returns:
- the negation
-
unaryPlusImpl
Returns the number as-is (unary plus/identity).Subclasses must implement this method according to type promotion hierarchy rules.
- Parameters:
left- the operand- Returns:
- the same value
-
bitwiseNegateImpl
Bitwise negation of a number.Default implementation throws UnsupportedOperationException. Subclasses supporting bitwise operations must override.
- Parameters:
left- the operand- Returns:
- the bitwise negation
- Throws:
UnsupportedOperationException- if not supported for this type
-
orImpl
Bitwise OR of two numbers.Default implementation throws UnsupportedOperationException. Subclasses supporting bitwise operations must override.
- Parameters:
left- the first operandright- the second operand- Returns:
- the bitwise OR result
- Throws:
UnsupportedOperationException- if not supported for this type
-
andImpl
Bitwise AND of two numbers.Default implementation throws UnsupportedOperationException. Subclasses supporting bitwise operations must override.
- Parameters:
left- the first operandright- the second operand- Returns:
- the bitwise AND result
- Throws:
UnsupportedOperationException- if not supported for this type
-
xorImpl
Bitwise XOR of two numbers.Default implementation throws UnsupportedOperationException. Subclasses supporting bitwise operations must override.
- Parameters:
left- the first operandright- the second operand- Returns:
- the bitwise XOR result
- Throws:
UnsupportedOperationException- if not supported for this type
-
remainderImpl
Remainder of two numbers.Default implementation throws UnsupportedOperationException. Subclasses supporting remainder must override.
- Parameters:
left- the dividendright- the divisor- Returns:
- the remainder
- Throws:
UnsupportedOperationException- if not supported for this type
-
modImpl
Modulo of two numbers.Default implementation throws UnsupportedOperationException. Subclasses supporting modulo must override.
- Parameters:
left- the dividendright- the divisor- Returns:
- the modulo result
- Throws:
UnsupportedOperationException- if not supported for this type
-
intdivImpl
Integer division of two numbers.Default implementation throws UnsupportedOperationException. Subclasses supporting integer division must override.
- Parameters:
left- the dividendright- the divisor- Returns:
- the integer quotient
- Throws:
UnsupportedOperationException- if not supported for this type
-
leftShiftImpl
Left shift of a number.Default implementation throws UnsupportedOperationException. Subclasses supporting bit shifting must override.
- Parameters:
left- the value to shiftright- the shift distance- Returns:
- the left-shifted value
- Throws:
UnsupportedOperationException- if not supported for this type
-
rightShiftImpl
Right shift of a number.Default implementation throws UnsupportedOperationException. Subclasses supporting bit shifting must override.
- Parameters:
left- the value to shiftright- the shift distance- Returns:
- the right-shifted value
- Throws:
UnsupportedOperationException- if not supported for this type
-
rightShiftUnsignedImpl
Unsigned right shift of a number.Default implementation throws UnsupportedOperationException. Subclasses supporting bit shifting must override.
- Parameters:
left- the value to shiftright- the shift distance- Returns:
- the unsigned right-shifted value
- Throws:
UnsupportedOperationException- if not supported for this type
-
createUnsupportedException
Creates an UnsupportedOperationException for an unsupported operation.- Parameters:
operation- the name of the unsupported operationleft- the operand- Returns:
- a new UnsupportedOperationException
-