Package org.uncommons.maths
Class Maths
java.lang.Object
org.uncommons.maths.Maths
Maths operations not provided by
java.lang.Math
.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final ConcurrentMap
<Integer, BigInteger> private static final int
private static final int
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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.
-
Field Details
-
MAX_LONG_FACTORIAL
private static final int MAX_LONG_FACTORIAL- See Also:
-
CACHE_SIZE
private static final int CACHE_SIZE- See Also:
-
BIG_FACTORIALS
-
-
Constructor Details
-
Maths
private Maths()
-
-
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 usebigFactorial(int)
.- Parameters:
n
- The factorial to calculate.- Returns:
- The factorial of n.
- See Also:
-
bigFactorial
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 usingfactorial(int)
instead since it uses a faster implementation.- Parameters:
n
- The factorial to calculate.- Returns:
- The factorial of n.
- See Also:
-
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 topower
.
-
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 specifiedbase
.
-
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, ormax
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, ormax
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, ormax
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
-