Class XMath
public final class XMath extends Object
-
Constructor Summary
Constructors Constructor Description XMath()
-
Method Summary
Modifier and Type Method Description static long
addCapped(long l1, long l2)
static double
avg(double... values)
static float
avg(float... values)
static int
avg(int... values)
static long
avg(long... values)
static BigDecimal
bigDec(double value)
Alias forBigDecimal.valueOf(value)
static BigDecimal
bigDec(long value)
Alias forBigDecimal.valueOf(value)
static BigInteger
bigInt(int value)
Alias forBigInteger.valueOf(value)
static BigInteger
bigInt(long value)
Alias forBigInteger.valueOf(value)
static int
cap_int(long value)
static double[]
column(int columnIndex, double[]... matrix)
static double
columnSum(int columnIndex, double[]... matrix)
static double
columnSum(int columnIndex, Double[]... matrix)
static double
cube(double d)
static float
cube(float f)
static int
cube(int i)
static long
cube(long l)
static long
equal(long value1, long value2)
static int
even(int value)
static long
even(long value)
static int
factorial(int n)
Usefactorial(long)
for n in [0;20].
Usefactorial(BigInteger)
for any n greater than 0.static long
factorial(long n)
Usefactorial(BigInteger)
for any n greater than 0.static BigInteger
factorial(BigInteger n)
static double
fractionToPercent(double decimalFractionValue)
static int
highestPowerOf2_int()
static boolean
isGreaterThanHighestPowerOf2(int value)
static boolean
isGreaterThanHighestPowerOf2(long value)
static boolean
isGreaterThanOrEqualHighestPowerOf2(int value)
static boolean
isGreaterThanOrEqualHighestPowerOf2(long value)
static boolean
isIn(int value, int... searchValues)
static boolean
isPow2(int value)
Determines if the passed value is a power-of-2 value.static int
log10discrete(int value)
Returns the integer value of base-10 logarithm of the passed value.static int
log2pow2(int pow2Value)
static 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]static double
max(double... values)
static float
max(float... values)
static int
max(int... values)
static long
max(long... values)
static double
min(double... values)
static float
min(float... values)
static int
min(int... values)
static long
min(long... values)
static <E> double
min_double(Iterable<E> elements, To_double<? super E> getter)
static <E> double
min_double(Iterable<E> elements, To_double<? super E> getter, double defaultValue)
static <E> Double
minDouble(Iterable<E> elements, To_double<? super E> getter)
static int
negative(int value)
static long
negative(long value)
static double
notNegative(double value)
static int
notNegative(int value)
static long
notNegative(long value)
static Integer
notNegative(Integer value)
static Long
notNegative(Long value)
static double
notNegativeMax1(double value)
static int
odd(int value)
static long
odd(long value)
static Double
one()
static double
percentToFraction(double decimalPercentValue)
static double
positive(double value)
static float
positive(float value)
static int
positive(int value)
static long
positive(long value)
static double
positiveMax1(double value)
static double
pow(double base, int exponent)
static float
pow(float base, int exponent)
static int
pow(int base, int exponent)
This method is an int version ofMath.pow(double, double)
, using only integer iteration for calculation.static int
pow10(int exponent)
static double
pow10_double(int exponent)
static long
pow10_long(int exponent)
static int
pow2Bound(int n)
static int
pow2BoundCapped(int n)
static int
pow2BoundMaxed(int n)
static Random
random()
static int
random(int n)
static int[]
randoming(int length)
static int[]
randoming(int length, int bound)
static int[]
randoming(int length, int lowestValue, int bound)
static _longRange
range(int start, int bound)
static double
round(double value, int decimals)
Roundsvalue
to the actual closest value fordecimals
decimals.
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 double
round0(double value)
static double
round1(double value)
Common rounding variant for 1 decimal.static double
round2(double value)
Common rounding variant for 2 decimals.static double
round3(double value)
Common rounding variant for 3 decimals.static double
round4(double value)
Common rounding variant for 4 decimals.static double
round5(double value)
Common rounding variant for 5 decimals.static double
round6(double value)
Common rounding variant for 6 decimals.static double
round7(double value)
Common rounding variant for 7 decimals.static double
round8(double value)
Common rounding variant for 8 decimals.static double
round9(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 double
square(double d)
static float
square(float f)
static int
square(int i)
static long
square(long l)
static int
stringLength(int value)
Returns the amount of digits the passed values requires to be projected as a string.static long
sum(byte... values)
static double
sum(double... values)
static double
sum(float... values)
static long
sum(int... values)
static long
sum(long... values)
static long
sum(short... values)
static Double
zero()
-
Constructor Details
-
XMath
public XMath()
-
-
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 forexponent
lower than 250 (significantly faster for exponents lt 100) and slower forexponent
greater than or equal 250 (significantly slower for exponents gt/e 500).
This may depend on the concrete system running the program, of course.
Note thatexponent
may not be negative, otherwise anIllegalArgumentException
is thrown.- Parameters:
base
-exponent
- my not be negative- Returns:
base^exponent
- Throws:
IllegalArgumentException
- ifexponent
is 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:
true
for 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
- theint
value 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)Roundsvalue
to the actual closest value fordecimals
decimals.
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
decimals
may not be negative.
Also note that while a value of 0 fordecimals
will 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
BigInteger
representingvalue
-
bigInt
Alias forBigInteger.valueOf(value)
- Parameters:
value
- any value- Returns:
- a
BigInteger
representingvalue
-
bigDec
Alias forBigDecimal.valueOf(value)
- Parameters:
value
- any value- Returns:
- a
BigDecimal
representingvalue
-
bigDec
Alias forBigDecimal.valueOf(value)
- Parameters:
value
- any value- Returns:
- a
BigDecimal
representingvalue
-
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
-