- Guava 教程
- Guava - 首页
- Guava - 概述
- Guava - 环境设置
- Guava - Optional 类
- Guava - Preconditions 类
- Guava - Ordering 类
- Guava - Objects 类
- Guava - Range 类
- Guava - Throwables 类
- Guava - 集合工具类
- Guava - 缓存工具类
- Guava - 字符串工具类
- Guava - 原语工具类
- Guava - 数学工具类
- Guava 有用资源
- Guava - 快速指南
- Guava - 有用资源
- Guava - 讨论
Guava - BigIntegerMath 类
BigIntegerMath 提供了关于 BigInteger 的实用方法。
类声明
以下是 com.google.common.math.BigIntegerMath 类的声明:
@GwtCompatible(emulated = true) public final class BigIntegerMath extends Object
方法
| 序号 | 方法及描述 |
|---|---|
| 1 |
static BigInteger binomial(int n, int k) 返回 n 选 k,也称为 n 和 k 的二项式系数,即 n! / (k! (n - k)!). |
| 2 | static BigInteger divide(BigInteger p, BigInteger q, RoundingMode mode) 返回 p 除以 q 的结果,使用指定的舍入模式进行舍入。 |
| 3 |
static BigInteger factorial(int n) 返回 n!,即前 n 个正整数的乘积,如果 n == 0 则返回 1。 |
| 4 |
static boolean isPowerOfTwo(BigInteger x) 如果 x 表示 2 的幂,则返回 true。 |
| 5 |
static int log10(BigInteger x, RoundingMode mode) 返回 x 的以 10 为底的对数,根据指定的舍入模式进行舍入。 |
| 6 |
static int log2(BigInteger x, RoundingMode mode) 返回 x 的以 2 为底的对数,根据指定的舍入模式进行舍入。 |
| 7 |
static BigInteger sqrt(BigInteger x, RoundingMode mode) 返回 x 的平方根,使用指定的舍入模式进行舍入。 |
继承的方法
此类继承自以下类:
- java.lang.Object
BigIntegerMath 类的示例
使用您选择的任何编辑器创建以下 Java 程序,例如在 C:/> Guava. 中。
GuavaTester.java
import java.math.BigInteger;
import java.math.RoundingMode;
import com.google.common.math.BigIntegerMath;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testBigIntegerMath();
}
private void testBigIntegerMath() {
System.out.println(BigIntegerMath.divide(BigInteger.TEN, new BigInteger("2"), RoundingMode.UNNECESSARY));
try {
//exception will be thrown as 100 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 e) {
System.out.println("Error: " + e.getMessage());
}
System.out.println("Log2(2): " + BigIntegerMath.log2(new BigInteger("2"), RoundingMode.HALF_EVEN));
System.out.println("Log10(10): " + BigIntegerMath.log10(BigInteger.TEN, RoundingMode.HALF_EVEN));
System.out.println("sqrt(100): " + BigIntegerMath.sqrt(BigInteger.TEN.multiply(BigInteger.TEN), RoundingMode.HALF_EVEN));
System.out.println("factorial(5): "+BigIntegerMath.factorial(5));
}
}
验证结果
使用 javac 编译器编译该类,如下所示:
C:\Guava>javac GuavaTester.java
现在运行 GuavaTester 以查看结果。
C:\Guava>java GuavaTester
查看结果。
5 Error: Rounding necessary Log2(2): 1 Log10(10): 1 sqrt(100): 10 factorial(5): 120
guava_math_utilities.htm
广告