Class LongMath
long. 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 int and for BigInteger can be found in IntMath and BigIntegerMath respectively. For other common operations on long
 values, see Longs.
- Since:
- 11.0
- Author:
- Louis Wasserman
- 
Method SummaryModifier and TypeMethodDescriptionstatic longbinomial(int n, int k) Returnsnchoosek, also known as the binomial coefficient ofnandk, orLong.MAX_VALUEif the result does not fit in along.static longceilingPowerOfTwo(long x) Returns the smallest power of two greater than or equal tox.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, 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 longfloorPowerOfTwo(long x) Returns the largest power of two less than or equal tox.static longgcd(long a, long b) Returns the greatest common divisor ofa, b.static booleanisPowerOfTwo(long x) Returnstrueifxrepresents a power of two.static booleanisPrime(long n) Returnstrueifnis a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers.static intlog10(long x, RoundingMode mode) Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.static intlog2(long x, 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, a non-negative value less thanm.static longmod(long x, long m) Returnsx mod m, a non-negative value less thanm.static longpow(long b, int k) Returnsbto thekth power.static doubleroundToDouble(long x, RoundingMode mode) Returnsx, rounded to adoublewith the specified rounding mode.static longsaturatedAbs(long x) Returns the closest representablelongto the absolute value ofx.static longsaturatedAdd(long a, long b) Returns the sum ofaandbunless it would overflow or underflow in which caseLong.MAX_VALUEorLong.MIN_VALUEis returned, respectively.static longsaturatedMultiply(long a, long b) Returns the product ofaandbunless it would overflow or underflow in which caseLong.MAX_VALUEorLong.MIN_VALUEis returned, respectively.static longsaturatedPow(long b, int k) Returns thebto thekth power, unless it would overflow or underflow in which caseLong.MAX_VALUEorLong.MIN_VALUEis returned, respectively.static longsaturatedSubtract(long a, long b) Returns the difference ofaandbunless it would overflow or underflow in which caseLong.MAX_VALUEorLong.MIN_VALUEis returned, respectively.static longsqrt(long x, RoundingMode mode) Returns the square root ofx, rounded with the specified rounding mode.
- 
Method Details- 
ceilingPowerOfTwopublic 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:
- IllegalArgumentException- if- x <= 0
- ArithmeticException- of the next-higher power of two is not representable as a- long, i.e. when- x > 2^62
- Since:
- 20.0
 
- 
floorPowerOfTwopublic 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:
- IllegalArgumentException- if- x <= 0
- Since:
- 20.0
 
- 
isPowerOfTwopublic 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.
- 
log2Returns the base-2 logarithm ofx, rounded according to the specified rounding mode.- Throws:
- IllegalArgumentException- if- x <= 0
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- xis not a power of two
 
- 
log10Returns the base-10 logarithm ofx, rounded according to the specified rounding mode.- Throws:
- IllegalArgumentException- if- x <= 0
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- xis not a power of ten
 
- 
powReturnsbto 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:
- IllegalArgumentException- if- k < 0
 
- 
sqrtReturns the square root ofx, rounded with the specified rounding mode.- Throws:
- IllegalArgumentException- if- x < 0
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- sqrt(x)is not an integer
 
- 
divideReturns the result of dividingpbyq, rounding using the specifiedRoundingMode. If theRoundingModeisRoundingMode.DOWN, then this method is equivalent to regular Java division,p / q; and if it isRoundingMode.FLOOR, then this method is equivalent toMath.floorDiv(p, q).- Throws:
- ArithmeticException- if- q == 0, or if- mode == UNNECESSARYand- ais not an integer multiple of- b
 
- 
modReturnsx 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- if- m <= 0
- See Also:
 
- 
modReturnsx 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- if- m <= 0
- See Also:
 
- 
gcdpublic static long gcd(long a, long b) Returns the greatest common divisor ofa, b. Returns0ifa == 0 && b == 0.- Throws:
- IllegalArgumentException- if- a < 0or- b < 0
 
- 
checkedAddReturns the sum ofaandb, provided it does not overflow.Note: this method is now unnecessary and should be treated as deprecated; use Math.addExact(long, long)instead. Note that if both arguments areintvalues, writingMath.addExact(a, b)will call theMath.addExact(int, int)overload, notMath.addExact(long, long). Also note that adding twointvalues can never overflow along, so you can just write(long) a + b.- Throws:
- ArithmeticException- if- a + boverflows in signed- longarithmetic
 
- 
checkedSubtract@InlineMe(replacement="Math.subtractExact(a, b)") public static long checkedSubtract(long a, long b) Returns the difference ofaandb, provided it does not overflow.Note: this method is now unnecessary and should be treated as deprecated; use Math.subtractExact(long, long)instead. Note that if both arguments areintvalues, writingMath.subtractExact(a, b)will call theMath.subtractExact(int, int)overload, notMath.subtractExact(long, long). Also note that subtracting twointvalues can never overflow along, so you can just write(long) a - b.- Throws:
- ArithmeticException- if- a - boverflows in signed- longarithmetic
 
- 
checkedMultiply@InlineMe(replacement="Math.multiplyExact(a, b)") public static long checkedMultiply(long a, long b) Returns the product ofaandb, provided it does not overflow.Note: this method is now unnecessary and should be treated as deprecated; use Math.multiplyExact(long, long)instead. Note that if both arguments areintvalues, writingMath.multiplyExact(a, b)will call theMath.multiplyExact(int, int)overload, notMath.multiplyExact(long, long). Also note that multiplying twointvalues can never overflow along, so you can just write(long) a * b.- Throws:
- ArithmeticException- if- a * boverflows in signed- longarithmetic
 
- 
checkedPowReturns thebto thekth power, provided it does not overflow.- Throws:
- ArithmeticException- if- bto the- kth power overflows in signed- longarithmetic
 
- 
saturatedAddpublic static long saturatedAdd(long a, long b) Returns the sum ofaandbunless it would overflow or underflow in which caseLong.MAX_VALUEorLong.MIN_VALUEis returned, respectively.- Since:
- 20.0
 
- 
saturatedSubtractpublic static long saturatedSubtract(long a, long b) Returns the difference ofaandbunless it would overflow or underflow in which caseLong.MAX_VALUEorLong.MIN_VALUEis returned, respectively.- Since:
- 20.0
 
- 
saturatedMultiplypublic static long saturatedMultiply(long a, long b) Returns the product ofaandbunless it would overflow or underflow in which caseLong.MAX_VALUEorLong.MIN_VALUEis returned, respectively.- Since:
- 20.0
 
- 
saturatedPowpublic static long saturatedPow(long b, int k) Returns thebto thekth power, unless it would overflow or underflow in which caseLong.MAX_VALUEorLong.MIN_VALUEis returned, respectively.- Since:
- 20.0
 
- 
factorialReturnsn!, that is, the product of the firstnpositive integers,1ifn == 0, orLong.MAX_VALUEif the result does not fit in along.- Throws:
- IllegalArgumentException- if- n < 0
 
- 
binomialpublic 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:
- IllegalArgumentException- if- n < 0,- k < 0, or- k > n
 
- 
meanpublic 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
 
- 
isPrimeReturnstrueifnis a prime number: an integer greater than one that cannot be factored into a product of smaller positive integers. Returnsfalseifnis zero, one, or a composite number (one which can be factored into smaller positive integers).To test larger numbers, use BigInteger.isProbablePrime(int).- Throws:
- IllegalArgumentException- if- nis negative
- Since:
- 20.0
 
- 
roundToDoubleReturnsx, rounded to adoublewith the specified rounding mode. Ifxis precisely representable as adouble, itsdoublevalue will be returned; otherwise, the rounding will choose between the two nearest representable values withmode.For the case of RoundingMode.HALF_EVEN, this implementation uses the IEEE 754 default rounding mode: if the two nearest representable values are equally near, the one with the least significant bit zero is chosen. (In such cases, both of the nearest representable values are even integers; this method returns the one that is a multiple of a greater power of two.)- Throws:
- ArithmeticException- if- modeis- RoundingMode.UNNECESSARYand- xis not precisely representable as a- double
- Since:
- 30.0
 
- 
saturatedAbspublic static long saturatedAbs(long x) Returns the closest representablelongto the absolute value ofx.This is the same thing as the true absolute value of xexcept in the case whenxisLong.MIN_VALUE, in which case this returnsLong.MAX_VALUE. (Note thatLong.MAX_VALUEis mathematically equal to-Long.MIN_VALUE - 1.)There are three common APIs for determining the absolute value of a long, all of which behave identically except when passed Long.MIN_VALUE. Those methods are:- Math.abs(long), which returns- Long.MIN_VALUEwhen passed- Long.MIN_VALUE
- Math.absExact(long), which throws- ArithmeticExceptionwhen passed- Long.MIN_VALUE
- this method, LongMath.saturatedAbs(long), which returnsLong.MAX_VALUEwhen passedLong.MIN_VALUE
 Note that if your only goal is to turn a well-distributed `long` (such as a random number) into a well-distributed nonnegative number, the most even distribution is achieved not by this method or other absolute value methods, but by x & Long.MAX_VALUE.- Since:
- 33.5.0
 
 
-