public final class Numbers extends Object
| Modifier and Type | Method and Description |
|---|---|
static int |
gcd(int a,
int b)
Returns the greatest common divisor of the absolute value of the
given two numbers.
|
static long |
gcd(long a,
long b)
Returns the greatest common divisor of the absolute value of the
given two numbers.
|
static boolean |
isFinite(double d)
Returns whether the given value is a "real" value.
|
static boolean |
isFinite(float f)
Returns whether the given value is a "real" value.
|
static int |
lcm(int a,
int b)
Returns the least common multiple of the absolute value of the given
two numbers.
|
static long |
lcm(long a,
long b)
Returns the least common multiple of the absolute value of the given
two numbers.
|
static double |
max(double... values)
Returns the maximum of the given values, using the same comparison
rules as
Math.max(double, double). |
static float |
max(float... values)
Returns the maximum of the given values, using the same comparison
rules as
Math.max(float, float). |
static int |
max(int... values)
Returns the maximum of the given values.
|
static long |
max(long... values)
Returns the maximum of the given values.
|
static double |
mean(double... values)
Returns the arithmetic mean of the given values.
|
static double |
mean(float... values)
Returns the arithmetic mean of the given values.
|
static double |
mean(int... values)
Returns the arithmetic mean of the given values.
|
static double |
mean(long... values)
Returns the arithmetic mean of the given values.
|
static double |
min(double... values)
Returns the minimum of the given values, using the same comparison
rules as
Math.min(double, double). |
static float |
min(float... values)
Returns the minimum of the given values, using the same comparison
rules as
Math.min(float, float). |
static int |
min(int... values)
Returns the minimum of the given values.
|
static long |
min(long... values)
Returns the minimum of the given values.
|
static int |
safeAbs(int a)
Returns the absolute value of the given value, provided the result
fits into an
int. |
static long |
safeAbs(long a)
Returns the absolute value of the given value, provided the result
fits into a
long. |
static int |
safeAdd(int a,
int b)
Returns the sum of
a and b, provided the result fits
into an int. |
static long |
safeAdd(long a,
long b)
Returns the sum of
a and b, provided the result fits
into a long. |
static int |
safeDivide(int a,
int b)
Returns the result of the integer division of the first number by the
second, provided the result fits into an
int. |
static long |
safeDivide(long a,
long b)
Returns the result of the integer division of the first number by the
second, provided the result fits into a
long. |
static int |
safeMultiply(int a,
int b)
Returns the product of
a and b, provided the result
fits into an int. |
static long |
safeMultiply(long a,
long b)
Returns the product of
a and b, provided the result
fits into a long. |
static int |
safeNegate(int a)
Returns the opposite of the given value, provided the result fits
into an
int. |
static long |
safeNegate(long a)
Returns the opposite of the given value, provided the result fits
into a
long. |
static int |
safePow(int a,
int b)
Returns the value of the first argument raised to the power of the
second, provided the result fits into an
int. |
static long |
safePow(long a,
int b)
Returns the value of the first argument raised to the power of the
second, provided the result fits into a
long. |
static int |
safeSubtract(int a,
int b)
Returns the difference of
a and b, provided the
result fits into an int. |
static long |
safeSubtract(long a,
long b)
Returns the difference of
a and b, provided the
result fits into a long. |
static int |
safeToInt(long a)
Casts the given
long value to int, if possible. |
public static long max(long... values)
values - a non-empty array of long values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static int max(int... values)
values - a non-empty array of int values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static float max(float... values)
Math.max(float, float).values - a non-empty array of float values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static double max(double... values)
Math.max(double, double).values - a non-empty array of double values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static long min(long... values)
values - a non-empty array of long values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static int min(int... values)
values - a non-empty array of int values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static float min(float... values)
Math.min(float, float).values - a non-empty array of float values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static double min(double... values)
Math.min(double, double).values - a non-empty array of double values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static double mean(int... values)
values - the values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static double mean(long... values)
values - the values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static double mean(double... values)
values - the values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static double mean(float... values)
values - the values.NullPointerException - if values is null.IllegalArgumentException - if values is empty.public static boolean isFinite(float f)
false if the given value is infinite or NaN, and true
in all other cases.f - the value to test.public static boolean isFinite(double d)
false if the given value is infinite or NaN, and true
in all other cases.d - the value to test.public static long safeAbs(long a)
long.a - the value.a.ArithmeticException - if the absolute value of a can't
be represented by a long.public static int safeAbs(int a)
int.a - the value.a.ArithmeticException - if the absolute value of a can't
be represented by an int.public static long safeNegate(long a)
long.a - the value to negate.-a.ArithmeticException - if -a can't be represented by a
long.public static int safeNegate(int a)
int.a - the value to negate.-a.ArithmeticException - if -a can't be represented by an
int.public static long safeAdd(long a,
long b)
a and b, provided the result fits
into a long.a - the first operand.b - the second operand.a + b.ArithmeticException - if a + b can't be represented by
a long.public static int safeAdd(int a,
int b)
a and b, provided the result fits
into an int.a - the first operand.b - the second operand.a + b.ArithmeticException - if a + b can't be represented by
an int.public static long safeSubtract(long a,
long b)
a and b, provided the
result fits into a long.a - the first operand.b - the second operand.a - b.ArithmeticException - if a - b can't be represented by
a long.public static int safeSubtract(int a,
int b)
a and b, provided the
result fits into an int.a - the first operand.b - the second operand.a - b.ArithmeticException - if a - b can't be represented by
an int.public static long safeMultiply(long a,
long b)
a and b, provided the result
fits into a long.a - the first operand.b - the second operand.a * b.ArithmeticException - if a * b can't be represented by
a long.public static int safeMultiply(int a,
int b)
a and b, provided the result
fits into an int.a - the first operand.b - the second operand.a * b.ArithmeticException - if a * b can't be represented by
an int.public static long safeDivide(long a,
long b)
long.a - the dividend.b - the divisor.a / b.ArithmeticException - if b is equal to 0 or if
a / b can't be represented by a long.public static int safeDivide(int a,
int b)
int.a - the dividend.b - the divisor.a / b.ArithmeticException - if b is equal to 0 or if
a / b can't be represented by an int.public static long safePow(long a,
int b)
long. This method
implements the exponentiation by squaring method. Please refer to
Wikipedia for further information.a - the base.b - the exponent.a ^ b.IllegalArgumentException - if b < 0.ArithmeticException - if a ^ b can't be represented by
a long.public static int safePow(int a,
int b)
int. This method
implements the exponentiation by squaring method. Please refer to
Wikipedia for further information.a - the base.b - the exponent.a ^ b.IllegalArgumentException - if b < 0.ArithmeticException - if a ^ b can't be represented by
an int.public static long gcd(long a,
long b)
Long.MIN_VALUE.
Also, note that if one of the given values is 0, this method
will return the absolute value of the second argument.
This method implements the binary GCD algorithm (also known as
Stein's algorithm). For further information on this algorithm, please
refer to
Wikipedia.a - the first number.b - the second number.a and b.ArithmeticException - if a or b is equal to
Long.MIN_VALUE.public static int gcd(int a,
int b)
Integer.MIN_VALUE.
Also, note that if one of the given values is 0, this method
will return the absolute value of the second argument.
This method implements the binary GCD algorithm (also known as
Stein's algorithm). For further information on this algorithm, please
refer to
Wikipedia.a - the first number.b - the second number.a and b.ArithmeticException - if a or b is equal to
Integer.MIN_VALUE.public static long lcm(long a,
long b)
Long.MIN_VALUE. Also,
note that if one of the given values is 0, this method will
return the absolute value of the second argument. This method uses
the formula lcm(a,b) = (a / gcd(a,b)) * b. Please refer to
Wikipedia for further information.a - the first number.b - the second number.a and b.ArithmeticException - if a or b is equal to
Long.MIN_VALUE.public static int lcm(int a,
int b)
Integer.MIN_VALUE.
Also, note that if one of the given values is 0, this method
will return the absolute value of the second argument. This method
uses the formula lcm(a,b) = (a / gcd(a,b)) * b. Please refer
to
Wikipedia for further information.a - the first number.b - the second number.a and b.ArithmeticException - if a or b is equal to
Integer.MIN_VALUE.public static int safeToInt(long a)
long value to int, if possible.a - the value to cast to int.ArithmeticException - if a cannot be cast into an
int.Copyright © 2012–2015. All rights reserved.