public final class ArithmeticUtils extends Object
| Modifier and Type | Method and Description |
|---|---|
static int |
gcdPositive(int... args)
Returns the greatest common divisor of the given absolute values.
|
static int |
gcdPositive(int a,
int b)
Returns the greatest common divisor of two positive numbers (this precondition is not checked and the
result is undefined if not fulfilled) using the "binary gcd" method which avoids division and modulo operations.
|
static int |
lcmPositive(int... args)
Returns the least common multiple of the given absolute values.
|
static int |
lcmPositive(int a,
int b)
Returns the least common multiple of the absolute value of two numbers, using the formula
lcm(a, b) = (a / gcd(a, b)) * b. |
static int |
mulAndCheck(int x,
int y)
Multiply two integers, checking for overflow.
|
static float |
wrapAngleAroundZero(float a)
Wraps the given angle to the range [-PI, PI]
|
public static float wrapAngleAroundZero(float a)
a - the angle in radianspublic static int gcdPositive(int a,
int b)
Special cases:
gcd(x, x), gcd(0, x) and gcd(x, 0) is the value of x.gcd(0, 0) is the only one which returns 0.a - a non negative number.b - a non negative number.public static int lcmPositive(int a,
int b)
throws ArithmeticException
lcm(a, b) = (a / gcd(a, b)) * b.
Special cases:
lcm(Integer.MIN_VALUE, n) and lcm(n, Integer.MIN_VALUE), where abs(n) is a power
of 2, throw an ArithmeticException, because the result would be 2^31, which is too large for an int value.lcm(0, x) and lcm(x, 0) is 0 for any x.
a - a non-negative number.b - a non-negative number.ArithmeticException - if the result cannot be represented as a non-negative int value.public static int gcdPositive(int... args)
gcdPositive(int, int)
and has the same special cases.args - non-negative numberspublic static int lcmPositive(int... args)
lcmPositive(int, int) and
has the same special cases.args - non-negative numbersArithmeticException - if the result cannot be represented as a non-negative int value.public static int mulAndCheck(int x,
int y)
throws ArithmeticException
x - first factory - second factorx * y.ArithmeticException - if the result can not be represented as an int.Copyright © 2017. All rights reserved.