# Class Maths

java.lang.Object
org.uncommons.maths.Maths

public final class Maths extends Object
Maths operations not provided by `java.lang.Math`.

## Method Summary

Modifier and Type
Method
Description
`static boolean`
`approxEquals(double value1, double value2, double tolerance)`
Checks that two values are approximately equal (plus or minus a specified tolerance).
`static BigInteger`
`bigFactorial(int n)`
Calculates the factorial of n where n is a positive integer.
`static long`
`factorial(int n)`
Calculates the factorial of n where n is a number in the range 0 - 20.
`static long`
`greatestCommonDivisor(long a, long b)`
Determines the greatest common divisor of a pair of natural numbers using the Euclidean algorithm.
`static double`
`log(double base, double arg)`
Calculate logarithms for arbitrary bases.
`static long`
`raiseToPower(int value, int power)`
Calculate the first argument raised to the power of the second.
`static double`
`restrictRange(double value, double min, double max)`
If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
`static int`
`restrictRange(int value, int min, int max)`
If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.
`static long`
`restrictRange(long value, long min, long max)`
If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.

### Methods inherited from class Object

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

## Method Details

### factorial

public static long factorial(int n)
Calculates the factorial of n where n is a number in the range 0 - 20. Zero factorial is equal to 1. For values of n greater than 20 you must use `bigFactorial(int)`.

Parameters:
  `n` - The factorial to calculate.
Returns:
  The factorial of n.
See Also:
  - `bigFactorial(int)`

### bigFactorial

public static BigInteger bigFactorial(int n)
Calculates the factorial of n where n is a positive integer. Zero factorial is equal to 1. For values of n up to 20, consider using `factorial(int)` instead since it uses a faster implementation.

Parameters:
  `n` - The factorial to calculate.
Returns:
  The factorial of n.
See Also:
  - `factorial(int)`

### raiseToPower

public static long raiseToPower(int value, int power)
Calculate the first argument raised to the power of the second. This method only supports non-negative powers.

Parameters:
  `value` - The number to be raised.
  `power` - The exponent (must be positive).
Returns:
  `value` raised to `power`.

### log

public static double log(double base, double arg)
Calculate logarithms for arbitrary bases.

Parameters:
  `base` - The base for the logarithm.
  `arg` - The value to calculate the logarithm for.
Returns:
  The log of `arg` in the specified `base`.

### approxEquals

public static boolean approxEquals(double value1, double value2, double tolerance)
Checks that two values are approximately equal (plus or minus a specified tolerance).

Parameters:
  `value1` - The first value to compare.
  `value2` - The second value to compare.
  `tolerance` - How much (in percentage terms, as a percentage of the first value) the values are allowed to differ and still be considered equal. Expressed as a value between 0 and 1.
Returns:
  true if the values are approximately equal, false otherwise.

### restrictRange

public static int restrictRange(int value, int min, int max)
If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.

Parameters:
  `value` - The value to check.
  `min` - The minimum permitted value.
  `max` - The maximum permitted value.
Returns:
  `value` if it is between the specified limits, `min` if the value is too low, or `max` if the value is too high.
Since:
  1.2

### restrictRange

public static long restrictRange(long value, long min, long max)
If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.

Parameters:
  `value` - The value to check.
  `min` - The minimum permitted value.
  `max` - The maximum permitted value.
Returns:
  `value` if it is between the specified limits, `min` if the value is too low, or `max` if the value is too high.
Since:
  1.2

### restrictRange

public static double restrictRange(double value, double min, double max)
If the specified value is not greater than or equal to the specified minimum and less than or equal to the specified maximum, adjust it so that it is.

Parameters:
  `value` - The value to check.
  `min` - The minimum permitted value.
  `max` - The maximum permitted value.
Returns:
  `value` if it is between the specified limits, `min` if the value is too low, or `max` if the value is too high.
Since:
  1.2

### greatestCommonDivisor

public static long greatestCommonDivisor(long a, long b)
Determines the greatest common divisor of a pair of natural numbers using the Euclidean algorithm. This method only works with natural numbers. If negative integers are passed in, the absolute values will be used. The return value is always positive.

Parameters:
  `a` - The first value.
  `b` - The second value.
Returns:
  The greatest common divisor.
Since:
  1.2