org.codehaus.groovy.runtime.typehandling

## Class NumberMath

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

- Direct Known Subclasses:
    BigDecimalMath, BigIntegerMath, FloatingPointMath, IntegerMath, LongMath

      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).
  Author:
    Steve Goetze

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `NumberMath()`

  - ### Method Summary

    All Methods Static Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static java.lang.Number` | `abs(java.lang.Number number)`
    `protected abstract java.lang.Number` | `absImpl(java.lang.Number number)`
    `static java.lang.Number` | `add(java.lang.Number left, java.lang.Number right)`
    `abstract 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 java.lang.Number` | `bitwiseNegate(java.lang.Number left)`
    `protected java.lang.Number` | `bitwiseNegateImpl(java.lang.Number left)`
    `static int` | `compareTo(java.lang.Number left, java.lang.Number right)`
    `abstract 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)`
    `abstract 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)`
    `abstract 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)`
    `abstract 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 abstract java.lang.Number` | `unaryMinusImpl(java.lang.Number left)`
    `static java.lang.Number` | `unaryPlus(java.lang.Number left)`
    `protected abstract java.lang.Number` | `unaryPlusImpl(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

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### NumberMath

          public NumberMath()

  - ### Method Detail

    - #### abs

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

    - #### add

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

    - #### subtract

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

    - #### multiply

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

    - #### divide

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

    - #### compareTo

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

    - #### or

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

    - #### and

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

    - #### xor

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

    - #### intdiv

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

    - #### mod

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

    - #### 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.

    - #### 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.

    - #### 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.

    - #### bitwiseNegate

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

    - #### unaryMinus

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

    - #### unaryPlus

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

    - #### 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)

    - #### isBigDecimal

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

    - #### isBigInteger

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

    - #### toBigDecimal

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

    - #### toBigInteger

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

    - #### 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

    - #### absImpl

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

    - #### addImpl

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

    - #### subtractImpl

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

    - #### multiplyImpl

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

    - #### divideImpl

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

    - #### compareToImpl

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

    - #### unaryMinusImpl

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

    - #### unaryPlusImpl

          protected abstract java.lang.Number unaryPlusImpl(java.lang.Number left)

    - #### bitwiseNegateImpl

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

    - #### orImpl

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

    - #### andImpl

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

    - #### xorImpl

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

    - #### modImpl

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

    - #### intdivImpl

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

    - #### leftShiftImpl

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

    - #### rightShiftImpl

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

    - #### rightShiftUnsignedImpl

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

    - #### createUnsupportedException

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