BigIntegerMath 类 |番石榴 |Java
BigIntegerMath用于对 BigInteger 值执行数学运算。基本的独立数学函数根据所涉及的主要数字类型分为IntMath、LongMath、DoubleMath 和 BigIntegerMath类。这些类具有并行结构,但每个仅支持相关的功能子集。 int 和 for long 的类似功能分别可以在 IntMath 和 LongMath 中找到。
声明: com.google.common.math.BigIntegerMath类的声明是:
@GwtCompatible(emulated = true)
public final class BigIntegerMath
extends Object
下表显示了 BigIntegerMath 的 Guava 类提供的方法:
例外:
- log2 : IllegalArgumentException如果 x <= 0
- log10 : IllegalArgumentException如果 x <= 0
- sqrt : IllegalArgumentException如果 x < 0
- 除 : ArithmeticException如果 q == 0,或者如果 mode == UNNECESSARY 并且 a 不是 b 的整数倍
- 阶乘: IllegalArgumentException如果 n < 0
- 二项式 : IllegalArgumentException如果 n < 0, kn
示例 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
示例 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
参考:谷歌番石榴