Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.apache.commons.math.util
Class MathUtils

    java.lang.Object
      org.apache.commons.math.util.MathUtils

public final class MathUtils
extends Object

Some useful additions to the built-in functions in `Math`.

Version:
  $Revision: 321510 $ $Date: 2005-10-15 15:33:14 -0700 (Sat, 15 Oct 2005) $

Method Summary
`static int` | `addAndCheck(int x, int y)` Add two integers, checking for overflow.
`static long` | `binomialCoefficient(int n, int k)` Returns an exact representation of the Binomial Coefficient, "`n choose k`", the number of `k`-element subsets that can be selected from an `n`-element set.
`static double` | `binomialCoefficientDouble(int n, int k)` Returns a `double` representation of the Binomial Coefficient, "`n choose k`", the number of `k`-element subsets that can be selected from an `n`-element set.
`static double` | `binomialCoefficientLog(int n, int k)` Returns the natural `log` of the Binomial Coefficient, "`n choose k`", the number of `k`-element subsets that can be selected from an `n`-element set.
`static double` | `cosh(double x)` Returns the hyperbolic cosine of x.
`static boolean` | `equals(double x, double y)` Returns true iff both arguments are NaN or neither is NaN and they are equal
`static long` | `factorial(int n)` Returns n!.
`static double` | `factorialDouble(int n)` Returns n!.
`static double` | `factorialLog(int n)` Returns the natural logarithm of n!.
`static int` | `gcd(int u, int v)` Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations.
`static int` | `hash(double value)` Returns an integer hash code representing the given double value.
`static byte` | `indicator(byte x)` For a byte value x, this method returns (byte)(+1) if x >= 0 and (byte)(-1) if x < 0.
`static double` | `indicator(double x)` For a double precision value x, this method returns +1.0 if x >= 0 and -1.0 if x < 0.
`static float` | `indicator(float x)` For a float value x, this method returns +1.0F if x >= 0 and -1.0F if x < 0.
`static int` | `indicator(int x)` For an int value x, this method returns +1 if x >= 0 and -1 if x < 0.
`static long` | `indicator(long x)` For a long value x, this method returns +1L if x >= 0 and -1L if x < 0.
`static short` | `indicator(short x)` For a short value x, this method returns (short)(+1) if x >= 0 and (short)(-1) if x < 0.
`static int` | `lcm(int a, int b)` Returns the least common multiple between two integer values.
`static int` | `mulAndCheck(int x, int y)` Multiply two integers, checking for overflow.
`static double` | `round(double x, int scale)` Round the given value to the specified number of decimal places.
`static double` | `round(double x, int scale, int roundingMethod)` Round the given value to the specified number of decimal places.
`static float` | `round(float x, int scale)` Round the given value to the specified number of decimal places.
`static float` | `round(float x, int scale, int roundingMethod)` Round the given value to the specified number of decimal places.
`static byte` | `sign(byte x)` Returns the sign for byte value `x`.
`static double` | `sign(double x)` Returns the sign for double precision `x`.
`static float` | `sign(float x)` Returns the sign for float value `x`.
`static int` | `sign(int x)` Returns the sign for int value `x`.
`static long` | `sign(long x)` Returns the sign for long value `x`.
`static short` | `sign(short x)` Returns the sign for short value `x`.
`static double` | `sinh(double x)` Returns the hyperbolic sine of x.
`static int` | `subAndCheck(int x, int y)` Subtract two integers, checking for overflow.

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Method Detail

### addAndCheck

    public static int addAndCheck(int x,
                                  int y)

  Add two integers, checking for overflow.

  Parameters:
    `x` - an addend
    `y` - an addend
  Returns:
    the sum `x+y`
  Throws:
    `ArithmeticException` - if the result can not be represented as an int
  Since:
    1.1

### binomialCoefficient

    public static long binomialCoefficient(int n,
                                           int k)

  Returns an exact representation of the Binomial Coefficient, "`n choose k`", the number of `k`-element subsets that can be selected from an `n`-element set.

  Preconditions:

  - `0 <= k <= n` (otherwise `IllegalArgumentException` is thrown)
  - The result is small enough to fit into a `long`. The largest value of `n` for which all coefficients are `< Long.MAX_VALUE` is 66. If the computed value exceeds `Long.MAX_VALUE` an `ArithMeticException` is thrown.

  Parameters:
    `n` - the size of the set
    `k` - the size of the subsets to be counted
  Returns:
    `n choose k`
  Throws:
    `IllegalArgumentException` - if preconditions are not met.
    `ArithmeticException` - if the result is too large to be represented by a long integer.

### binomialCoefficientDouble

    public static double binomialCoefficientDouble(int n,
                                                   int k)

  Returns a `double` representation of the Binomial Coefficient, "`n choose k`", the number of `k`-element subsets that can be selected from an `n`-element set.

  Preconditions:

  - `0 <= k <= n` (otherwise `IllegalArgumentException` is thrown)
  - The result is small enough to fit into a `double`. The largest value of `n` for which all coefficients are < Double.MAX_VALUE is 1029. If the computed value exceeds Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned

  Parameters:
    `n` - the size of the set
    `k` - the size of the subsets to be counted
  Returns:
    `n choose k`
  Throws:
    `IllegalArgumentException` - if preconditions are not met.

### binomialCoefficientLog

    public static double binomialCoefficientLog(int n,
                                                int k)

  Returns the natural `log` of the Binomial Coefficient, "`n choose k`", the number of `k`-element subsets that can be selected from an `n`-element set.

  Preconditions:

  - `0 <= k <= n` (otherwise `IllegalArgumentException` is thrown)

  Parameters:
    `n` - the size of the set
    `k` - the size of the subsets to be counted
  Returns:
    `n choose k`
  Throws:
    `IllegalArgumentException` - if preconditions are not met.

### cosh

    public static double cosh(double x)

  Returns the hyperbolic cosine of x.

  Parameters:
    `x` - double value for which to find the hyperbolic cosine
  Returns:
    hyperbolic cosine of x

### equals

    public static boolean equals(double x,
                                 double y)

  Returns true iff both arguments are NaN or neither is NaN and they are equal

  Parameters:
    `x` - first value
    `y` - second value
  Returns:
    true if the values are equal or both are NaN

### factorial

    public static long factorial(int n)

  Returns n!. Shorthand for `n` Factorial, the product of the numbers `1,...,n`.

  Preconditions:

  - `n >= 0` (otherwise `IllegalArgumentException` is thrown)
  - The result is small enough to fit into a `long`. The largest value of `n` for which `n!` < Long.MAX_VALUE is 20. If the computed value exceeds `Long.MAX_VALUE` an `ArithMeticException` is thrown.

  Parameters:
    `n` - argument
  Returns:
    `n!`
  Throws:
    `ArithmeticException` - if the result is too large to be represented by a long integer.
    `IllegalArgumentException` - if n < 0

### factorialDouble

    public static double factorialDouble(int n)

  Returns n!. Shorthand for `n` Factorial, the product of the numbers `1,...,n` as a `double`.

  Preconditions:

  - `n >= 0` (otherwise `IllegalArgumentException` is thrown)
  - The result is small enough to fit into a `double`. The largest value of `n` for which `n!` < Double.MAX_VALUE is 170. If the computed value exceeds Double.MAX_VALUE, Double.POSITIVE_INFINITY is returned

  Parameters:
    `n` - argument
  Returns:
    `n!`
  Throws:
    `IllegalArgumentException` - if n < 0

### factorialLog

    public static double factorialLog(int n)

  Returns the natural logarithm of n!.

  Preconditions:

  - `n >= 0` (otherwise `IllegalArgumentException` is thrown)

  Parameters:
    `n` - argument
  Returns:
    `n!`
  Throws:
    `IllegalArgumentException` - if preconditions are not met.

### gcd

    public static int gcd(int u,
                          int v)

  Gets the greatest common divisor of the absolute value of two numbers, using the "binary gcd" method which avoids division and modulo operations. See Knuth 4.5.2 algorithm B. This algorithm is due to Josef Stein (1961).

  Parameters:
    `u` - a non-zero number
    `v` - a non-zero number
  Returns:
    the greatest common divisor, never zero
  Since:
    1.1

### hash

    public static int hash(double value)

  Returns an integer hash code representing the given double value.

  Parameters:
    `value` - the value to be hashed
  Returns:
    the hash code

### indicator

    public static byte indicator(byte x)

  For a byte value x, this method returns (byte)(+1) if x >= 0 and (byte)(-1) if x < 0.

  Parameters:
    `x` - the value, a byte
  Returns:
    (byte)(+1) or (byte)(-1), depending on the sign of x

### indicator

    public static double indicator(double x)

  For a double precision value x, this method returns +1.0 if x >= 0 and -1.0 if x < 0. Returns `NaN` if `x` is `NaN`.

  Parameters:
    `x` - the value, a double
  Returns:
    +1.0 or -1.0, depending on the sign of x

### indicator

    public static float indicator(float x)

  For a float value x, this method returns +1.0F if x >= 0 and -1.0F if x < 0. Returns `NaN` if `x` is `NaN`.

  Parameters:
    `x` - the value, a float
  Returns:
    +1.0F or -1.0F, depending on the sign of x

### indicator

    public static int indicator(int x)

  For an int value x, this method returns +1 if x >= 0 and -1 if x < 0.

  Parameters:
    `x` - the value, an int
  Returns:
    +1 or -1, depending on the sign of x

### indicator

    public static long indicator(long x)

  For a long value x, this method returns +1L if x >= 0 and -1L if x < 0.

  Parameters:
    `x` - the value, a long
  Returns:
    +1L or -1L, depending on the sign of x

### indicator

    public static short indicator(short x)

  For a short value x, this method returns (short)(+1) if x >= 0 and (short)(-1) if x < 0.

  Parameters:
    `x` - the value, a short
  Returns:
    (short)(+1) or (short)(-1), depending on the sign of x

### lcm

    public static int lcm(int a,
                          int b)

  Returns the least common multiple between two integer values.

  Parameters:
    `a` - the first integer value.
    `b` - the second integer value.
  Returns:
    the least common multiple between a and b.
  Throws:
    `ArithmeticException` - if the lcm is too large to store as an int
  Since:
    1.1

### mulAndCheck

    public static int mulAndCheck(int x,
                                  int y)

  Multiply two integers, checking for overflow.

  Parameters:
    `x` - a factor
    `y` - a factor
  Returns:
    the product `x*y`
  Throws:
    `ArithmeticException` - if the result can not be represented as an int
  Since:
    1.1

### round

    public static double round(double x,
                               int scale)

  Round the given value to the specified number of decimal places. The value is rounded using the `BigDecimal.ROUND_HALF_UP` method.

  Parameters:
    `x` - the value to round.
    `scale` - the number of digits to the right of the decimal point.
  Returns:
    the rounded value.
  Since:
    1.1

### round

    public static double round(double x,
                               int scale,
                               int roundingMethod)

  Round the given value to the specified number of decimal places. The value is rounded using the given method which is any method defined in `BigDecimal`.

  Parameters:
    `x` - the value to round.
    `scale` - the number of digits to the right of the decimal point.
    `roundingMethod` - the rounding method as defined in `BigDecimal`.
  Returns:
    the rounded value.
  Since:
    1.1

### round

    public static float round(float x,
                              int scale)

  Round the given value to the specified number of decimal places. The value is rounding using the `BigDecimal.ROUND_HALF_UP` method.

  Parameters:
    `x` - the value to round.
    `scale` - the number of digits to the right of the decimal point.
  Returns:
    the rounded value.
  Since:
    1.1

### round

    public static float round(float x,
                              int scale,
                              int roundingMethod)

  Round the given value to the specified number of decimal places. The value is rounded using the given method which is any method defined in `BigDecimal`.

  Parameters:
    `x` - the value to round.
    `scale` - the number of digits to the right of the decimal point.
    `roundingMethod` - the rounding method as defined in `BigDecimal`.
  Returns:
    the rounded value.
  Since:
    1.1

### sign

    public static byte sign(byte x)

  Returns the sign for byte value `x`.

  For a byte value x, this method returns (byte)(+1) if x > 0, (byte)(0) if x = 0, and (byte)(-1) if x < 0.

  Parameters:
    `x` - the value, a byte
  Returns:
    (byte)(+1), (byte)(0), or (byte)(-1), depending on the sign of x

### sign

    public static double sign(double x)

  Returns the sign for double precision `x`.

  For a double value `x`, this method returns `+1.0` if `x > 0`, `0.0` if `x = 0.0`, and `-1.0` if `x < 0`. Returns `NaN` if `x` is `NaN`.

  Parameters:
    `x` - the value, a double
  Returns:
    +1.0, 0.0, or -1.0, depending on the sign of x

### sign

    public static float sign(float x)

  Returns the sign for float value `x`.

  For a float value x, this method returns +1.0F if x > 0, 0.0F if x = 0.0F, and -1.0F if x < 0. Returns `NaN` if `x` is `NaN`.

  Parameters:
    `x` - the value, a float
  Returns:
    +1.0F, 0.0F, or -1.0F, depending on the sign of x

### sign

    public static int sign(int x)

  Returns the sign for int value `x`.

  For an int value x, this method returns +1 if x > 0, 0 if x = 0, and -1 if x < 0.

  Parameters:
    `x` - the value, an int
  Returns:
    +1, 0, or -1, depending on the sign of x

### sign

    public static long sign(long x)

  Returns the sign for long value `x`.

  For a long value x, this method returns +1L if x > 0, 0L if x = 0, and -1L if x < 0.

  Parameters:
    `x` - the value, a long
  Returns:
    +1L, 0L, or -1L, depending on the sign of x

### sign

    public static short sign(short x)

  Returns the sign for short value `x`.

  For a short value x, this method returns (short)(+1) if x > 0, (short)(0) if x = 0, and (short)(-1) if x < 0.

  Parameters:
    `x` - the value, a short
  Returns:
    (short)(+1), (short)(0), or (short)(-1), depending on the sign of x

### sinh

    public static double sinh(double x)

  Returns the hyperbolic sine of x.

  Parameters:
    `x` - double value for which to find the hyperbolic sine
  Returns:
    hyperbolic sine of x

### subAndCheck

    public static int subAndCheck(int x,
                                  int y)

  Subtract two integers, checking for overflow.

  Parameters:
    `x` - the minuend
    `y` - the subtrahend
  Returns:
    the difference `x-y`
  Throws:
    `ArithmeticException` - if the result can not be represented as an int
  Since:
    1.1

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2003-2005 The Apache Software Foundation. All Rights Reserved.