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 booleanapproxEquals(double value1, double value2, double tolerance)Checks that two values are approximately equal (plus or minus a specified tolerance).static BigIntegerbigFactorial(int n)Calculates the factorial of n where n is a positive integer.static doubleclamp(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 floatclamp(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 intclamp(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 longclamp(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 longfactorial(int n)Calculates the factorial of n where n is a number in the range 0 - 20.static longgreatestCommonDivisor(long a, long b)Determines the greatest common divisor of a pair of natural numbers using the Euclidean algorithm.static doublelog(double base, double arg)Calculate logarithms for arbitrary bases.static intmodularMultiplicativeInverse(int a)Given any odd inta, this finds another odd intbsuch thata * b == 1.static longmodularMultiplicativeInverse(long a)Given any odd longa, this finds another odd longbsuch thata * b == 1L.static longraiseToPower(int value, int power)Calculate the first argument raised to the power of the second.static doubleremainder(double op, double d)Like the modulo operator%, but the result will always match the sign ofdinstead 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:
valueraised 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
argin 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:
valueif it is between the specified limits,minif the value is too low, ormaxif 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:
valueif it is between the specified limits,minif the value is too low, ormaxif 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:
valueif it is between the specified limits,minif the value is too low, ormaxif 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:
valueif it is between the specified limits,minif the value is too low, ormaxif the value is too high.
-
remainder
Like the modulo operator%, but the result will always match the sign ofdinstead 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 intbsuch 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
amodulo 4294967296 (or, 2 to the 32)
-
modularMultiplicativeInverse
Given any odd longa, this finds another odd longbsuch 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
amodulo 18446744073709551616 (or, 2 to the 64)
-