BigIntegerMath divide() function | Guava | Java Last Updated : 15 Nov, 2018 Comments Improve Suggest changes Like Article Like Report The method divide(BigInteger p, BigInteger q, RoundingMode mode) of Guava's BigIntegerMath class returns the result of dividing p by q, rounding using the specified RoundingMode. Syntax: public static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) Parameters: This method takes the following parameters: p: The BigInteger dividend q: The BigInteger divisor mode: The rounding mode for calculating the division of p and q. Return Value: This method returns the result of dividing p by q, rounding using the specified RoundingMode. Exceptions: The method throws ArithmeticException if q == 0, or if mode == UNNECESSARY and a is not an integer multiple of b. Enum RoundingMode Enum Constant Description CEILING Rounding mode to round towards positive infinity. DOWN Rounding mode to round towards zero. FLOOR Rounding mode to round towards negative infinity. HALF_DOWN Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round down. HALF_EVEN Rounding mode to round towards the "nearest neighbor" unless both neighbors are equidistant, in which case, round towards the even neighbor. HALF_UP Rounding mode to round towards "nearest neighbor" unless both neighbors are equidistant, in which case round up. UNNECESSARY Rounding mode to assert that the requested operation has an exact result, hence no rounding is necessary. UP Rounding mode to round away from zero. Below examples illustrates the BigIntegerMath.divide() method: Example 1: Java // Java code to show implementation of // divide(BigInteger p, BigInteger q, RoundingMode mode) // method of Guava's BigIntegerMath class import java.math.*; import com.google.common.math.BigIntegerMath; class GFG { // Driver code public static void main(String args[]) { BigInteger dividend1 = BigInteger.valueOf(55); BigInteger divisor1 = BigInteger.valueOf(10); // Using divide() // method of Guava's BigIntegerMath class BigInteger quotient1 = BigIntegerMath .divide(dividend1, divisor1, RoundingMode.HALF_DOWN); System.out.println(dividend1 + " divided by " + divisor1 + " with HALF_DOWN rounding mode: " + quotient1); BigInteger dividend2 = BigInteger.valueOf(55); BigInteger divisor2 = BigInteger.valueOf(10); // Using divide() // method of Guava's BigIntegerMath class BigInteger quotient2 = BigIntegerMath .divide(dividend2, divisor2, RoundingMode.CEILING); System.out.println(dividend2 + " divided by " + divisor2 + " with CEILING rounding mode: " + quotient2); } } Output: 55 divided by 10 with HALF_DOWN rounding mode: 5 55 divided by 10 with CEILING rounding mode: 6 Example 2: Java // Java code to show implementation of // divide(BigInteger p, BigInteger q, RoundingMode mode) // method of Guava's BigIntegerMath class import java.math.*; import com.google.common.math.BigIntegerMath; class GFG { // Driver code public static void main(String args[]) { try { BigInteger dividend1 = BigInteger.valueOf(25); BigInteger divisor1 = BigInteger.valueOf(0); // Using divide() // method of Guava's BigIntegerMath class // This should raise "ArithmeticException" // as divisor1 = 0 BigInteger quotient1 = BigIntegerMath .divide(dividend1, divisor1, RoundingMode.HALF_DOWN); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: Exception: java.lang.ArithmeticException: / by zero Reference: https://google.github.io/guava/releases/21.0/api/docs/com/google/common/math/BigIntegerMath.html#divide-java.math.BigInteger-java.math.BigInteger-java.math.RoundingMode- Comment More infoAdvertise with us Next Article BigIntegerMath divide() function | Guava | Java S Sahil_Bansall Follow Improve Article Tags : Java java-guava Java-BigInteger Practice Tags : JavaJava-BigInteger Similar Reads BigIntegerMath binomial() function | Guava | Java The binomial(int n, int k) method of Guava's BigIntegerMath class returns n choose k, also known as the binomial coefficient of n and k, that is, n! / (k! (n - k)!) Syntax: public static BigInteger binomial(int n, int k) Parameters: This method takes the following parameters: n: The base for binomia 2 min read BigIntegerMath sqrt() function | Guava | Java The method sqrt(BigInteger x, RoundingMode mode) of Guava's BigIntegerMath class returns the square root of x, rounded with the specified rounding mode. Syntax: public static BigInteger sqrt(BigInteger x, RoundingMode mode) Parameters: This method takes the following parameters: x : The BigInteger n 3 min read BigIntegerMath log2() function | Guava | Java The method log2(BigInteger x, RoundingMode mode) of Guava's BigIntegerMath class returns the base-2 logarithm of x, rounded according to the specified rounding mode. Syntax: public static int log2(BigInteger x, RoundingMode mode) Parameters: This method takes the following parameters: x: The BigInte 3 min read BigIntegerMath log10() function | Guava | Java The method log10(BigInteger x, RoundingMode mode) of Guava's BigIntegerMath class returns the base-10 logarithm of x, rounded according to the specified rounding mode. Syntax: public static int log10(BigInteger x, RoundingMode mode) Parameters: This method takes the following parameters: x: The BigI 3 min read BigIntegerMath ceilingPowerOfTwo() function | Guava | Java The ceilingPowerOfTwo(BigInteger x) method of Guava's BigIntegerMath class returns the smallest power of two greater than or equal to x. This is equivalent to BigInteger.valueOf(2).pow(log2(x, CEILING)). Syntax: public static BigInteger ceilingPowerOfTwo(BigInteger x) Parameters: This method takes t 2 min read Like