Package com.google.common.math
Class LongMath
java.lang.Object
com.google.common.math.LongMath
@GwtCompatible(emulated=true)
@Deprecated(since="2022-12-01")
public final class LongMath
extends Object
Deprecated.
The Google Guava Core Libraries are deprecated and will not be part of the AEM SDK after April 2023
A class for arithmetic on values of type
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
-
Method Summary
Modifier and TypeMethodDescriptionstatic long
binomial
(int n, int k) Deprecated.Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orLong.MAX_VALUE
if the result does not fit in along
.static long
checkedAdd
(long a, long b) Deprecated.Returns the sum ofa
andb
, provided it does not overflow.static long
checkedMultiply
(long a, long b) Deprecated.Returns the product ofa
andb
, provided it does not overflow.static long
checkedPow
(long b, int k) Deprecated.Returns theb
to thek
th power, provided it does not overflow.static long
checkedSubtract
(long a, long b) Deprecated.Returns the difference ofa
andb
, provided it does not overflow.static long
divide
(long p, long q, RoundingMode mode) Deprecated.Returns the result of dividingp
byq
, rounding using the specifiedRoundingMode
.static long
factorial
(int n) Deprecated.Returnsn!
, that is, the product of the firstn
positive integers,1
ifn == 0
, orLong.MAX_VALUE
if the result does not fit in along
.static long
gcd
(long a, long b) Deprecated.Returns the greatest common divisor ofa, b
.static boolean
isPowerOfTwo
(long x) Deprecated.Returnstrue
ifx
represents a power of two.static int
log10
(long x, RoundingMode mode) Deprecated.Returns the base-10 logarithm ofx
, rounded according to the specified rounding mode.static int
log2
(long x, RoundingMode mode) Deprecated.Returns the base-2 logarithm ofx
, rounded according to the specified rounding mode.static long
mean
(long x, long y) Deprecated.Returns the arithmetic mean ofx
andy
, rounded toward negative infinity.static int
mod
(long x, int m) Deprecated.Returnsx mod m
.static long
mod
(long x, long m) Deprecated.Returnsx mod m
.static long
pow
(long b, int k) Deprecated.Returnsb
to thek
th power.static long
sqrt
(long x, RoundingMode mode) Deprecated.Returns the square root ofx
, rounded with the specified rounding mode.
-
Method Details
-
isPowerOfTwo
public static boolean isPowerOfTwo(long x) Deprecated.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. -
log2
Deprecated.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
Deprecated.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
-
pow
Deprecated.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:
IllegalArgumentException
- ifk < 0
-
sqrt
Deprecated.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
-
divide
Deprecated.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
Deprecated.Returnsx mod m
. This differs fromx % m
in 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:
ArithmeticException
- ifm <= 0
-
mod
Deprecated.Returnsx mod m
. This differs fromx % m
in 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:
ArithmeticException
- ifm <= 0
-
gcd
public static long gcd(long a, long b) Deprecated.Returns the greatest common divisor ofa, b
. Returns0
ifa == 0 && b == 0
.- Throws:
IllegalArgumentException
- ifa < 0
orb < 0
-
checkedAdd
Deprecated.Returns the sum ofa
andb
, provided it does not overflow.- Throws:
ArithmeticException
- ifa + b
overflows in signedlong
arithmetic
-
checkedSubtract
Deprecated.Returns the difference ofa
andb
, provided it does not overflow.- Throws:
ArithmeticException
- ifa - b
overflows in signedlong
arithmetic
-
checkedMultiply
Deprecated.Returns the product ofa
andb
, provided it does not overflow.- Throws:
ArithmeticException
- ifa * b
overflows in signedlong
arithmetic
-
checkedPow
Deprecated.Returns theb
to thek
th power, provided it does not overflow.- Throws:
ArithmeticException
- ifb
to thek
th power overflows in signedlong
arithmetic
-
factorial
Deprecated.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:
IllegalArgumentException
- ifn < 0
-
binomial
public static long binomial(int n, int k) Deprecated.Returnsn
choosek
, also known as the binomial coefficient ofn
andk
, orLong.MAX_VALUE
if the result does not fit in along
.- Throws:
IllegalArgumentException
- ifn < 0
,k < 0
, ork > n
-
mean
public static long mean(long x, long y) Deprecated.Returns the arithmetic mean ofx
andy
, rounded toward negative infinity. This method is resilient to overflow.- Since:
- 14.0
-