Overview | Package | Class | Deprecated | Index | Help
| Groovy Documentation
| FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.codehaus.groovy.runtime.typehandling
[Java] Class NumberMath

    java.lang.Object
      org.codehaus.groovy.runtime.typehandling.NumberMath

    public abstract class NumberMath
    extends java.lang.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:

1. 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).
2. 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.
3. 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).

Authors:
  Steve Goetze

Method Summary
`static java.lang.Number` | `abs(java.lang.Number number)`
`protected java.lang.Number` | `absImpl(java.lang.Number number)`
`static java.lang.Number` | `add(java.lang.Number left, java.lang.Number right)`
`java.lang.Number` | `addImpl(java.lang.Number left, java.lang.Number right)`
`static java.lang.Number` | `and(java.lang.Number left, java.lang.Number right)`
`protected java.lang.Number` | `andImpl(java.lang.Number left, java.lang.Number right)`
`static int` | `compareTo(java.lang.Number left, java.lang.Number right)`
`int` | `compareToImpl(java.lang.Number left, java.lang.Number right)`
`protected java.lang.UnsupportedOperationException` | `createUnsupportedException(java.lang.String operation, java.lang.Number left)`
`static java.lang.Number` | `divide(java.lang.Number left, java.lang.Number right)`
`java.lang.Number` | `divideImpl(java.lang.Number left, java.lang.Number right)`
`static NumberMath` | `getMath(java.lang.Number left, java.lang.Number right)` Determine which NumberMath instance to use, given the supplied operands.
`static java.lang.Number` | `intdiv(java.lang.Number left, java.lang.Number right)`
`protected java.lang.Number` | `intdivImpl(java.lang.Number left, java.lang.Number right)`
`static boolean` | `isBigDecimal(java.lang.Number number)`
`static boolean` | `isBigInteger(java.lang.Number number)`
`static boolean` | `isFloatingPoint(java.lang.Number number)`
`static boolean` | `isInteger(java.lang.Number number)`
`static boolean` | `isLong(java.lang.Number number)`
`static java.lang.Number` | `leftShift(java.lang.Number left, java.lang.Number right)` For this operation, consider the operands independently.
`protected java.lang.Number` | `leftShiftImpl(java.lang.Number left, java.lang.Number right)`
`static java.lang.Number` | `mod(java.lang.Number left, java.lang.Number right)`
`protected java.lang.Number` | `modImpl(java.lang.Number left, java.lang.Number right)`
`static java.lang.Number` | `multiply(java.lang.Number left, java.lang.Number right)`
`java.lang.Number` | `multiplyImpl(java.lang.Number left, java.lang.Number right)`
`static java.lang.Number` | `or(java.lang.Number left, java.lang.Number right)`
`protected java.lang.Number` | `orImpl(java.lang.Number left, java.lang.Number right)`
`static java.lang.Number` | `rightShift(java.lang.Number left, java.lang.Number right)` For this operation, consider the operands independently.
`protected java.lang.Number` | `rightShiftImpl(java.lang.Number left, java.lang.Number right)`
`static java.lang.Number` | `rightShiftUnsigned(java.lang.Number left, java.lang.Number right)` For this operation, consider the operands independently.
`protected java.lang.Number` | `rightShiftUnsignedImpl(java.lang.Number left, java.lang.Number right)`
`static java.lang.Number` | `subtract(java.lang.Number left, java.lang.Number right)`
`java.lang.Number` | `subtractImpl(java.lang.Number left, java.lang.Number right)`
`static java.math.BigDecimal` | `toBigDecimal(java.lang.Number n)`
`static java.math.BigInteger` | `toBigInteger(java.lang.Number n)`
`static java.lang.Number` | `unaryMinus(java.lang.Number left)`
`protected java.lang.Number` | `unaryMinusImpl(java.lang.Number left)`
`static java.lang.Number` | `xor(java.lang.Number left, java.lang.Number right)`
`protected java.lang.Number` | `xorImpl(java.lang.Number left, java.lang.Number right)`

Methods inherited from class java.lang.Object
java.lang.Object#wait(long), java.lang.Object#wait(long, int), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()

Method Detail

### abs

    public static java.lang.Number abs(java.lang.Number number)

### absImpl

    protected java.lang.Number absImpl(java.lang.Number number)

### add

    public static java.lang.Number add(java.lang.Number left, java.lang.Number right)

### addImpl

    public java.lang.Number addImpl(java.lang.Number left, java.lang.Number right)

### and

    public static java.lang.Number and(java.lang.Number left, java.lang.Number right)

### andImpl

    protected java.lang.Number andImpl(java.lang.Number left, java.lang.Number right)

### compareTo

    public static int compareTo(java.lang.Number left, java.lang.Number right)

### compareToImpl

    public int compareToImpl(java.lang.Number left, java.lang.Number right)

### createUnsupportedException

    protected java.lang.UnsupportedOperationException createUnsupportedException(java.lang.String operation, java.lang.Number left)

### divide

    public static java.lang.Number divide(java.lang.Number left, java.lang.Number right)

### divideImpl

    public java.lang.Number divideImpl(java.lang.Number left, java.lang.Number right)

### getMath

    public static NumberMath getMath(java.lang.Number left, java.lang.Number right)

  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

### intdiv

    public static java.lang.Number intdiv(java.lang.Number left, java.lang.Number right)

### intdivImpl

    protected java.lang.Number intdivImpl(java.lang.Number left, java.lang.Number right)

### isBigDecimal

    public static boolean isBigDecimal(java.lang.Number number)

### isBigInteger

    public static boolean isBigInteger(java.lang.Number number)

### isFloatingPoint

    public static boolean isFloatingPoint(java.lang.Number number)

### isInteger

    public static boolean isInteger(java.lang.Number number)

### isLong

    public static boolean isLong(java.lang.Number number)

### leftShift

    public static java.lang.Number leftShift(java.lang.Number left, java.lang.Number right)

  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.

### leftShiftImpl

    protected java.lang.Number leftShiftImpl(java.lang.Number left, java.lang.Number right)

### mod

    public static java.lang.Number mod(java.lang.Number left, java.lang.Number right)

### modImpl

    protected java.lang.Number modImpl(java.lang.Number left, java.lang.Number right)

### multiply

    public static java.lang.Number multiply(java.lang.Number left, java.lang.Number right)

### multiplyImpl

    public java.lang.Number multiplyImpl(java.lang.Number left, java.lang.Number right)

### or

    public static java.lang.Number or(java.lang.Number left, java.lang.Number right)

### orImpl

    protected java.lang.Number orImpl(java.lang.Number left, java.lang.Number right)

### rightShift

    public static java.lang.Number rightShift(java.lang.Number left, java.lang.Number right)

  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.

### rightShiftImpl

    protected java.lang.Number rightShiftImpl(java.lang.Number left, java.lang.Number right)

### rightShiftUnsigned

    public static java.lang.Number rightShiftUnsigned(java.lang.Number left, java.lang.Number right)

  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.

### rightShiftUnsignedImpl

    protected java.lang.Number rightShiftUnsignedImpl(java.lang.Number left, java.lang.Number right)

### subtract

    public static java.lang.Number subtract(java.lang.Number left, java.lang.Number right)

### subtractImpl

    public java.lang.Number subtractImpl(java.lang.Number left, java.lang.Number right)

### toBigDecimal

    public static java.math.BigDecimal toBigDecimal(java.lang.Number n)

### toBigInteger

    public static java.math.BigInteger toBigInteger(java.lang.Number n)

### unaryMinus

    public static java.lang.Number unaryMinus(java.lang.Number left)

### unaryMinusImpl

    protected java.lang.Number unaryMinusImpl(java.lang.Number left)

### xor

    public static java.lang.Number xor(java.lang.Number left, java.lang.Number right)

### xorImpl

    protected java.lang.Number xorImpl(java.lang.Number left, java.lang.Number right)

Groovy Documentation