ModSQL
Class NumberMath

java.lang.Object
  |
  +--ModSQL.NumberMath

public class NumberMath
extends Object

A class very much like java.lang.Math, but for doing math on Number objects. It would be nice if this class could extend java.lang.Math (and be called Math itself), but since java.lang.Math is final, we can't do that.

Author:
chris.studholme@utoronto.ca

Constructor Summary
NumberMath()
           
 
Method Summary
static Number abs(Number n)
          Returns the absolute value of a Number object.
static Number add(Number a, Number b)
          Add two Number objects to produce an appropriate Number object.
static Number ceil(Number n)
          Returns the smallest (closest to negative infinity) Number that is not less than the argument and is equal to a mathematical integer.
static Number divide(Number a, Number b)
          Divide two Number objects to produce an appropriate Number object.
static Number floor(Number n)
          Returns the largest (closest to positive infinity) Number that is not greater than the argument and is equal to a mathematical integer.
static Number mod(Number n, Number d)
          Compute the least non-negative residue of n mod d.
static Number multiply(Number a, Number b)
          Multiply two Number objects to produce an appropriate Number object.
static Number negate(Number n)
          Negate a Number object.
static int sign(Number n)
          Determine the sign of a number.
static double sqrt(Number n)
          Returns the correctly rounded positive square root of a Number object.
static Number subtract(Number a, Number b)
          Subtract two Number objects to produce an appropriate Number object.
static BigDecimal toBigDecimal(Number n)
          Convert a Number object to a BigDecimal object.
static BigInteger toBigInteger(Number n)
          Convert a Number object to a BigInteger object.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Constructor Detail

NumberMath

public NumberMath()
Method Detail

toBigDecimal

public static BigDecimal toBigDecimal(Number n)
Convert a Number object to a BigDecimal object.
Parameters:
n - number to convert
Returns:
number as a BigDecimal object

toBigInteger

public static BigInteger toBigInteger(Number n)
Convert a Number object to a BigInteger object.
Parameters:
n - number to convert
Returns:
number as a BigInteger object

negate

public static Number negate(Number n)
Negate a Number object.
Parameters:
n - number to negate
Returns:
negated number
Throws:
IllegalArgumentException - if n is an unknown subtype of Number

add

public static Number add(Number a,
                         Number b)
Add two Number objects to produce an appropriate Number object.
Parameters:
a - first number
b - second number
Returns:
sum of a and b
Throws:
IllegalArgumentException - if n is an unknown subtype of Number

subtract

public static Number subtract(Number a,
                              Number b)
Subtract two Number objects to produce an appropriate Number object.

multiply

public static Number multiply(Number a,
                              Number b)
Multiply two Number objects to produce an appropriate Number object.
Parameters:
a - first number
b - second number
Returns:
product of a and b
Throws:
IllegalArgumentException - if n is an unknown subtype of Number

divide

public static Number divide(Number a,
                            Number b)
Divide two Number objects to produce an appropriate Number object.
Parameters:
a - first number
b - second number
Returns:
a/b
Throws:
IllegalArgumentException - if n is an unknown subtype of Number

abs

public static Number abs(Number n)
Returns the absolute value of a Number object.
Parameters:
n - the argument whose absolute value is to be determined
Returns:
the absolute value of the argument
Throws:
IllegalArgumentException - if n is an unknown subtype of Number

ceil

public static Number ceil(Number n)
Returns the smallest (closest to negative infinity) Number that is not less than the argument and is equal to a mathematical integer. If the number if of integer type, it is simply returned. Unknown subtypes of number are assumed to be integers.
Parameters:
n - a number
Returns:
the smallest (closest to negative infinity) floating-point value that is not less than the argument and is equal to a mathematical integer

floor

public static Number floor(Number n)
Returns the largest (closest to positive infinity) Number that is not greater than the argument and is equal to a mathematical integer. If the number if of integer type, it is simply returned. Unknown subtypes of number are assumed to be integers.
Parameters:
n - a number
Returns:
the smallest (closest to negative infinity) floating-point value that is not less than the argument and is equal to a mathematical integer

sign

public static int sign(Number n)
Determine the sign of a number. This method uses the doubleValue() method of Number and then compares the result with 0 to determine the return value.
Parameters:
n - a number
Returns:
-1 is the number is negative, +1 if it is positive, 0 otherwise

sqrt

public static double sqrt(Number n)
Returns the correctly rounded positive square root of a Number object. This method simply converts the Number to a double using doubleValue(), and then uses java.lang.Math.sqrt() to compute the square root.

mod

public static Number mod(Number n,
                         Number d)
Compute the least non-negative residue of n mod d. Both n and d must be integer types.
Parameters:
n - numerator
d - modulus
Returns:
n%d as a Number object with the same type as d
Throws:
IllegalArgumentException - if n or d is an unknown subtype of Number