Package squidpony.squidmath
Class MathExtras
java.lang.Object
squidpony.squidmath.MathExtras
public final class MathExtras extends Object
Mathematical operations not provided by
Originally part of the Uncommon Maths software package as Maths.
java.lang.Math
.
Originally part of the Uncommon Maths software package as Maths.
- Author:
- Daniel Dyer
-
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 double
clamp(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 float
clamp(float value, float min, float 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
clamp(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
clamp(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.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 int
modularMultiplicativeInverse(int a)
Given any odd inta
, this finds another odd intb
such thata * b == 1
.static long
modularMultiplicativeInverse(long a)
Given any odd longa
, this finds another odd longb
such thata * b == 1L
.static long
raiseToPower(int value, int power)
Calculate the first argument raised to the power of the second.static double
remainder(double op, double d)
Like the modulo operator%
, but the result will always match the sign ofd
instead ofop
.
-
Method Details
-
factorial
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(int)
-
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:
factorial(int)
-
raiseToPower
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
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
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.
-
clamp
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.
-
clamp
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.
-
clamp
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.
-
clamp
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.
-
remainder
Like the modulo operator%
, but the result will always match the sign ofd
instead ofop
.- Parameters:
op
- the dividend; negative values are permitted and wrap instead of producing negative resultsd
- the divisor; if this is negative then the result will be negative, otherwise it will be positive- Returns:
- the remainder of the division of op by d, with a sign matching d
-
greatestCommonDivisor
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.
-
modularMultiplicativeInverse
Given any odd inta
, this finds another odd intb
such thata * b == 1
.
This is incompatible with GWT, but it should usually only find uses in exploratory code or in tests anyway... It is only incompatible because it tends to rely on multiplication overflow to work.- Parameters:
a
- any odd int; note that even numbers do not have inverses modulo 2 to the 32- Returns:
- the multiplicative inverse of
a
modulo 4294967296 (or, 2 to the 32)
-
modularMultiplicativeInverse
Given any odd longa
, this finds another odd longb
such thata * b == 1L
.- Parameters:
a
- any odd long; note that even numbers do not have inverses modulo 2 to the 64- Returns:
- the multiplicative inverse of
a
modulo 18446744073709551616 (or, 2 to the 64)
-