Class XMath
public final class XMath extends Object
-
Method Summary
Modifier and Type Method Description static longaddCapped(long l1, long l2)static doubleavg(double... values)static floatavg(float... values)static intavg(int... values)static longavg(long... values)static BigDecimalbigDec(double value)Alias forBigDecimal.valueOf(value)static BigDecimalbigDec(long value)Alias forBigDecimal.valueOf(value)static BigIntegerbigInt(int value)Alias forBigInteger.valueOf(value)static BigIntegerbigInt(long value)Alias forBigInteger.valueOf(value)static intcap_int(long value)static double[]column(int columnIndex, double[]... matrix)static doublecolumnSum(int columnIndex, double[]... matrix)static doublecolumnSum(int columnIndex, Double[]... matrix)static doublecube(double d)static floatcube(float f)static intcube(int i)static longcube(long l)static longequal(long value1, long value2)static inteven(int value)static longeven(long value)static intfactorial(int n)Usefactorial(long)for n in [0;20].
Usefactorial(BigInteger)for any n greater than 0.static longfactorial(long n)Usefactorial(BigInteger)for any n greater than 0.static BigIntegerfactorial(BigInteger n)static doublefractionToPercent(double decimalFractionValue)static inthighestPowerOf2_int()static booleanisGreaterThanHighestPowerOf2(int value)static booleanisGreaterThanHighestPowerOf2(long value)static booleanisGreaterThanOrEqualHighestPowerOf2(int value)static booleanisGreaterThanOrEqualHighestPowerOf2(long value)static booleanisIn(int value, int... searchValues)static booleanisMathematicalInteger(double value)static booleanisPow2(int value)Determines if the passed value is a power-of-2 value.static intlog10discrete(int value)Returns the integer value of base-10 logarithm of the passed value.static intlog2pow2(int pow2Value)static doublelowerRatio(double a, double b)Returns abs(d1/d2) for abs(d1) lower than abs(d2), else abs(d2/d1) in order to guarantee a codomain of [0.0;1.0]static doublemax(double... values)static floatmax(float... values)static intmax(int... values)static longmax(long... values)static doublemin(double... values)static floatmin(float... values)static intmin(int... values)static longmin(long... values)static <E> doublemin_double(Iterable<E> elements, To_double<? super E> getter)static <E> doublemin_double(Iterable<E> elements, To_double<? super E> getter, double defaultValue)static <E> DoubleminDouble(Iterable<E> elements, To_double<? super E> getter)static intnegative(int value)static longnegative(long value)static doublenotNegative(double value)static intnotNegative(int value)static longnotNegative(long value)static IntegernotNegative(Integer value)static LongnotNegative(Long value)static doublenotNegativeMax1(double value)static intodd(int value)static longodd(long value)static Doubleone()static doublepercentToFraction(double decimalPercentValue)static doublepositive(double value)static floatpositive(float value)static intpositive(int value)static longpositive(long value)static doublepositiveMax1(double value)static doublepow(double base, int exponent)static floatpow(float base, int exponent)static intpow(int base, int exponent)This method is an int version ofMath.pow(double, double), using only integer iteration for calculation.static intpow10(int exponent)static doublepow10_double(int exponent)static longpow10_long(int exponent)static intpow2Bound(int n)static intpow2BoundCapped(int n)static intpow2BoundMaxed(int n)static Randomrandom()static intrandom(int n)static int[]randoming(int length)static int[]randoming(int length, int bound)static int[]randoming(int length, int lowestValue, int bound)static _longRangerange(int start, int bound)static doubleround(double value, int decimals)Roundsvalueto the actual closest value fordecimalsdecimals.
This is useful as well in order to "normalize" values if multiple subsequent calulations with double values accumulate rounding errors that drift the value away from the value it actually should (could) be.
See the "candy" example in Joshua Bloch's "Effective Java": this method fixes the problem.static doubleround0(double value)static doubleround1(double value)Common rounding variant for 1 decimal.static doubleround2(double value)Common rounding variant for 2 decimals.static doubleround3(double value)Common rounding variant for 3 decimals.static doubleround4(double value)Common rounding variant for 4 decimals.static doubleround5(double value)Common rounding variant for 5 decimals.static doubleround6(double value)Common rounding variant for 6 decimals.static doubleround7(double value)Common rounding variant for 7 decimals.static doubleround8(double value)Common rounding variant for 8 decimals.static doubleround9(double value)Common rounding variant for 9 decimals.static byte[]sequence(byte from, byte to)static int[]sequence(int lastValue)static int[]sequence(int from, int to)static long[]sequence(long from, long to)static short[]sequence(short from, short to)static Integer[]sequence(Integer lastValue)static doublesquare(double d)static floatsquare(float f)static intsquare(int i)static longsquare(long l)static intstringLength(int value)Returns the amount of digits the passed values requires to be projected as a string.static longsum(byte... values)static doublesum(double... values)static doublesum(float... values)static longsum(int... values)static longsum(long... values)static longsum(short... values)static Doublezero()
-
Method Details
-
zero
-
one
-
pow
This method is an int version ofMath.pow(double, double), using only integer iteration for calculation.As a rule of thumb:
It is faster forexponentlower than 250 (significantly faster for exponents lt 100) and slower forexponentgreater than or equal 250 (significantly slower for exponents gt/e 500).
This may depend on the concrete system running the program, of course.
Note thatexponentmay not be negative, otherwise anIllegalArgumentExceptionis thrown.- Parameters:
base-exponent- my not be negative- Returns:
base^exponent- Throws:
IllegalArgumentException- ifexponentis negative
-
pow10
- Throws:
IllegalArgumentException
-
pow10_double
- Throws:
IllegalArgumentException
-
pow10_long
- Throws:
IllegalArgumentException
-
pow2BoundMaxed
public static final int pow2BoundMaxed(int n) -
pow2BoundCapped
public static final int pow2BoundCapped(int n) -
pow2Bound
public static final int pow2Bound(int n) -
log2pow2
public static final int log2pow2(int pow2Value) -
isPow2
public static final boolean isPow2(int value)Determines if the passed value is a power-of-2 value.- Parameters:
value- the value to be tested.- Returns:
truefor any n in [0;30] that satisfiesvalue = 2^n.
-
log10discrete
Returns the integer value of base-10 logarithm of the passed value.Examples:
Note that passing a value lower than or equal 0 will throw anlog10(1) = 0 log10(8) = 0 log10(10) = 1 log10(99) = 1 log10(100) = 2 log10(1000000000) = 9 log10(2147483647) = 9
IllegalArgumentException.- Parameters:
value-- Throws:
IllegalArgumentException
-
stringLength
public static final int stringLength(int value)Returns the amount of digits the passed values requires to be projected as a string.The additional length for the minus of to represent negative values is acounted for as well.
Examples:stringLength(0) = 1 stringLength(+6) = 1 stringLength(10) = 2 stringLength(+2147483647) = 10 stringLength(-1) = 2 stringLength(-2147483648) = 11
- Parameters:
value- theintvalue whose string length shall be calculated- Returns:
- the length the passed value's string representation will require.
-
pow
- Throws:
IllegalArgumentException
-
pow
- Throws:
IllegalArgumentException
-
square
public static final float square(float f) -
square
public static final long square(long l) -
square
public static final int square(int i) -
square
public static final double square(double d) -
cube
public static final float cube(float f) -
cube
public static final long cube(long l) -
cube
public static final int cube(int i) -
cube
public static final double cube(double d) -
round0
public static final double round0(double value) -
round1
public static final double round1(double value)Common rounding variant for 1 decimal.- Parameters:
value- the decimal value to be rounded.- Returns:
- the passed value rounded to 1 decimal.
-
round2
public static final double round2(double value)Common rounding variant for 2 decimals.- Parameters:
value- the decimal value to be rounded.- Returns:
- the passed value rounded to 2 decimals.
-
round3
public static final double round3(double value)Common rounding variant for 3 decimals.- Parameters:
value- the decimal value to be rounded.- Returns:
- the passed value rounded to 3 decimals.
-
round4
public static final double round4(double value)Common rounding variant for 4 decimals.- Parameters:
value- the decimal value to be rounded.- Returns:
- the passed value rounded to 4 decimals.
-
round5
public static final double round5(double value)Common rounding variant for 5 decimals.- Parameters:
value- the decimal value to be rounded.- Returns:
- the passed value rounded to 5 decimals.
-
round6
public static final double round6(double value)Common rounding variant for 6 decimals.- Parameters:
value- the decimal value to be rounded.- Returns:
- the passed value rounded to 6 decimals.
-
round7
public static final double round7(double value)Common rounding variant for 7 decimals.- Parameters:
value- the decimal value to be rounded.- Returns:
- the passed value rounded to 7 decimals.
-
round8
public static final double round8(double value)Common rounding variant for 8 decimals.- Parameters:
value- the decimal value to be rounded.- Returns:
- the passed value rounded to 8 decimals.
-
round9
public static final double round9(double value)Common rounding variant for 9 decimals.- Parameters:
value- the decimal value to be rounded.- Returns:
- the passed value rounded to 9 decimals.
-
round
public static final double round(double value, int decimals)Roundsvalueto the actual closest value fordecimalsdecimals.
This is useful as well in order to "normalize" values if multiple subsequent calulations with double values accumulate rounding errors that drift the value away from the value it actually should (could) be.
See the "candy" example in Joshua Bloch's "Effective Java": this method fixes the problem.Note that
decimalsmay not be negative.
Also note that while a value of 0 fordecimalswill yield the correct result, it makes not much sense to call this method for it in the first place.- Parameters:
value- any double valuedecimals- the number of decimals. May not be negative.- Returns:
- the normalized value for
value
-
range
-
sequence
public static byte[] sequence(byte from, byte to) -
sequence
public static short[] sequence(short from, short to) -
sequence
public static int[] sequence(int lastValue) -
randoming
public static int[] randoming(int length) -
randoming
public static int[] randoming(int length, int bound) -
randoming
public static int[] randoming(int length, int lowestValue, int bound) -
sequence
-
sequence
public static int[] sequence(int from, int to) -
sequence
- Throws:
IllegalArgumentException
-
max
public static final double max(double... values) -
max
public static final float max(float... values) -
max
public static final int max(int... values) -
max
public static final long max(long... values) -
min
public static final double min(double... values) -
min
public static final float min(float... values) -
min
public static final int min(int... values) -
min
public static final long min(long... values) -
sum
public static final long sum(byte... values) -
sum
public static final long sum(short... values) -
sum
public static final long sum(int... values) -
sum
public static final double sum(float... values) -
sum
public static final long sum(long... values) -
sum
public static final double sum(double... values) -
columnSum
public static final double columnSum(int columnIndex, double[]... matrix) -
columnSum
-
avg
public static final double avg(double... values) -
avg
public static final float avg(float... values) -
avg
public static final int avg(int... values) -
avg
public static final long avg(long... values) -
lowerRatio
public static final double lowerRatio(double a, double b)Returns abs(d1/d2) for abs(d1) lower than abs(d2), else abs(d2/d1) in order to guarantee a codomain of [0.0;1.0]- Parameters:
a- the first valueb- the second value- Returns:
- the lower ratio of d1 and d2
-
factorial
Usefactorial(long)for n in [0;20].
Usefactorial(BigInteger)for any n greater than 0.- Parameters:
n- natural number in [0;12]- Returns:
- n!
- Throws:
IllegalArgumentException- for n lower than 0 or n greater than 12.
-
factorial
Usefactorial(BigInteger)for any n greater than 0.- Parameters:
n- natural number in [0;20]- Returns:
- n!
- Throws:
IllegalArgumentException- for n lower than 0 or n greater than 20
-
factorial
- Parameters:
n- any natural number greater than or equal 0- Returns:
- n!
- Throws:
IllegalArgumentException- for n lower than 0
-
bigInt
Alias forBigInteger.valueOf(value)- Parameters:
value- any value- Returns:
- a
BigIntegerrepresentingvalue
-
bigInt
Alias forBigInteger.valueOf(value)- Parameters:
value- any value- Returns:
- a
BigIntegerrepresentingvalue
-
bigDec
Alias forBigDecimal.valueOf(value)- Parameters:
value- any value- Returns:
- a
BigDecimalrepresentingvalue
-
bigDec
Alias forBigDecimal.valueOf(value)- Parameters:
value- any value- Returns:
- a
BigDecimalrepresentingvalue
-
random
- Returns:
- the random
-
random
public static final int random(int n) -
even
public static int even(int value) -
even
public static long even(long value) -
odd
public static int odd(int value) -
odd
public static long odd(long value) -
positive
- Throws:
NumberRangeException
-
notNegative
- Throws:
NumberRangeException
-
notNegative
- Throws:
NumberRangeException
-
negative
- Throws:
NumberRangeException
-
positive
- Throws:
NumberRangeException
-
notNegative
- Throws:
NumberRangeException
-
notNegative
- Throws:
NumberRangeException
-
negative
- Throws:
NumberRangeException
-
positive
- Throws:
NumberRangeException
-
notNegative
- Throws:
NumberRangeException
-
positiveMax1
- Throws:
NumberRangeException
-
notNegativeMax1
- Throws:
NumberRangeException
-
positive
- Throws:
NumberRangeException
-
equal
- Throws:
IllegalArgumentException
-
column
public static double[] column(int columnIndex, double[]... matrix) -
cap_int
public static final int cap_int(long value) -
isGreaterThanOrEqualHighestPowerOf2
public static final boolean isGreaterThanOrEqualHighestPowerOf2(long value) -
isGreaterThanHighestPowerOf2
public static final boolean isGreaterThanHighestPowerOf2(long value) -
isGreaterThanOrEqualHighestPowerOf2
public static final boolean isGreaterThanOrEqualHighestPowerOf2(int value) -
isGreaterThanHighestPowerOf2
public static final boolean isGreaterThanHighestPowerOf2(int value) -
highestPowerOf2_int
public static final int highestPowerOf2_int() -
fractionToPercent
public static final double fractionToPercent(double decimalFractionValue) -
percentToFraction
public static final double percentToFraction(double decimalPercentValue) -
isIn
public static final boolean isIn(int value, int... searchValues) -
addCapped
public static long addCapped(long l1, long l2) -
minDouble
-
min_double
-
min_double
-
isMathematicalInteger
public static boolean isMathematicalInteger(double value)
-