## Class MathUtils

• ```public final class MathUtils
extends Object```
various mathematics utilities. The functions do not exist in the basic math package Math.*
Since:
8 dec. 2008 version 2.0.1
Version:
2.0.1
Author:
Charles Prud'homme, Arnaud Malapert
• ### Field Summary

Fields
Modifier and Type Field Description
`static double` `ROUNDED_LOG_PRECISION`
Precision for rounded logarithm.
• ### Method Summary

All Methods
Modifier and Type Method Description
`static int` ```combinaison​(int n, int p)```
it computes the number of combinaison C_n^p.
`static int` ```divCeil​(int x, int y)```
Returns the smallest (closest to positive infinity) `int` value that is greater or equal to a/b.
`static int` ```divFloor​(int x, int y)```
Returns the largest (closest to positive infinity) `int` value that is less than or equal to a/b.
`static long` `factorial​(int n)`
simple recursive version of factorial
`static boolean` `isPowerOfTwo​(int x)`
`static double` ```log​(double value, double exponent)```
`static int` `max​(int[] values)`
Returns the element with the greatest value in values.
`static int` `max​(int[][] values)`
Returns the element with the greatest value in values.
`static int` `min​(int[] values)`
Returns the element with the smallest value in values.
`static int` `min​(int[][] values)`
Returns the element with the smallest value in values.
`static int` ```pow​(int value, int exp)```
Returns the value of the first argument raised to the power of the second argument.
`static double` ```roundedLog​(double value, double exponent)```
a rounded logarithm to avoid issues with jvm dependant math functions
`static int` ```safeAdd​(int x, int y)```
Returns the sum of its arguments, returning either `Integer.MAX_VALUE` if the result overflows an `int`, or `Integer.MIN_VALUE` if the result underflows an `int`, .
`static int` `safeCast​(long x)`
`static int` ```safeMultiply​(int x, int y)```
Returns the product of its arguments, returning either `Integer.MAX_VALUE` if the result overflows an `int`, or `Integer.MIN_VALUE` if the result underflows an `int`, .
`static int` ```safeSubstract​(int x, int y)```
Returns the difference of its arguments, returning either `Integer.MAX_VALUE` if the result overflows an `int`, or `Integer.MIN_VALUE` if the result underflows an `int`, .
`static int` `sum​(int[] values)`
Returns the sum of elements in values.
`static int` `sum​(int[][] values)`
Retuns the sum of elements in values.
`static int` ```sum​(int[] values, int begin, int end)```
Returns the sum of elements in values from position begin (inclusive) to position end (exclusive).
`static int` ```sumFrom​(int[] values, int begin)```
Returns the sum of elements in values from position begin (inclusive) to values.length.
`static int` ```sumTo​(int[] values, int end)```
Returns the sum of elements in values from position 0 (inclusive) to position end (exclusive).
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Field Detail

• #### ROUNDED_LOG_PRECISION

`public static final double ROUNDED_LOG_PRECISION`
Precision for rounded logarithm.
Constant Field Values
• ### Method Detail

• #### factorial

`public static long factorial​(int n)`
simple recursive version of factorial
Parameters:
`n` - size of the suite
Returns:
n!
• #### combinaison

```public static int combinaison​(int n,
int p)```
it computes the number of combinaison C_n^p. The function is only recursive and do not use an array to store temporary results
Parameters:
`n` - max cardinality
`p` - sub cardinality
Returns:
n among k combinations
• #### isPowerOfTwo

`public static boolean isPowerOfTwo​(int x)`
Parameters:
`x` - a value
Returns:
true if x is power of 2.
• #### pow

```public static int pow​(int value,
int exp)```
Returns the value of the first argument raised to the power of the second argument. See `Math.pow(double, double)` for special cases.
Parameters:
`value` -
`exp` -
Returns:
• #### log

```public static double log​(double value,
double exponent)```
• #### roundedLog

```public static double roundedLog​(double value,
double exponent)```
a rounded logarithm to avoid issues with jvm dependant math functions
• #### sum

```public static int sum​(int[] values,
int begin,
int end)```
Returns the sum of elements in values from position begin (inclusive) to position end (exclusive).
Parameters:
`values` - array of ints
`begin` - starting position (inclusive)
`end` - ending position (exclusive)
Returns:
the sum of elements in values from position begin (inclusive) to position end (exclusive).
• #### sumFrom

```public static int sumFrom​(int[] values,
int begin)```
Returns the sum of elements in values from position begin (inclusive) to values.length.
Parameters:
`values` - array of ints
`begin` - starting position (inclusive)
Returns:
the sum of elements in values from position begin (inclusive) to values.length.
• #### sumTo

```public static int sumTo​(int[] values,
int end)```
Returns the sum of elements in values from position 0 (inclusive) to position end (exclusive).
Parameters:
`values` - array of ints
`end` - ending position (exclusive)
Returns:
the sum of elements in values from position 0 (inclusive) to position end (exclusive).
• #### sum

`public static int sum​(int[] values)`
Returns the sum of elements in values.
Parameters:
`values` - array of ints
Returns:
the sum of elements in values.
• #### sum

`public static int sum​(int[][] values)`
Retuns the sum of elements in values.
Parameters:
`values` - matrix of ints
Returns:
the sum of elements in values.
• #### max

`public static int max​(int[] values)`
Returns the element with the greatest value in values.
Parameters:
`values` - array of ints
Returns:
the element with the greatest value in values.
• #### max

`public static int max​(int[][] values)`
Returns the element with the greatest value in values.
Parameters:
`values` - array of ints
Returns:
the element with the greatest value in values.
• #### min

`public static int min​(int[] values)`
Returns the element with the smallest value in values.
Parameters:
`values` - array of ints
Returns:
the element with the smallest value in values.
• #### min

`public static int min​(int[][] values)`
Returns the element with the smallest value in values.
Parameters:
`values` - array of ints
Returns:
the element with the smallest value in values.
• #### divFloor

```public static int divFloor​(int x,
int y)```
Returns the largest (closest to positive infinity) `int` value that is less than or equal to a/b. Adapted from `Math.floorDiv(int, int)`.
Parameters:
`x` - the dividend
`y` - the divisor
Returns:
the largest (closest to positive infinity) `int` value that is less than or equal to a/b.
• #### divCeil

```public static int divCeil​(int x,
int y)```
Returns the smallest (closest to positive infinity) `int` value that is greater or equal to a/b. Adapted from `Math.floorDiv(int, int)`.
Parameters:
`x` - the dividend
`y` - the divisor
Returns:
the smallest (closest to positive infinity) `int` value that is greater or equal to a/b.

```public static int safeAdd​(int x,
int y)```
Returns the sum of its arguments, returning either `Integer.MAX_VALUE` if the result overflows an `int`, or `Integer.MIN_VALUE` if the result underflows an `int`, .
Parameters:
`x` - the first value
`y` - the second value
Returns:
the result
• #### safeSubstract

```public static int safeSubstract​(int x,
int y)```
Returns the difference of its arguments, returning either `Integer.MAX_VALUE` if the result overflows an `int`, or `Integer.MIN_VALUE` if the result underflows an `int`, .
Parameters:
`x` - the first value
`y` - the second value
Returns:
the result
• #### safeMultiply

```public static int safeMultiply​(int x,
int y)```
Returns the product of its arguments, returning either `Integer.MAX_VALUE` if the result overflows an `int`, or `Integer.MIN_VALUE` if the result underflows an `int`, .
Parameters:
`x` - the first value
`y` - the second value
Returns:
the result
• #### safeCast

`public static int safeCast​(long x)`
Parameters:
`x` - long to cast
Returns:
the closest int value when safe casting a long into an int