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 theirBigIntegercounterparts.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
intand forBigIntegercan be found inIntMathandBigIntegerMathrespectively. For other common operations onlongvalues, seeLongs.- Since:
- 11.0
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static longbinomial(int n, int k)Returnsnchoosek, also known as the binomial coefficient ofnandk, orLong.MAX_VALUEif the result does not fit in along.static longcheckedAdd(long a, long b)Returns the sum ofaandb, provided it does not overflow.static longcheckedMultiply(long a, long b)Returns the product ofaandb, provided it does not overflow.static longcheckedPow(long b, int k)Returns thebto thekth power, provided it does not overflow.static longcheckedSubtract(long a, long b)Returns the difference ofaandb, provided it does not overflow.static longdivide(long p, long q, java.math.RoundingMode mode)Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.static longfactorial(int n)Returnsn!, that is, the product of the firstnpositive integers,1ifn == 0, orLong.MAX_VALUEif the result does not fit in along.static longgcd(long a, long b)Returns the greatest common divisor ofa, b.static booleanisPowerOfTwo(long x)Returnstrueifxrepresents a power of two.static intlog10(long x, java.math.RoundingMode mode)Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.static intlog2(long x, java.math.RoundingMode mode)Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.static longmean(long x, long y)Returns the arithmetic mean ofxandy, rounded toward negative infinity.static intmod(long x, int m)Returnsx mod m.static longmod(long x, long m)Returnsx mod m.static longpow(long b, int k)Returnsbto thekth power.static longsqrt(long x, java.math.RoundingMode mode)Returns the square root ofx, rounded with the specified rounding mode.
-
-
-
Method Detail
-
isPowerOfTwo
public static boolean isPowerOfTwo(long x)
Returnstrueifxrepresents a power of two.This differs from
Long.bitCount(x) == 1, becauseLong.bitCount(Long.MIN_VALUE) == 1, butLong.MIN_VALUEis not a power of two.
-
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 <= 0java.lang.ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not a power of two
-
log10
@GwtIncompatible("TODO") 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 <= 0java.lang.ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandxis not a power of ten
-
pow
@GwtIncompatible("TODO") public static long pow(long b, int k)
Returnsbto thekth 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("TODO") 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 < 0java.lang.ArithmeticException- ifmodeisRoundingMode.UNNECESSARYandsqrt(x)is not an integer
-
divide
@GwtIncompatible("TODO") public static long divide(long p, long q, java.math.RoundingMode mode)
Returns the result of dividingpbyq, rounding using the specifiedRoundingMode.- Throws:
java.lang.ArithmeticException- ifq == 0, or ifmode == UNNECESSARYandais not an integer multiple ofb
-
mod
@GwtIncompatible("TODO") public static int mod(long x, int m)
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:
java.lang.ArithmeticException- ifm <= 0
-
mod
@GwtIncompatible("TODO") public static long mod(long x, long m)
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:
java.lang.ArithmeticException- ifm <= 0
-
gcd
public static long gcd(long a, long b)Returns the greatest common divisor ofa, b. Returns0ifa == 0 && b == 0.- Throws:
java.lang.IllegalArgumentException- ifa < 0orb < 0
-
checkedAdd
@GwtIncompatible("TODO") public static long checkedAdd(long a, long b)
Returns the sum ofaandb, provided it does not overflow.- Throws:
java.lang.ArithmeticException- ifa + boverflows in signedlongarithmetic
-
checkedSubtract
@GwtIncompatible("TODO") public static long checkedSubtract(long a, long b)
Returns the difference ofaandb, provided it does not overflow.- Throws:
java.lang.ArithmeticException- ifa - boverflows in signedlongarithmetic
-
checkedMultiply
@GwtIncompatible("TODO") public static long checkedMultiply(long a, long b)
Returns the product ofaandb, provided it does not overflow.- Throws:
java.lang.ArithmeticException- ifa * boverflows in signedlongarithmetic
-
checkedPow
@GwtIncompatible("TODO") public static long checkedPow(long b, int k)
Returns thebto thekth power, provided it does not overflow.- Throws:
java.lang.ArithmeticException- ifbto thekth power overflows in signedlongarithmetic
-
factorial
@GwtIncompatible("TODO") public static long factorial(int n)
Returnsn!, that is, the product of the firstnpositive integers,1ifn == 0, orLong.MAX_VALUEif the result does not fit in along.- Throws:
java.lang.IllegalArgumentException- ifn < 0
-
binomial
public static long binomial(int n, int k)Returnsnchoosek, also known as the binomial coefficient ofnandk, orLong.MAX_VALUEif the result does not fit in along.- Throws:
java.lang.IllegalArgumentException- ifn < 0,k < 0, ork > n
-
mean
public static long mean(long x, long y)Returns the arithmetic mean ofxandy, rounded toward negative infinity. This method is resilient to overflow.- Since:
- 14.0
-
-