Package com.google.common.math
Class IntMath
- java.lang.Object
-
- com.google.common.math.IntMath
-
@GwtCompatible(emulated=true) public final class IntMath extends java.lang.Object
A class for arithmetic on values of typeint
. Where possible, methods are defined and named analogously to theirBigInteger
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 forBigInteger
can be found inLongMath
andBigIntegerMath
respectively. For other common operations onint
values, seeInts
.- Since:
- 11.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
binomial(int n, int k)
Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orInteger.MAX_VALUE
if the result does not fit in anint
.static int
checkedAdd(int a, int b)
Returns the sum ofa
andb
, provided it does not overflow.static int
checkedMultiply(int a, int b)
Returns the product ofa
andb
, provided it does not overflow.static int
checkedPow(int b, int k)
Returns theb
to thek
th power, provided it does not overflow.static int
checkedSubtract(int a, int b)
Returns the difference ofa
andb
, provided it does not overflow.static int
divide(int p, int q, java.math.RoundingMode mode)
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.static int
factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orInteger.MAX_VALUE
if the result does not fit in aint
.static int
gcd(int a, int b)
Returns the greatest common divisor ofa, b
.static boolean
isPowerOfTwo(int x)
Returnstrue
ifx
represents a power of two.static int
log10(int x, java.math.RoundingMode mode)
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.static int
log2(int x, java.math.RoundingMode mode)
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.static int
mean(int x, int y)
Returns the arithmetic mean ofx
andy
, rounded towards negative infinity.static int
mod(int x, int m)
Returnsx mod m
, a non-negative value less thanm
.static int
pow(int b, int k)
Returnsb
to thek
th power.static int
sqrt(int x, java.math.RoundingMode mode)
Returns the square root ofx
, rounded with the specified rounding mode.
-
-
-
Method Detail
-
isPowerOfTwo
public static boolean isPowerOfTwo(int x)
Returnstrue
ifx
represents a power of two.This differs from
Integer.bitCount(x) == 1
, becauseInteger.bitCount(Integer.MIN_VALUE) == 1
, butInteger.MIN_VALUE
is not a power of two.
-
log2
public static int log2(int x, java.math.RoundingMode mode)
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.- Throws:
java.lang.IllegalArgumentException
- ifx <= 0
java.lang.ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of two
-
log10
@GwtIncompatible("need BigIntegerMath to adequately test") public static int log10(int x, java.math.RoundingMode mode)
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.- Throws:
java.lang.IllegalArgumentException
- ifx <= 0
java.lang.ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of ten
-
pow
@GwtIncompatible("failing tests") public static int pow(int b, int k)
Returnsb
to thek
th 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 anArithmeticException
upon overflow.- Throws:
java.lang.IllegalArgumentException
- ifk < 0
-
sqrt
@GwtIncompatible("need BigIntegerMath to adequately test") public static int sqrt(int x, java.math.RoundingMode mode)
Returns the square root ofx
, rounded with the specified rounding mode.- Throws:
java.lang.IllegalArgumentException
- ifx < 0
java.lang.ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andsqrt(x)
is not an integer
-
divide
public static int divide(int p, int q, java.math.RoundingMode mode)
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.- Throws:
java.lang.ArithmeticException
- ifq == 0
, or ifmode == UNNECESSARY
anda
is not an integer multiple ofb
-
mod
public static int mod(int x, int m)
Returnsx mod m
, a non-negative value less thanm
. This differs fromx % m
, which might be negative.For example:
mod(7, 4) == 3 mod(-7, 4) == 1 mod(-1, 4) == 3 mod(-8, 4) == 0 mod(8, 4) == 0
- Throws:
java.lang.ArithmeticException
- ifm <= 0
- See Also:
- Remainder Operator
-
gcd
public static int gcd(int a, int b)
Returns the greatest common divisor ofa, b
. Returns0
ifa == 0 && b == 0
.- Throws:
java.lang.IllegalArgumentException
- ifa < 0
orb < 0
-
checkedAdd
public static int checkedAdd(int a, int b)
Returns the sum ofa
andb
, provided it does not overflow.- Throws:
java.lang.ArithmeticException
- ifa + b
overflows in signedint
arithmetic
-
checkedSubtract
public static int checkedSubtract(int a, int b)
Returns the difference ofa
andb
, provided it does not overflow.- Throws:
java.lang.ArithmeticException
- ifa - b
overflows in signedint
arithmetic
-
checkedMultiply
public static int checkedMultiply(int a, int b)
Returns the product ofa
andb
, provided it does not overflow.- Throws:
java.lang.ArithmeticException
- ifa * b
overflows in signedint
arithmetic
-
checkedPow
public static int checkedPow(int b, int k)
Returns theb
to thek
th power, provided it does not overflow.pow(int, int)
may be faster, but does not check for overflow.- Throws:
java.lang.ArithmeticException
- ifb
to thek
th power overflows in signedint
arithmetic
-
factorial
public static int factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orInteger.MAX_VALUE
if the result does not fit in aint
.- Throws:
java.lang.IllegalArgumentException
- ifn < 0
-
binomial
@GwtIncompatible("need BigIntegerMath to adequately test") public static int binomial(int n, int k)
Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orInteger.MAX_VALUE
if the result does not fit in anint
.- Throws:
java.lang.IllegalArgumentException
- ifn < 0
,k < 0
ork > n
-
mean
public static int mean(int x, int y)
Returns the arithmetic mean ofx
andy
, rounded towards negative infinity. This method is overflow resilient.- Since:
- 14.0
-
-