BigIntegerMath Class | Guava | Java
BigIntegerMath is used to perform mathematical operations on BigInteger values. Basic standalone math functions are divided into the classes IntMath, LongMath, DoubleMath, and BigIntegerMath based on the primary numeric type involved. These classes have parallel structure, but each supports only the relevant subset of functions. Similar functionality for int and for long can be found in IntMath and LongMath respectively.
Declaration : The declaration for com.google.common.math.BigIntegerMath class is :
@GwtCompatible(emulated = true) public final class BigIntegerMath extends Object
Below table shows the methods provided by BigIntegerMath Class of Guava :

Exceptions :
- log2 : IllegalArgumentException if x <= 0
- log10 : IllegalArgumentException if x <= 0
- sqrt : IllegalArgumentException if x < 0
- divide : ArithmeticException if q == 0, or if mode == UNNECESSARY and a is not an integer multiple of b
- factorial : IllegalArgumentException if n < 0
- binomial : IllegalArgumentException if n < 0, k n
Example 1 :
// Java code to show implementation of // BigIntegerMath Class of Guava import java.math.*; import com.google.common.math.BigIntegerMath; class GFG { // Driver code public static void main(String args[]) { // Creating an object of GFG class GFG obj = new GFG(); // Function calling obj.examples(); } private void examples() { try { // exception will be thrown as 10 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 ex) { System.out.println("Error Message is : " + ex.getMessage()); } } } |
Error Message is : Rounding necessary
Example 2 :
// Java code to show implementation of // BigIntegerMath Class of Guava import java.math.*; import com.google.common.math.BigIntegerMath; class GFG { // Driver code public static void main(String args[]) { // Creating an object of GFG class GFG obj = new GFG(); // Function calling obj.examples(); } private void examples() { // As 10 is divisible by 5, so // no exception is thrown System.out.println(BigIntegerMath.divide(BigInteger.TEN, new BigInteger("5"), RoundingMode.UNNECESSARY)); // To compute log to base 10 System.out.println("Log10 is : " + BigIntegerMath.log10(new BigInteger("1000"), RoundingMode.HALF_EVEN)); // To compute factorial System.out.println("factorial is : " + BigIntegerMath.factorial(7)); // To compute log to base 2 System.out.println("Log2 is : " + BigIntegerMath.log2(new BigInteger("8"), RoundingMode.HALF_EVEN)); // To compute square root System.out.println("sqrt is : " + BigIntegerMath.sqrt(BigInteger. TEN.multiply(BigInteger.TEN), RoundingMode.HALF_EVEN)); } } |
2 Log10 is : 3 factorial is : 5040 Log2 is : 3 sqrt is : 10
Reference : Google Guava
Recommended Posts:
- Java.util.BitSet class methods in Java with Examples | Set 2
- Shadowing of static functions in Java
- How does default virtual behavior differ in C++ and Java ?
- How are Java objects stored in memory?
- How are parameters passed in Java?
- Are static local variables allowed in Java?
- final variables in Java
- Default constructor in Java
- Assigning values to static final variables in Java
- Comparison of Exception Handling in C++ and Java
- Does Java support goto?
- Arrays in Java
- Inheritance and constructors in Java
- More restrictive access to a derived class method in Java
- Comparison of static keyword in C++ and Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



