Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
Guava - LongMath Class
LongMath provides utility methods on long.
Class Declaration
Following is the declaration for com.google.common.math.LongMath class −
@GwtCompatible(emulated = true) public final class LongMath extends Object
| Sr.No | Method & Description |
|---|---|
| 1 |
static long binomial(int n, int k) Returns n choose k, also known as the binomial coefficient of n and k, or Long.MAX_VALUE if the result does not fit in a long. |
| 2 |
static long checkedAdd(long a, long b) Returns the sum of a and b, provided it does not overflow. |
| 3 |
static long checkedMultiply(long a, long b) Returns the product of a and b, provided it does not overflow. |
| 4 |
static long checkedPow(long b, int k) Returns the b to the kth power, provided it does not overflow. |
| 5 |
static long checkedSubtract(long a, long b) Returns the difference of a and b, provided it does not overflow. |
| 6 |
static long divide(long p, long q, RoundingMode mode) Returns the result of dividing p by q, rounding using the specified RoundingMode. |
| 7 |
static long factorial(int n) Returns n!, that is, the product of the first n positive integers, 1 if n == 0, or Long.MAX_VALUE if the result does not fit in a long. |
| 8 |
static long gcd(long a, long b) Returns the greatest common divisor of a, b. |
| 9 |
static boolean isPowerOfTwo(long x) Returns true if x represents a power of two. |
| 10 |
static int log10(long x, RoundingMode mode) Returns the base-10 logarithm of x, rounded according to the specified rounding mode. |
| 11 |
static int log2(long x, RoundingMode mode) Returns the base-2 logarithm of x, rounded according to the specified rounding mode. |
| 12 |
static long mean(long x, long y) Returns the arithmetic mean of x and y, rounded toward negative infinity. |
| 13 |
static int mod(long x, int m) Returns x mod m, a non-negative value less than m. |
| 14 |
static long mod(long x, long m) Returns x mod m, a non-negative value less than m. |
| 15 |
static long pow(long b, int k) Returns b to the kth power. |
| 16 |
static long sqrt(long x, RoundingMode mode) Returns the square root of x, rounded with the specified rounding mode. |
Methods Inherited
This class inherits methods from the following class −
- java.lang.Object
Example - Checked Operations of longs
GuavaTester.java
package com.tutorialspoint;
import java.math.RoundingMode;
import com.google.common.math.LongMath;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testLongMath();
}
private void testLongMath() {
try {
System.out.println(LongMath.checkedAdd(Long.MAX_VALUE, Long.MAX_VALUE));
} catch(ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
System.out.println(LongMath.divide(100L, 5L, RoundingMode.UNNECESSARY));
try {
//exception will be thrown as 100 is not completely divisible by 3
// thus rounding is required, and RoundingMode is set as UNNESSARY
System.out.println(LongMath.divide(100L, 3L, RoundingMode.UNNECESSARY));
} catch(ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output
Run the GuavaTester and verify the output −
Error: overflow: checkedAdd(9223372036854775807, 9223372036854775807) 20 Error: mode was UNNECESSARY, but rounding was necessary
Example - Computing operations on Longs
GuavaTester.java
package com.tutorialspoint;
import java.math.RoundingMode;
import com.google.common.math.LongMath;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testLongMath();
}
private void testLongMath() {
System.out.println("Log2(2L): " + LongMath.log2(2L, RoundingMode.HALF_EVEN));
System.out.println("Log10(10L): " + LongMath.log10(10L, RoundingMode.HALF_EVEN));
System.out.println("sqrt(100L): " + LongMath.sqrt(LongMath.pow(10L,2), RoundingMode.HALF_EVEN));
}
}
Output
Run the GuavaTester and verify the output −
Log2(2): 1 Log10(10): 1 sqrt(100): 10
Example - More Operations on Longs
GuavaTester.java
package com.tutorialspoint;
import com.google.common.math.LongMath;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testLongMath();
}
private void testLongMath() {
System.out.println("gcd(100L,50L): " + LongMath.gcd(100L,50L));
System.out.println("modulus(100L,50L): " + LongMath.mod(100L,50L));
System.out.println("factorial(5): " + LongMath.factorial(5L));
}
}
Output
Run the GuavaTester and verify the output −
gcd(100,50): 50 modulus(100,50): 0 factorial(5): 120