Package com.google.common.math
Class IntMath
java.lang.Object
com.google.common.math.IntMath
@GwtCompatible(emulated=true)
@Deprecated(since="2022-12-01")
public final class IntMath
extends Object
Deprecated.
The Google Guava Core Libraries are deprecated and will not be part of the AEM SDK after April 2023
A class for arithmetic on values of type
int. Where possible, methods are defined and
named analogously to their BigInteger counterparts.
The implementations of many methods in this class are based on material from Henry S. Warren, Jr.'s Hacker's Delight, (Addison Wesley, 2002).
Similar functionality for long and for BigInteger can be found in
LongMath and BigIntegerMath respectively. For other common operations on
int values, see Ints.
- Since:
- 11.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic intbinomial(int n, int k) Deprecated.Returnsnchoosek, also known as the binomial coefficient ofnandk, orInteger.MAX_VALUEif the result does not fit in anint.static intcheckedAdd(int a, int b) Deprecated.Returns the sum ofaandb, provided it does not overflow.static intcheckedMultiply(int a, int b) Deprecated.Returns the product ofaandb, provided it does not overflow.static intcheckedPow(int b, int k) Deprecated.Returns thebto thekth power, provided it does not overflow.static intcheckedSubtract(int a, int b) Deprecated.Returns the difference ofaandb, provided it does not overflow.static intdivide(int p, int q, RoundingMode mode) Deprecated.Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.static intfactorial(int n) Deprecated.Returnsn!, that is, the product of the firstnpositive integers,1ifn == 0, orInteger.MAX_VALUEif the result does not fit in aint.static intgcd(int a, int b) Deprecated.Returns the greatest common divisor ofa, b.static booleanisPowerOfTwo(int x) Deprecated.Returnstrueifxrepresents a power of two.static intlog10(int x, RoundingMode mode) Deprecated.Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.static intlog2(int x, RoundingMode mode) Deprecated.Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.static intmean(int x, int y) Deprecated.Returns the arithmetic mean ofxandy, rounded towards negative infinity.static intmod(int x, int m) Deprecated.Returnsx mod m.static intpow(int b, int k) Deprecated.Returnsbto thekth power.static intsqrt(int x, RoundingMode mode) Deprecated.Returns the square root ofx, rounded with the specified rounding mode.
-
Method Details
-
isPowerOfTwo
public static boolean isPowerOfTwo(int x) Deprecated.Returnstrueifxrepresents a power of two.This differs from
Integer.bitCount(x) == 1, becauseInteger.bitCount(Integer.MIN_VALUE) == 1, butInteger.MIN_VALUEis not a power of two. -
log2
Deprecated.Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.- Throws:
IllegalArgumentException- ifx <= 0ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not a power of two
-
log10
@GwtIncompatible("need BigIntegerMath to adequately test") public static int log10(int x, RoundingMode mode) Deprecated.Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.- Throws:
IllegalArgumentException- ifx <= 0ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not a power of ten
-
pow
Deprecated.Returnsbto thekth power. Even if the result overflows, it will be equal toBigInteger.valueOf(b).pow(k).intValue(). This implementation runs inO(log k)time.Compare
checkedPow(int, int), which throws anArithmeticExceptionupon overflow.- Throws:
IllegalArgumentException- ifk < 0
-
sqrt
@GwtIncompatible("need BigIntegerMath to adequately test") public static int sqrt(int x, RoundingMode mode) Deprecated.Returns the square root ofx, rounded with the specified rounding mode.- Throws:
IllegalArgumentException- ifx < 0ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandsqrt(x)is not an integer
-
divide
Deprecated.Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.- Throws:
ArithmeticException- ifq == 0, or ifmode == UNNECESSARYandais not an integer multiple ofb
-
mod
public static int mod(int x, int m) Deprecated.Returnsx mod m. This differs fromx % min that it always returns a non-negative result.For example:
mod(7, 4) == 3 mod(-7, 4) == 1 mod(-1, 4) == 3 mod(-8, 4) == 0 mod(8, 4) == 0- Throws:
ArithmeticException- ifm <= 0
-
gcd
public static int gcd(int a, int b) Deprecated.Returns the greatest common divisor ofa, b. Returns0ifa == 0 && b == 0.- Throws:
IllegalArgumentException- ifa < 0orb < 0
-
checkedAdd
public static int checkedAdd(int a, int b) Deprecated.Returns the sum ofaandb, provided it does not overflow.- Throws:
ArithmeticException- ifa + boverflows in signedintarithmetic
-
checkedSubtract
public static int checkedSubtract(int a, int b) Deprecated.Returns the difference ofaandb, provided it does not overflow.- Throws:
ArithmeticException- ifa - boverflows in signedintarithmetic
-
checkedMultiply
public static int checkedMultiply(int a, int b) Deprecated.Returns the product ofaandb, provided it does not overflow.- Throws:
ArithmeticException- ifa * boverflows in signedintarithmetic
-
checkedPow
public static int checkedPow(int b, int k) Deprecated.Returns thebto thekth power, provided it does not overflow.pow(int, int)may be faster, but does not check for overflow.- Throws:
ArithmeticException- ifbto thekth power overflows in signedintarithmetic
-
factorial
public static int factorial(int n) Deprecated.Returnsn!, that is, the product of the firstnpositive integers,1ifn == 0, orInteger.MAX_VALUEif the result does not fit in aint.- Throws:
IllegalArgumentException- ifn < 0
-
binomial
Deprecated.Returnsnchoosek, also known as the binomial coefficient ofnandk, orInteger.MAX_VALUEif the result does not fit in anint.- Throws:
IllegalArgumentException- ifn < 0,k < 0ork > n
-
mean
public static int mean(int x, int y) Deprecated.Returns the arithmetic mean ofxandy, rounded towards negative infinity. This method is overflow resilient.- Since:
- 14.0
-