public class Math2 extends java.lang.Object
java.lang.
Math
.Modifier and Type | Field and Description |
---|---|
static java.util.regex.Pattern |
INT_PATTERN |
Constructor and Description |
---|
Math2() |
Modifier and Type | Method and Description |
---|---|
static boolean |
containsRect(java.awt.Rectangle containedRect,
java.awt.Rectangle containingRect)
Checks if one rectangle is completely contained inside the other.
|
static java.lang.String |
decimalToString(double decimal,
int decimalPlaceCount) |
static int |
divAbove(int divided,
int divisor) |
static java.math.BigDecimal |
floorBigDecimal(java.math.BigDecimal value) |
static java.math.BigInteger |
floorBigDecimalToBigInteger(java.math.BigDecimal value) |
static int |
intPow(int basis,
int exponent) |
static boolean |
isBetween(byte value,
byte min,
byte max)
Checks if the specified value is greater or equal to the specified minimum and
lower or equal to the specified maximum.
|
static boolean |
isBetween(double value,
double min,
double max)
Checks if the specified value is greater or equal to the specified minimum and
lower or equal to the specified maximum.
|
static boolean |
isBetween(float value,
float min,
float max)
Checks if the specified value is greater or equal to the specified minimum and
lower or equal to the specified maximum.
|
static boolean |
isBetween(int value,
int min,
int max)
Checks if the specified value is greater or equal to the specified minimum and
lower or equal to the specified maximum.
|
static boolean |
isBetween(long value,
long min,
long max)
Checks if the specified value is greater or equal to the specified minimum and
lower or equal to the specified maximum.
|
static boolean |
isBetweenNE(byte value,
byte min,
byte max)
Checks if the specified value is greater then the specified minimum and
lower than the specified maximum.
|
static boolean |
isBetweenNE(double value,
double min,
double max)
Checks if the specified value is greater then the specified minimum and
lower than the specified maximum.
|
static boolean |
isBetweenNE(float value,
float min,
float max)
Checks if the specified value is greater then the specified minimum and
lower than the specified maximum.
|
static boolean |
isBetweenNE(int value,
int min,
int max)
Checks if the specified value is greater then the specified minimum and
lower than the specified maximum.
|
static boolean |
isBetweenNE(long value,
long min,
long max)
Checks if the specified value is greater then the specified minimum and
lower than the specified maximum.
|
static boolean |
isDecimal(java.lang.String value)
Returns the whether the given string can be parsed to a
double or a float . |
static boolean |
isInt(double value)
Checks if the specified value is a mathematical integer.
|
static boolean |
isInt(float value)
Checks if the specified value is a mathematical integer.
|
static boolean |
isInt(java.lang.String value)
Checks if the specified value is the string representation of a mathematical integer.
|
static double |
log(double a,
double base) |
static long |
longPow(long basis,
long exponent) |
static double |
maxDouble(double... values)
Returns the maximum of the specified values.
|
static float |
maxFloat(float... values)
Returns the maximum of the specified values.
|
static int |
maxInt(int... values)
Returns the maximum of the specified values.
|
static long |
maxLong(long... values)
Returns the maximum of the specified values.
|
static double |
minDouble(double... values)
Returns the minimum of the specified values.
|
static float |
minFloat(float... values)
Returns the minimum of the specified values.
|
static int |
minInt(int... values)
Returns the minimum of the specified values.
|
static long |
minLong(long... values)
Returns the minimum of the specified values.
|
static byte |
moveBetween(byte value,
byte min,
byte max)
Returns the specified value if it is between the specified minimum and maximum.
|
static double |
moveBetween(double value,
double min,
double max)
Returns the specified value if it is between the specified minimum and maximum.
|
static float |
moveBetween(float value,
float min,
float max)
Returns the specified value if it is between the specified minimum and maximum.
|
static int |
moveBetween(int value,
int min,
int max)
Returns the specified value if it is between the specified minimum and maximum.
|
static long |
moveBetween(long value,
long min,
long max)
Returns the specified value if it is between the specified minimum and maximum.
|
static boolean |
overlaps(byte min1,
byte max1,
byte min2,
byte max2)
Checks whether the two specified intervals have an overlap.
|
static boolean |
overlaps(double min1,
double max1,
double min2,
double max2)
Checks whether the two specified intervals have an overlap.
|
static boolean |
overlaps(float min1,
float max1,
float min2,
float max2)
Checks whether the two specified intervals have an overlap.
|
static boolean |
overlaps(int min1,
int max1,
int min2,
int max2)
Checks whether the two specified intervals have an overlap.
|
static boolean |
overlaps(long min1,
long max1,
long min2,
long max2)
Checks whether the two specified intervals have an overlap.
|
static boolean |
overlapsNE(byte min1,
byte max1,
byte min2,
byte max2) |
static boolean |
overlapsNE(double min1,
double max1,
double min2,
double max2) |
static boolean |
overlapsNE(float min1,
float max1,
float min2,
float max2) |
static boolean |
overlapsNE(int min1,
int max1,
int min2,
int max2) |
static boolean |
overlapsNE(long min1,
long max1,
long min2,
long max2) |
static double |
parseDouble(java.lang.String string)
Parses a double value which can have either "," or "." as its decimal separator.
|
static float |
parseFloat(java.lang.String string)
Parses a float value which can have either "," or "." as its decimal separator.
|
static java.math.BigDecimal |
roundBigDecimal(java.math.BigDecimal value,
java.math.RoundingMode roundingMode) |
static java.math.BigInteger |
roundBigDecimalToBigInteger(java.math.BigDecimal value,
java.math.RoundingMode roundingMode) |
static double |
roundFirstSignificantDigit(double value)
Rounds a double value at the given first significant digit.
|
static float |
roundFirstSignificantDigit(float value)
Rounds a float value at the given first significant digit.
|
static double |
roundSignificantDigit(double value,
int digit)
Rounds a double value at the given significant digit.
|
static float |
roundSignificantDigit(float value,
int digit)
Rounds a float value at the given significant digit.
|
static long |
roundUp(double value)
Returns the next integer value that is greater or equal to the specified value.
|
static int |
roundUp(float value)
Returns the next integer value that is greater or equal to the specified value.
|
static int |
sum1ToN(int n)
Returns the sum of all integers from 1 to the specified value.
|
public static final java.util.regex.Pattern INT_PATTERN
public Math2()
public static double log(double a, double base)
public static int intPow(int basis, int exponent)
public static long longPow(long basis, long exponent)
public static int divAbove(int divided, int divisor)
public static boolean isInt(java.lang.String value)
value
- the string to be testedtrue
if value
is the string representation of an integer or false
otherwisepublic static boolean isInt(double value)
value
- the value to be testedtrue
if value
is an integer or false
otherwisepublic static boolean isInt(float value)
value
- the value to be testedtrue
if value
is an integer or false
otherwisepublic static boolean isDecimal(java.lang.String value)
double
or a float
.value
- the string to be parsedtrue
if the given string could be parsed, false
otherwise.public static int minInt(int... values)
It is possible to pass only one value which is directly returned in that case.
values
- a list of valuesjava.lang.IllegalArgumentException
- if an empty array is specifiedpublic static long minLong(long... values)
It is possible to pass only one value which is directly returned in that case.
values
- a list of valuesjava.lang.IllegalArgumentException
- if an empty array is specifiedpublic static float minFloat(float... values)
It is possible to pass only one value which is directly returned in that case.
values
- a list of valuesjava.lang.IllegalArgumentException
- if an empty array is specifiedpublic static double minDouble(double... values)
It is possible to pass only one value which is directly returned in that case.
values
- a list of valuesjava.lang.IllegalArgumentException
- if an empty array is specifiedpublic static int maxInt(int... values)
It is possible to pass only one value which is directly returned in that case.
values
- a list of valuesjava.lang.IllegalArgumentException
- if an empty array is specifiedpublic static long maxLong(long... values)
It is possible to pass only one value which is directly returned in that case.
values
- a list of valuesjava.lang.IllegalArgumentException
- if an empty array is specifiedpublic static float maxFloat(float... values)
It is possible to pass only one value which is directly returned in that case.
values
- a list of valuesjava.lang.IllegalArgumentException
- if an empty array is specifiedpublic static double maxDouble(double... values)
It is possible to pass only one value which is directly returned in that case.
values
- a list of valuesjava.lang.IllegalArgumentException
- if an empty array is specifiedpublic static boolean isBetween(byte value, byte min, byte max)
value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static boolean isBetween(int value, int min, int max)
value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static boolean isBetween(long value, long min, long max)
value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static boolean isBetween(float value, float min, float max)
value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static boolean isBetween(double value, double min, double max)
value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static boolean isBetweenNE(byte value, byte min, byte max)
min
or max
this method will return false.value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static boolean isBetweenNE(int value, int min, int max)
min
or max
this method will return false.value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static boolean isBetweenNE(long value, long min, long max)
min
or max
this method will return false.value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static boolean isBetweenNE(float value, float min, float max)
min
or max
this method will return false.value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static boolean isBetweenNE(double value, double min, double max)
min
or max
this method will return false.value
- the value to be testedmin
- the minimum valuemax
- the maximum valuetrue
if value
if between min
and max
, false
otherwisepublic static byte moveBetween(byte value, byte min, byte max)
value
is lower than min
, min
is returned, if it is greater than max
, max
is returned.value
- the value to be moved into the specified intervalmin
- the lower border of the intervalmax
- the upper border of the intervalMath.min(max, Math.max(min, value))
public static int moveBetween(int value, int min, int max)
value
is lower than min
, min
is returned, if it is greater than max
, max
is returned.value
- the value to be moved into the specified intervalmin
- the lower border of the intervalmax
- the upper border of the intervalMath.min(max, Math.max(min, value))
public static long moveBetween(long value, long min, long max)
value
is lower than min
, min
is returned, if it is greater than max
, max
is returned.value
- the value to be moved into the specified intervalmin
- the lower border of the intervalmax
- the upper border of the intervalMath.min(max, Math.max(min, value))
public static float moveBetween(float value, float min, float max)
value
is lower than min
, min
is returned, if it is greater than max
, max
is returned.value
- the value to be moved into the specified intervalmin
- the lower border of the intervalmax
- the upper border of the intervalMath.min(max, Math.max(min, value))
public static double moveBetween(double value, double min, double max)
value
is lower than min
, min
is returned, if it is greater than max
, max
is returned.value
- the value to be moved into the specified intervalmin
- the lower border of the intervalmax
- the upper border of the intervalMath.min(max, Math.max(min, value))
public static boolean overlaps(byte min1, byte max1, byte min2, byte max2)
min1
must be lower or equal than max1
and min2
must be lower or equal to max2
.
This method does not check whether this conditions are fulfilled.
min1
- the lower border of the first intervalmax1
- the upper border of the first intervalmin2
- the lower border of the second intervalmax2
- the upper border of the second intervaltrue
if the two intervals overlap, false
otherwisepublic static boolean overlaps(int min1, int max1, int min2, int max2)
min1
must be lower or equal than max1
and min2
must be lower or equal to max2
.
This method does not check whether this conditions are fulfilled.
min1
- the lower border of the first intervalmax1
- the upper border of the first intervalmin2
- the lower border of the second intervalmax2
- the upper border of the second intervaltrue
if the two intervals overlap, false
otherwisepublic static boolean overlaps(long min1, long max1, long min2, long max2)
min1
must be lower or equal than max1
and min2
must be lower or equal to max2
.
This method does not check whether this conditions are fulfilled.
min1
- the lower border of the first intervalmax1
- the upper border of the first intervalmin2
- the lower border of the second intervalmax2
- the upper border of the second intervaltrue
if the two intervals overlap, false
otherwisepublic static boolean overlaps(float min1, float max1, float min2, float max2)
min1
must be lower or equal than max1
and min2
must be lower or equal to max2
.
This method does not check whether this conditions are fulfilled.
min1
- the lower border of the first intervalmax1
- the upper border of the first intervalmin2
- the lower border of the second intervalmax2
- the upper border of the second intervaltrue
if the two intervals overlap, false
otherwisepublic static boolean overlaps(double min1, double max1, double min2, double max2)
min1
must be lower or equal than max1
and min2
must be lower or equal to max2
.
This method does not check whether this conditions are fulfilled.
min1
- the lower border of the first intervalmax1
- the upper border of the first intervalmin2
- the lower border of the second intervalmax2
- the upper border of the second intervaltrue
if the two intervals overlap, false
otherwisepublic static boolean overlapsNE(byte min1, byte max1, byte min2, byte max2)
public static boolean overlapsNE(int min1, int max1, int min2, int max2)
public static boolean overlapsNE(long min1, long max1, long min2, long max2)
public static boolean overlapsNE(float min1, float max1, float min2, float max2)
public static boolean overlapsNE(double min1, double max1, double min2, double max2)
public static boolean containsRect(java.awt.Rectangle containedRect, java.awt.Rectangle containingRect)
containedRect
- the rectangle that shall be tested to be contained in containingRect
containingRect
- the rectangle that shall be tested to contain containedRect
true
if the one rectangle is contained in the other, false
otherwisepublic static long roundUp(double value)
value
- the floating point value to be convertedlong
>= value
public static int roundUp(float value)
value
- the floating point value to be convertedint
>= value
public static java.math.BigDecimal roundBigDecimal(java.math.BigDecimal value, java.math.RoundingMode roundingMode)
public static java.math.BigDecimal floorBigDecimal(java.math.BigDecimal value)
public static java.math.BigInteger roundBigDecimalToBigInteger(java.math.BigDecimal value, java.math.RoundingMode roundingMode)
public static java.math.BigInteger floorBigDecimalToBigInteger(java.math.BigDecimal value)
public static double roundSignificantDigit(double value, int digit)
Examples:
roundSignificantDigit(12.4, 0)
would return 12roundSignificantDigit(12.4, 1)
would return 10roundSignificantDigit(0.03462, -3)
would return 0.035value
- the value to be roundeddigit
- the decimal power to be rounded topublic static float roundSignificantDigit(float value, int digit)
Examples:
roundSignificantDigit(12.4, 0)
would return 12roundSignificantDigit(12.4, 1)
would return 10roundSignificantDigit(0.03462, -3)
would return 0.035value
- the value to be roundeddigit
- the decimal power to be rounded topublic static double roundFirstSignificantDigit(double value)
Examples:
roundSignificantDigit(79.0)
would return 80.0roundSignificantDigit(12.0)
would return 10.0roundSignificantDigit(0.03462)
would return 0.03roundSignificantDigit(value, (int)Math.floor(Math.log10(value)))
.value
- the value to be roundedpublic static float roundFirstSignificantDigit(float value)
Examples:
roundSignificantDigit(79.0)
would return 80.0roundSignificantDigit(12.0)
would return 10.0roundSignificantDigit(0.03462)
would return 0.03roundSignificantDigit(value, (int)Math.floor(Math.log10(value)))
.value
- the value to be roundedpublic static float parseFloat(java.lang.String string)
string
- the string to parsepublic static double parseDouble(java.lang.String string)
string
- the string to parsejava.lang.NumberFormatException
- if the specified string does not represent a valid decimal valuepublic static java.lang.String decimalToString(double decimal, int decimalPlaceCount)
public static int sum1ToN(int n)
n
- the value to sum up to (must be greater or equal to zero)java.lang.ArithmeticException
- if n
is smaller than zero