public final class MathUtils extends Object
The MathUtils
class provides utility methods for mathematics and
number handling.
Modifier and Type | Method and Description |
---|---|
static double |
average(Collection<? extends Number> values)
Returns the average of the values(the arithmetic average(sum of all
values divided by the number of values)).
|
static double |
average(double... values)
Returns the average of the values(the arithmetic average(sum of all
values divided by the number of values)).
|
static double |
average(int... values)
Returns the average of the values(the arithmetic average(sum of all
values divided by the number of values)).
|
static double |
average(long... values)
Returns the average of the values(the arithmetic average(sum of all
values divided by the number of values)).
|
static double |
bcdAdd(double addend,
double augend)
Returns a
double whose value is (addend +
augend ). |
static double |
bcdDivide(double dividend,
double divisor)
|
static double |
bcdDivide(double dividend,
double divisor,
int roundingMode) |
static double |
bcdMultiply(double multiplier,
double multiplicand)
Returns a
double whose value is (multiplier x
multiplicand ). |
static double |
bcdSubtract(double minuend,
double subtrahend)
Returns a
double whose value is (minuend -
augend ). |
static void |
checkRange(int val,
int min,
int max)
* Checks if the variable
val is a value between
min and max . |
static int |
computeHash(Object... objects) |
static int |
computeHashDeep(Object... objects)
Examples.
|
static int |
getDecimals(double value,
int decimals)
Examples
|
static boolean |
isInRange(int val,
int min,
int max)
* Checks if the variable
val is a value between
min and max . |
static byte |
max(byte... numbers)
Returns the biggest of all numbers.
|
static double |
max(double... numbers)
Returns the biggest of all numbers.
|
static float |
max(float... numbers)
Returns the biggest of all numbers.
|
static int |
max(int... numbers)
Returns the biggest of all numbers.
|
static long |
max(long... numbers)
Returns the biggest of all numbers.
|
static short |
max(short... numbers)
Returns the biggest of all numbers.
|
static byte |
min(byte... numbers)
Returns the smallest of all numbers.
|
static double |
min(double... numbers)
Returns the smallest of all numbers.
|
static float |
min(float... numbers)
Returns the smallest of all numbers.
|
static int |
min(int... numbers)
Returns the smallest of all numbers.
|
static long |
min(long... numbers)
Returns the smallest of all numbers.
|
static short |
min(short... numbers)
Returns the smallest of all numbers.
|
static double |
round(double value,
int decimals)
Examples
|
static double |
sum(Collection<? extends Number> values)
Returns the sum of all values.
|
static double |
sum(double... values)
Returns the sum of all
Array values. |
static long |
sum(int... values)
Returns the sum of all
Array values. |
static long |
sum(long... values)
Returns the sum of all
Array values. |
public static double round(double value, int decimals)
MathUtils.round(1020.3044, 2); returns 1020.30 MathUtils.round(1020.3044323210, 4); returns 1020.3044 MathUtils.round(-1020.3044,2); returns -1020.30 MathUtils.round(-1020.304446023,5); returns 31020.30445
public static int getDecimals(double value, int decimals)
MathUtils.getDecimals(1020.30552, 4); returns 3055.0 MathUtils.getDecimals(1020.97018, 2); returns 97.0
public static double sum(Collection<? extends Number> values)
XdevListHintliste = new XdevList (); liste.add(200); liste.add(5.20); liste.add(20); liste.add(0); returns 225.2 XdevList liste = new XdevList (); liste.add(200); liste.add(5.20); liste.add(20); liste.add(0); liste.add(-30); returns 195.2 XdevList liste = new XdevList (); liste.add(-200); liste.add(-5.20); liste.add(-20); liste.add(-0); returns -225.2
If the single summands are already in the program (as variable, method return in a loop, etc.), then it has more performance to sum the values yourself , than put them into a list and sum it with this method.
values
- Collection of Number
s which will be summed up.double
.public static long sum(int... values)
Array
values. Examples
MathUtils.sum(new int[]{10,25,30,45}); returns 110 MathUtils.sum(new int[]{10,25,30,45,-9}); returns 101 MathUtils.sum(new int[]{-10,-25,-30,-45,-9}); returns -119
values
- Array of int
which will be sumint
sum(Collection)
,
sum(double...)
,
sum(long...)
public static long sum(long... values)
Array
values.
Examples
MathUtils.sum(new long[]{58976,58895,9975,01,55}); returns 127902 MathUtils.sum(new long[]{58976,58895,9975,01,55,-30}); returns 127872 MathUtils.sum(new long[]{-58976,-58895,-9975,-01,-55}); returns -127902
values
- Array of long
which will be sumlong
sum(Collection)
,
sum(double...)
,
sum(int...)
public static double sum(double... values)
Array
values.
Examples
MathUtils.sum(new double[]{1.50,5.20,30}); returns 36.7 MathUtils.sum(new double[]{1.50,5.20,-10.78}); returns -4.08 MathUtils.sum(new double[]{-1.50,-5.20,-10.78}); returns -17.48
values
- Array of double
which will be sumdouble
sum(Collection)
,
sum(int...)
,
sum(long...)
public static double average(Collection<? extends Number> values)
XdevListexpected = new XdevList (); expected.add(200); expected.add(5.20); expected.add(20); expected.add(0); returns 56.3 XdevList liste = new XdevList (); liste.add(200); liste.add(5.20); liste.add(20); liste.add(0); liste.add(-30); returns 39.04 XdevList liste = new XdevList (); liste.add(-200); liste.add(-5.20); liste.add(-20); liste.add(-0); returns -56.3
values
- List of Number
s which average will be calculated.values
is
empty or null
then it returns 0.0;average(double...)
,
average(int...)
,
average(long...)
public static double average(int... values)
MathUtils.average(new int[]{10,25,30,45}); returns 27.5 MathUtils.average(new int[]{10,25,30,45,-9}); returns 20.2 MathUtils.average(new int[]{-10,-25,-30,-45,-9}); returns -23.8
values
- List of int
which average will be calculated.List
values. If
values
is empty or null
then it returns
0;average(double...)
,
average(Collection)
,
average(long...)
public static double average(long... values)
MathUtils.average(new long[]{58976,58895,9975,04,55}); returns 25581.0 MathUtils.average(new long[]{28976,58895,9975,01,55,-30}); returns 16312.0 MathUtils.average(new long[]{-58976,-58895,-9975,-01,-55}); returns -25580.4
values
- List of long
which average will be calculated.List
values. If
values
is empty or null
then it returns
0;average(double...)
,
average(int...)
,
average(Collection)
public static double average(double... values)
MathUtils.average(new double[]{1.50,5.20,30}); returns 12.233333333333334 MathUtils.average(new double[]{1.50,5.20,30,-10.78}); returns 6.48 MathUtils.average(new double[]{-1.50,-5.20,-30,-10.78}); returns -11.870000000000001
values
- List of double
which average will be calculated.List
values. If
values
is empty or null
then it returns
0;average(Collection)
,
average(int...)
,
average(long...)
public static byte min(byte... numbers)
MathUtils.min(new byte[]{5,6,2,3}); returns 2 MathUtils.min(new byte[]{5,-6,2,-3}); returns -6 MathUtils.min(new byte[]{-5,-6,-2,-3}); returns -6
numbers
- List of byte
of which the minimum will be
calculated.List
numbers. If
numbers
is empty or null
then it
returns the minimum value of the data type byte
;min(byte...)
,
min(int...)
,
min(short...)
,
min(long...)
,
min(double...)
public static byte max(byte... numbers)
MathUtils.max(new byte[]{5,6,2,3}); returns 6 MathUtils.max(new byte[]{5,-6,2,-3}); returns 5 MathUtils.max(new byte[]{-5,-6,-2,-3}); returns -2
numbers
- List of byte
of which the maximum will be
calculated.List
numbers. If
numbers
is empty or null
then it
returns the maximum value of the data type byte
;max(float...)
,
max(int...)
,
max(short...)
,
max(long...)
,
max(double...)
public static short min(short... numbers)
MathUtils.min(new short[]{5,4,7,9,15,3}); returns 3 MathUtils.min(new short[]{5,-4,7,-9,15,-3}); returns -9 MathUtils.min(new short[]{-5,-4,-7,-9,-15,-3}); returns -15
numbers
- List of short
of which the minimum will be
calculated.List
numbers. If
numbers
is empty or null
then it
returns the minimum value of the data type short
;min(byte...)
,
min(int...)
,
min(float...)
,
min(long...)
,
min(double...)
public static short max(short... numbers)
MathUtils.max(new short[]{5,4,7,9,15,3}); returns 15 MathUtils.max(new short[]{5,-4,7,-9,15,-3}); returns 15 MathUtils.max(new short[]{-5,-4,-7,-9,-15,-3}); returns -3
numbers
- List of short
of which the maximum will be
calculated.List
numbers. If
numbers
is empty or null
then it
returns the maximum value of the data type short
;max(float...)
,
max(int...)
,
max(byte...)
,
max(long...)
,
max(double...)
public static int max(int... numbers)
MathUtils.max(new int[]{5,6,2,1,3}); returns 6 MathUtils.max(new int[]{5,9,15,-5,-20}); returns 15 MathUtils.max(new int[]{-5,-9,-15,-5,-20}); returns -5
numbers
- List of int
of which the maximum will be
calculated.List
numbers. If
numbers
is empty or null
then it
returns the maximum value of the data type int
;max(byte...)
,
max(short...)
,
max(float...)
,
max(long...)
,
max(double...)
public static int min(int... numbers)
ExamplesMathUtils.min(new int[]{5,6,2,1,3}); returns 1 MathUtils.min(new int[]{5,9,15,-5,-20}); returns -20 MathUtils.min(new int[]{-5,-9,-15,-5,-20}); returns -20
numbers
- List of int
of which the minimum will be
calculated.List
numbers. If
numbers
is empty or null
then it
returns the minimum value of the data type int
;min(byte...)
,
min(short...)
,
min(float...)
,
min(long...)
,
min(double...)
public static long max(long... numbers)
MathUtils.max(new long[]{8,6,3,0,14,9}); returns 14 MathUtils.max(new long[]{-8,6,-3,0,-14,9}); returns 9 MathUtils.max(new long[]{-8,-6,-3,-0,-14,-9}); returns 0Hint
As long as no absolute value over 2.000.000 exists, max(int...)
works as well.
numbers
- List of long
which maximum will be calculated.List
numbers. If
numbers
is empty or null
then it
returns the maximum value of the data type long
;max(byte...)
,
max(short...)
,
max(float...)
,
max(int...)
,
max(double...)
public static long min(long... numbers)
Examples
MathUtils.min(new long[]{8,6,3,0,14,9}); returns 0 MathUtils.min(new long[]{-8,6,-3,0,-14,9}); returns -14 MathUtils.min(new long[]{-8,-6,-3,-0,-14,-9}); returns -14Hint
As long as no absolute value over 2.000.000 exists, min(int...)
works as well.
numbers
- List of long
which minimum will be calculated.List
numbers. If
numbers
is empty or null
then it
returns the minimum value of the data type long
;min(byte...)
,
min(short...)
,
min(float...)
,
min(int...)
,
min(double...)
public static float max(float... numbers)
MathUtils.max(new float[]{5,8,10,26,3,8,19}); returns 26 MathUtils.max(new float[]{5,-8,10,-26,3,-8,19}); returns 19 MathUtils.max(new float[]{-5,-8,-10,-26,-3,-8,-19}); returns -3
numbers
- List of float
of which the maximum will be
calculated.List
numbers. If
numbers
is empty or null
then it
returns the maximum value of the data type float
;max(byte...)
,
max(int...)
,
max(short...)
,
max(long...)
,
max(double...)
public static float min(float... numbers)
MathUtils.min(new float[]{5,8,10,26,3,8,19}); returns 3 MathUtils.min(new float[]{5,-8,10,-26,3,-8,19}); returns -26 MathUtils.min(new float[]{-5,-8,-10,-26,-3,-8,-19}); returns -26
numbers
- List of float
of which the minimum will be
calculated.List
numbers. If
numbers
is empty or null
then it
returns the minimum value of the data type float
;min(byte...)
,
min(int...)
,
min(short...)
,
min(long...)
,
min(double...)
public static double max(double... numbers)
MathUtils.max(new double[]{5.5,9.8,1.5,5.2,2.9}); returns 9.8 MathUtils.max(new double[]{-5.5,9.4,1.5,-5.4,-2.5}); returns 9.4 MathUtils.max(new double[]{-5.5,-9.7,-15.2,-5.3,-20.4}); returns -5.5
numbers
- List of double
which maximum will be calculated.List
numbers. If
numbers
is empty or null
then it
returns the maximum value of the data type double
;max(byte...)
,
max(short...)
,
max(float...)
,
max(int...)
,
max(long...)
public static double min(double... numbers)
MathUtils.min(new double[]{5.5,9.8,1.5,5.2,2.9}); returns 1.5 MathUtils.min(new double[]{-5.5,9.4,1.5,-5.4,-2.5}); returns -5.5 MathUtils.min(new double[]{-5.5,-9.7,-15.2,-5.3,-20.4}); returns -20.4
numbers
- List of double
which minimum will be calculated.List
numbers. If
numbers
is empty or null
then it
returns the minimum value of the data type double
;min(byte...)
,
min(short...)
,
min(float...)
,
min(int...)
,
min(long...)
public static double bcdAdd(double addend, double augend)
double
whose value is (addend
+
augend
).
MathUtils.bcdAdd(12.40, 130.98); returns 143.38 MathUtils.bcdAdd(25.40, 120.40); returns 145.80 MathUtils.bcdAdd(-12.40, 130.98); returns 118,57 MathUtils.bcdAdd(-12.40, -130.98); returns -143.38
addend
- augend
- value to be added to addend
double
whose value is (addend
+
augend
)public static double bcdSubtract(double minuend, double subtrahend)
double
whose value is (minuend
-
augend
).
MathUtils.bcdSubtract(12.40, 130.98); returns -118.58 MathUtils.bcdSubtract(-12.40, 130.98); returns -143.38 MathUtils.bcdSubtract(-12.40, -130.98); returns 118.58
minuend
- subtrahend
- value to be subtracted from minuend
double
whose value is (minuend
-
augend
)public static double bcdMultiply(double multiplier, double multiplicand)
double
whose value is (multiplier
x
multiplicand
).
MathUtils.bcdMultiply(-12.40, 130.98); returns -1624.1519999999998 MathUtils.bcdMultiply(-12.40, -130.98); returns 1624.1519999999998 MathUtils.bcdMultiply(-12.40, 130.98); returns -1624.1519999999998
multiplier
- multiplicand
- value to be multiplied by multiplier
double
whose value is (multiplier
x
multiplicand
)public static double bcdDivide(double dividend, double divisor)
bcdDivide(double, double, int)
with
BigDecimal.ROUND_HALF_EVEN
. MathUtils.bcdDivide(150.56, 78.25, 1); returns 1.9240894568690097 MathUtils.bcdDivide(-120.56, 45.20, 0); returns -2.6672566371681414 MathUtils.bcdDivide(-2589.23, -235.56, 1); returns 10.99180675836305
public static double bcdDivide(double dividend, double divisor, int roundingMode)
public static void checkRange(int val, int min, int max) throws IndexOutOfBoundsException
val
is a value between
min
and max
. With the condition:
min
<= val
<= max
.val
- the value which will be checkedmin
- the(inclusive)lower border for value
max
- the(inclusive)upper border for value
IndexOutOfBoundsException
- if val
not between min
and
max
isInRange(int, int, int)
public static boolean isInRange(int val, int min, int max)
val
is a value between
min
and max
. With the condition:
min
<= val
<= max
.
MathUtils.isInRange(42, 10, 50); returns true MathUtils.isInRange(80, 10, 50); returns false MathUtils.isInRange(80, 10, -80); returns false
val
- the value which will be checkedmin
- the(inclusive)lower border for value
max
- the(inclusive)upper border for value
min
<= value
<=
max
checkRange(int, int, int)
public static int computeHash(Object... objects)
objects
- the array whose content-based hash code to computeobjects
Examples
MathUtils.computeHash(5987); returns 6018
MathUtils.computeHash("XDEV"); returns 2689212
MathUtils.computeHash(-545689); returns -545658
public static int computeHashDeep(Object... objects)
MathUtils.computeHashDeep(87956); returns 87987 MathUtils.computeHashDeep(-5646); returns -5615 MathUtils.computeHashDeep(54); returns 85
objects
- the array whose deep-content-based hash code to computeobjects
Arrays.deepHashCode(Object[])
Copyright © 2003–2021 XDEV Software. All rights reserved.