Package com.google.common.math
Class IntMath
java.lang.Object
com.google.common.math.IntMath
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
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static int[]
private static final int[]
(package private) static final int
(package private) static final int[]
(package private) static final int
The biggest half power of two that can fit in an unsigned int.(package private) static final int
(package private) static final byte[]
(package private) static final int[]
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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
ceilingPowerOfTwo
(int x) Returns the smallest power of two greater than or equal tox
.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, 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
floorPowerOfTwo
(int x) Returns the largest power of two less than or equal tox
.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 boolean
isPrime
(int n) Returnstrue
ifn
is a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers.(package private) static int
lessThanBranchFree
(int x, int y) Returns 1 ifx < y
as unsigned integers, and 0 otherwise.static int
log10
(int x, RoundingMode mode) Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.private static int
log10Floor
(int x) static int
log2
(int x, 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
saturatedAdd
(int a, int b) Returns the sum ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.static int
saturatedMultiply
(int a, int b) Returns the product ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.static int
saturatedPow
(int b, int k) Returns theb
to thek
th power, unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.static int
saturatedSubtract
(int a, int b) Returns the difference ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.static int
sqrt
(int x, RoundingMode mode) Returns the square root ofx
, rounded with the specified rounding mode.private static int
sqrtFloor
(int x)
-
Field Details
-
MAX_SIGNED_POWER_OF_TWO
static final int MAX_SIGNED_POWER_OF_TWO- See Also:
-
MAX_POWER_OF_SQRT2_UNSIGNED
static final int MAX_POWER_OF_SQRT2_UNSIGNEDThe biggest half power of two that can fit in an unsigned int.- See Also:
-
maxLog10ForLeadingZeros
static final byte[] maxLog10ForLeadingZeros -
powersOf10
static final int[] powersOf10 -
halfPowersOf10
static final int[] halfPowersOf10 -
FLOOR_SQRT_MAX_INT
static final int FLOOR_SQRT_MAX_INT- See Also:
-
factorials
private static final int[] factorials -
biggestBinomials
static int[] biggestBinomials
-
-
Constructor Details
-
IntMath
private IntMath()
-
-
Method Details
-
ceilingPowerOfTwo
public static int ceilingPowerOfTwo(int x) Returns the smallest power of two greater than or equal tox
. This is equivalent tocheckedPow(2, log2(x, CEILING))
.- Throws:
IllegalArgumentException
- ifx <= 0
ArithmeticException
- of the next-higher power of two is not representable as anint
, i.e. whenx > 2^30
- Since:
- 20.0
-
floorPowerOfTwo
public static int floorPowerOfTwo(int x) Returns the largest power of two less than or equal tox
. This is equivalent tocheckedPow(2, log2(x, FLOOR))
.- Throws:
IllegalArgumentException
- ifx <= 0
- Since:
- 20.0
-
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. -
lessThanBranchFree
static int lessThanBranchFree(int x, int y) Returns 1 ifx < y
as unsigned integers, and 0 otherwise. Assumes that x - y fits into a signed int. The implementation is branch-free, and benchmarks suggest it is measurably (if narrowly) faster than the straightforward ternary expression. -
log2
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.- Throws:
IllegalArgumentException
- ifx <= 0
ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of two
-
log10
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.- Throws:
IllegalArgumentException
- ifx <= 0
ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andx
is not a power of ten
-
log10Floor
private static int log10Floor(int x) -
pow
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:
IllegalArgumentException
- ifk < 0
-
sqrt
Returns the square root ofx
, rounded with the specified rounding mode.- Throws:
IllegalArgumentException
- ifx < 0
ArithmeticException
- ifmode
isRoundingMode.UNNECESSARY
andsqrt(x)
is not an integer
-
sqrtFloor
private static int sqrtFloor(int x) -
divide
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.- Throws:
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:
ArithmeticException
- ifm <= 0
- See Also:
-
gcd
public static int gcd(int a, int b) Returns the greatest common divisor ofa, b
. Returns0
ifa == 0 && b == 0
.- Throws:
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:
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:
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:
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:
ArithmeticException
- ifb
to thek
th power overflows in signedint
arithmetic
-
saturatedAdd
public static int saturatedAdd(int a, int b) Returns the sum ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.- Since:
- 20.0
-
saturatedSubtract
public static int saturatedSubtract(int a, int b) Returns the difference ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.- Since:
- 20.0
-
saturatedMultiply
public static int saturatedMultiply(int a, int b) Returns the product ofa
andb
unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.- Since:
- 20.0
-
saturatedPow
public static int saturatedPow(int b, int k) Returns theb
to thek
th power, unless it would overflow or underflow in which caseInteger.MAX_VALUE
orInteger.MIN_VALUE
is returned, respectively.- Since:
- 20.0
-
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:
IllegalArgumentException
- ifn < 0
-
binomial
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:
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
-
isPrime
public static boolean isPrime(int n) Returnstrue
ifn
is a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers. Returnsfalse
ifn
is zero, one, or a composite number (one which can be factored into smaller positive integers).To test larger numbers, use
LongMath.isPrime(long)
orBigInteger.isProbablePrime(int)
.- Throws:
IllegalArgumentException
- ifn
is negative- Since:
- 20.0
-