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 - BigIntegerMath Class
BigIntegerMath provides utility methods on BigInteger.
Class Declaration
Following is the declaration for com.google.common.math.BigIntegerMath class −
@GwtCompatible(emulated = true) public final class BigIntegerMath extends Object
| Sr.No | Method & Description |
|---|---|
| 1 |
static BigInteger binomial(int n, int k) Returns n choose k, also known as the binomial coefficient of n and k, that is, n! / (k! (n - k)!). |
| 2 |
static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) Returns the result of dividing p by q, rounding using the specified RoundingMode. |
| 3 |
static BigInteger factorial(int n) Returns n!, that is, the product of the first n positive integers, or 1 if n == 0. |
| 4 |
static boolean isPowerOfTwo(BigInteger x) Returns true if x represents a power of two. |
| 5 |
static int log10(BigInteger x, RoundingMode mode) Returns the base-10 logarithm of x, rounded according to the specified rounding mode. |
| 6 |
static int log2(BigInteger x, RoundingMode mode) Returns the base-2 logarithm of x, rounded according to the specified rounding mode. |
| 7 |
static BigInteger sqrt(BigInteger 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 - BigInteger Divison
GuavaTester.java
package com.tutorialspoint;
import java.math.BigInteger;
import java.math.RoundingMode;
import com.google.common.math.BigIntegerMath;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testBigIntegerMath();
}
private void testBigIntegerMath() {
System.out.println(BigIntegerMath.divide(BigInteger.TEN, new BigInteger("2"), 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(BigIntegerMath.divide(BigInteger.TEN, new BigInteger("3"), RoundingMode.UNNECESSARY));
} catch(ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output
Run the GuavaTester and verify the result.
5 Error: Rounding necessary
Example - BigInteger Log Operations
GuavaTester.java
package com.tutorialspoint;
import java.math.BigInteger;
import java.math.RoundingMode;
import com.google.common.math.BigIntegerMath;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testBigIntegerMath();
}
private void testBigIntegerMath() {
System.out.println("Log2(2): " + BigIntegerMath.log2(new BigInteger("2"), RoundingMode.HALF_EVEN));
System.out.println("Log10(10): " + BigIntegerMath.log10(BigInteger.TEN, RoundingMode.HALF_EVEN));
}
}
Output
Run the GuavaTester and verify the result.
Log2(2): 1 Log10(10): 1
Example - BigInteger SQRT/Factorial Operations
GuavaTester.java
package com.tutorialspoint;
import java.math.BigInteger;
import java.math.RoundingMode;
import com.google.common.math.BigIntegerMath;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testBigIntegerMath();
}
private void testBigIntegerMath() {
System.out.println("sqrt(100): " + BigIntegerMath.sqrt(BigInteger.TEN.multiply(BigInteger.TEN), RoundingMode.HALF_EVEN));
System.out.println("factorial(5): "+BigIntegerMath.factorial(5));
}
}
Output
Run the GuavaTester and verify the result.
sqrt(100): 10 factorial(5): 120