Package com.google.common.math
Class LongMath
- java.lang.Object
-
- com.google.common.math.LongMath
-
@GwtCompatible(emulated=true) public final class LongMath extends java.lang.Object
A class for arithmetic on values of typelong
. 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
int
and forBigInteger
can be found inIntMath
andBigIntegerMath
respectively. For other common operations onlong
values, seeLongs
.- Since:
- 11.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
LongMath.MillerRabinTester
-
Field Summary
Fields Modifier and Type Field Description (package private) static int[]
biggestBinomials
(package private) static int[]
biggestSimpleBinomials
(package private) static long[]
factorials
(package private) static long
FLOOR_SQRT_MAX_LONG
(package private) static long[]
halfPowersOf10
(package private) static long
MAX_POWER_OF_SQRT2_UNSIGNED
The biggest half power of two that fits into an unsigned long(package private) static long
MAX_SIGNED_POWER_OF_TWO
(package private) static byte[]
maxLog10ForLeadingZeros
private static long[][]
millerRabinBaseSets
(package private) static long[]
powersOf10
private static int
SIEVE_30
-
Constructor Summary
Constructors Modifier Constructor Description private
LongMath()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
binomial(int n, int k)
Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orLong.MAX_VALUE
if the result does not fit in along
.static long
ceilingPowerOfTwo(long x)
Returns the smallest power of two greater than or equal tox
.static long
checkedAdd(long a, long b)
Returns the sum ofa
andb
, provided it does not overflow.static long
checkedMultiply(long a, long b)
Returns the product ofa
andb
, provided it does not overflow.static long
checkedPow(long b, int k)
Returns theb
to thek
th power, provided it does not overflow.static long
checkedSubtract(long a, long b)
Returns the difference ofa
andb
, provided it does not overflow.static long
divide(long p, long q, java.math.RoundingMode mode)
Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.static long
factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orLong.MAX_VALUE
if the result does not fit in along
.(package private) static boolean
fitsInInt(long x)
static long
floorPowerOfTwo(long x)
Returns the largest power of two less than or equal tox
.static long
gcd(long a, long b)
Returns the greatest common divisor ofa, b
.static boolean
isPowerOfTwo(long x)
Returnstrue
ifx
represents a power of two.static boolean
isPrime(long 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(long x, long y)
Returns 1 ifx < y
as unsigned longs, and 0 otherwise.static int
log10(long x, java.math.RoundingMode mode)
Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.(package private) static int
log10Floor(long x)
static int
log2(long x, java.math.RoundingMode mode)
Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.static long
mean(long x, long y)
Returns the arithmetic mean ofx
andy
, rounded toward negative infinity.static int
mod(long x, int m)
Returnsx mod m
, a non-negative value less thanm
.static long
mod(long x, long m)
Returnsx mod m
, a non-negative value less thanm
.(package private) static long
multiplyFraction(long x, long numerator, long denominator)
Returns (x * numerator / denominator), which is assumed to come out to an integral value.static long
pow(long b, int k)
Returnsb
to thek
th power.static long
saturatedAdd(long a, long b)
Returns the sum ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.static long
saturatedMultiply(long a, long b)
Returns the product ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.static long
saturatedPow(long b, int k)
Returns theb
to thek
th power, unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.static long
saturatedSubtract(long a, long b)
Returns the difference ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.static long
sqrt(long x, java.math.RoundingMode mode)
Returns the square root ofx
, rounded with the specified rounding mode.
-
-
-
Field Detail
-
MAX_SIGNED_POWER_OF_TWO
static final long MAX_SIGNED_POWER_OF_TWO
- See Also:
- Constant Field Values
-
MAX_POWER_OF_SQRT2_UNSIGNED
static final long MAX_POWER_OF_SQRT2_UNSIGNED
The biggest half power of two that fits into an unsigned long- See Also:
- Constant Field Values
-
maxLog10ForLeadingZeros
static final byte[] maxLog10ForLeadingZeros
-
powersOf10
@GwtIncompatible static final long[] powersOf10
-
halfPowersOf10
@GwtIncompatible static final long[] halfPowersOf10
-
FLOOR_SQRT_MAX_LONG
static final long FLOOR_SQRT_MAX_LONG
- See Also:
- Constant Field Values
-
factorials
static final long[] factorials
-
biggestBinomials
static final int[] biggestBinomials
-
biggestSimpleBinomials
static final int[] biggestSimpleBinomials
-
SIEVE_30
private static final int SIEVE_30
- See Also:
- Constant Field Values
-
millerRabinBaseSets
private static final long[][] millerRabinBaseSets
-
-
Method Detail
-
ceilingPowerOfTwo
@Beta public static long ceilingPowerOfTwo(long x)
Returns the smallest power of two greater than or equal tox
. This is equivalent tocheckedPow(2, log2(x, CEILING))
.- Throws:
java.lang.IllegalArgumentException
- ifx <= 0
java.lang.ArithmeticException
- of the next-higher power of two is not representable as along
, i.e. whenx > 2^62
- Since:
- 20.0
-
floorPowerOfTwo
@Beta public static long floorPowerOfTwo(long x)
Returns the largest power of two less than or equal tox
. This is equivalent tocheckedPow(2, log2(x, FLOOR))
.- Throws:
java.lang.IllegalArgumentException
- ifx <= 0
- Since:
- 20.0
-
isPowerOfTwo
public static boolean isPowerOfTwo(long x)
Returnstrue
ifx
represents a power of two.This differs from
Long.bitCount(x) == 1
, becauseLong.bitCount(Long.MIN_VALUE) == 1
, butLong.MIN_VALUE
is not a power of two.
-
lessThanBranchFree
static int lessThanBranchFree(long x, long y)
Returns 1 ifx < y
as unsigned longs, and 0 otherwise. Assumes that x - y fits into a signed long. The implementation is branch-free, and benchmarks suggest it is measurably faster than the straightforward ternary expression.
-
log2
public static int log2(long 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 public static int log10(long 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
-
log10Floor
@GwtIncompatible static int log10Floor(long x)
-
pow
@GwtIncompatible public static long pow(long b, int k)
Returnsb
to thek
th power. Even if the result overflows, it will be equal toBigInteger.valueOf(b).pow(k).longValue()
. This implementation runs inO(log k)
time.- Throws:
java.lang.IllegalArgumentException
- ifk < 0
-
sqrt
@GwtIncompatible public static long sqrt(long 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
@GwtIncompatible public static long divide(long p, long 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
@GwtIncompatible public static int mod(long 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
-
mod
@GwtIncompatible public static long mod(long x, long 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 long gcd(long a, long b)
Returns the greatest common divisor ofa, b
. Returns0
ifa == 0 && b == 0
.- Throws:
java.lang.IllegalArgumentException
- ifa < 0
orb < 0
-
checkedAdd
@GwtIncompatible public static long checkedAdd(long a, long b)
Returns the sum ofa
andb
, provided it does not overflow.- Throws:
java.lang.ArithmeticException
- ifa + b
overflows in signedlong
arithmetic
-
checkedSubtract
@GwtIncompatible public static long checkedSubtract(long a, long b)
Returns the difference ofa
andb
, provided it does not overflow.- Throws:
java.lang.ArithmeticException
- ifa - b
overflows in signedlong
arithmetic
-
checkedMultiply
public static long checkedMultiply(long a, long b)
Returns the product ofa
andb
, provided it does not overflow.- Throws:
java.lang.ArithmeticException
- ifa * b
overflows in signedlong
arithmetic
-
checkedPow
@GwtIncompatible public static long checkedPow(long b, int k)
Returns theb
to thek
th power, provided it does not overflow.- Throws:
java.lang.ArithmeticException
- ifb
to thek
th power overflows in signedlong
arithmetic
-
saturatedAdd
@Beta public static long saturatedAdd(long a, long b)
Returns the sum ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.- Since:
- 20.0
-
saturatedSubtract
@Beta public static long saturatedSubtract(long a, long b)
Returns the difference ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.- Since:
- 20.0
-
saturatedMultiply
@Beta public static long saturatedMultiply(long a, long b)
Returns the product ofa
andb
unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.- Since:
- 20.0
-
saturatedPow
@Beta public static long saturatedPow(long b, int k)
Returns theb
to thek
th power, unless it would overflow or underflow in which caseLong.MAX_VALUE
orLong.MIN_VALUE
is returned, respectively.- Since:
- 20.0
-
factorial
@GwtIncompatible public static long factorial(int n)
Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orLong.MAX_VALUE
if the result does not fit in along
.- Throws:
java.lang.IllegalArgumentException
- ifn < 0
-
binomial
public static long binomial(int n, int k)
Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orLong.MAX_VALUE
if the result does not fit in along
.- Throws:
java.lang.IllegalArgumentException
- ifn < 0
,k < 0
, ork > n
-
multiplyFraction
static long multiplyFraction(long x, long numerator, long denominator)
Returns (x * numerator / denominator), which is assumed to come out to an integral value.
-
fitsInInt
static boolean fitsInInt(long x)
-
mean
public static long mean(long x, long y)
Returns the arithmetic mean ofx
andy
, rounded toward negative infinity. This method is resilient to overflow.- Since:
- 14.0
-
isPrime
@GwtIncompatible @Beta public static boolean isPrime(long 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
BigInteger.isProbablePrime(int)
.- Throws:
java.lang.IllegalArgumentException
- ifn
is negative- Since:
- 20.0
-
-