Skip to content
Related Articles

Related Articles

Improve Article

BigIntegerMath divide() function | Guava | Java

  • Last Updated : 15 Nov, 2018

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 ConstantDescription
CEILINGRounding mode to round towards positive infinity.
DOWNRounding mode to round towards zero.
FLOORRounding mode to round towards negative infinity.
HALF_DOWNRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round down.
HALF_EVENRounding mode to round towards the “nearest neighbor” unless both neighbors are equidistant, in which case, round towards the even neighbor.
HALF_UPRounding mode to round towards “nearest neighbor” unless both neighbors are equidistant, in which case round up.
UNNECESSARYRounding mode to assert that the requested operation has an exact result, hence no rounding is necessary.
UPRounding mode to round away from zero.

Below examples illustrates the BigIntegerMath.divide() method:

Example 1:




// 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 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-

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :