📜  BigIntegerMath divide()函数|番石榴 |Java

📅  最后修改于: 2022-05-13 01:54:55.410000             🧑  作者: Mango

BigIntegerMath divide()函数|番石榴 |Java

Guava 的 BigIntegerMath 类的方法divide(BigInteger p, BigInteger q, RoundingMode mode)返回 p 除以 q 的结果,使用指定的 RoundingMode 进行舍入。

句法:

public static BigInteger divide(BigInteger p,
                                BigInteger q,
                                RoundingMode mode)

参数:此方法采用以下参数:

  • p : BigInteger 红利
  • q : BigInteger 除数
  • mode : 计算 p 和 q 相除的舍入模式。

返回值:该方法返回p除以q的结果,使用指定的RoundingMode进行舍入。

异常:如果 q == 0,或者如果 mode == UNNECESSARY 并且 a 不是 b 的整数倍,则该方法抛出ArithmeticException

枚举舍入模式

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.

下面的示例说明了 BigIntegerMath.divide() 方法:

示例 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);
    }
}
输出:
55 divided by 10 with HALF_DOWN rounding mode: 5
55 divided by 10 with CEILING rounding mode: 6

示例 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);
        }
    }
}
输出:
Exception: java.lang.ArithmeticException: / by zero

参考: 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-