public final class MathUtils extends Object
java.lang.Math
.Modifier and Type | Method and Description |
---|---|
static int |
floorDiv(int a,
int b)
Returns the floor division.
|
static long |
floorDiv(long a,
long b)
Returns the floor division.
|
static int |
floorMod(int a,
int b)
Returns the floor modulus.
|
static int |
floorMod(long a,
int b)
Returns the floor modulus.
|
static long |
floorMod(long a,
long b)
Returns the floor modulus.
|
static int |
safeAdd(int a,
int b)
Safely adds two int values.
|
static long |
safeAdd(long a,
long b)
Safely adds two long values.
|
static int |
safeCompare(int a,
int b)
Safely compare one int with another.
|
static int |
safeCompare(long a,
long b)
Safely compare one long with another.
|
static int |
safeDecrement(int value)
Safely decrements an int.
|
static long |
safeDecrement(long value)
Safely decrements a long.
|
static int |
safeIncrement(int value)
Safely increments an int.
|
static long |
safeIncrement(long value)
Safely increments a long.
|
static int |
safeMultiply(int a,
int b)
Safely multiply one int by another.
|
static long |
safeMultiply(long a,
int b)
Safely multiply a long by an int.
|
static long |
safeMultiply(long a,
long b)
Multiply two values throwing an exception if overflow occurs.
|
static int |
safeNegate(int value)
Negates the input value, throwing an exception if an overflow occurs.
|
static long |
safeNegate(long value)
Negates the input value, throwing an exception if an overflow occurs.
|
static int |
safeSubtract(int a,
int b)
Safely subtracts one int from another.
|
static long |
safeSubtract(long a,
long b)
Safely subtracts one long from another.
|
static int |
safeToInt(long value)
Safely convert a long to an int.
|
public static int safeNegate(int value)
value
- the value to negateArithmeticException
- if the value is MIN_VALUE and cannot be negatedpublic static long safeNegate(long value)
value
- the value to negateArithmeticException
- if the value is MIN_VALUE and cannot be negatedpublic static int safeAdd(int a, int b)
a
- the first valueb
- the second valueArithmeticException
- if the result overflows an intpublic static long safeAdd(long a, long b)
a
- the first valueb
- the second valueArithmeticException
- if the result overflows a longpublic static int safeSubtract(int a, int b)
a
- the first valueb
- the second value to subtract from the firstArithmeticException
- if the result overflows an intpublic static long safeSubtract(long a, long b)
a
- the first valueb
- the second value to subtract from the firstArithmeticException
- if the result overflows a longpublic static int safeMultiply(int a, int b)
a
- the first valueb
- the second valueArithmeticException
- if the result overflows an intpublic static long safeMultiply(long a, int b)
a
- the first valueb
- the second valueArithmeticException
- if the result overflows a longpublic static long safeMultiply(long a, long b)
a
- the first valueb
- the second valueArithmeticException
- if the result overflows a longpublic static int safeIncrement(int value)
value
- the value to incrementArithmeticException
- if the result overflows a longpublic static long safeIncrement(long value)
value
- the value to incrementArithmeticException
- if the result overflows a longpublic static int safeDecrement(int value)
value
- the value to decrementArithmeticException
- if the result overflows a longpublic static long safeDecrement(long value)
value
- the value to decrementArithmeticException
- if the result overflows a longpublic static int safeToInt(long value)
value
- the value to convertArithmeticException
- if the result overflows an intpublic static int safeCompare(int a, int b)
a
- the first valueb
- the second valuepublic static int safeCompare(long a, long b)
a
- the first valueb
- the second valuepublic static long floorDiv(long a, long b)
This returns 0
for floorDiv(0, 4)
.
This returns -1
for floorDiv(-1, 4)
.
This returns -1
for floorDiv(-2, 4)
.
This returns -1
for floorDiv(-3, 4)
.
This returns -1
for floorDiv(-4, 4)
.
This returns -2
for floorDiv(-5, 4)
.
a
- the dividendb
- the divisorpublic static long floorMod(long a, long b)
This returns 0
for floorMod(0, 4)
.
This returns 1
for floorMod(-1, 4)
.
This returns 2
for floorMod(-2, 4)
.
This returns 3
for floorMod(-3, 4)
.
This returns 0
for floorMod(-4, 4)
.
a
- the dividendb
- the divisorpublic static int floorMod(long a, int b)
This returns 0
for floorMod(0, 4)
.
This returns 3
for floorMod(-1, 4)
.
This returns 2
for floorMod(-2, 4)
.
This returns 1
for floorMod(-3, 4)
.
This returns 0
for floorMod(-4, 4)
.
This returns 3
for floorMod(-5, 4)
.
a
- the dividendb
- the divisorpublic static int floorDiv(int a, int b)
This returns 0
for floorDiv(0, 4)
.
This returns -1
for floorDiv(-1, 4)
.
This returns -1
for floorDiv(-2, 4)
.
This returns -1
for floorDiv(-3, 4)
.
This returns -1
for floorDiv(-4, 4)
.
This returns -2
for floorDiv(-5, 4)
.
a
- the dividendb
- the divisorpublic static int floorMod(int a, int b)
This returns 0
for floorMod(0, 4)
.
This returns 1
for floorMod(-1, 4)
.
This returns 2
for floorMod(-2, 4)
.
This returns 3
for floorMod(-3, 4)
.
This returns 0
for floorMod(-4, 4)
.
a
- the dividendb
- the divisorCopyright © 2014. All rights reserved.