java.math 包详解
java.math
包是 Java 提供的用于高精度数学计算的工具包,主要包含两个核心类:BigInteger
和 BigDecimal
。这些类用于处理超出基本数据类型范围的数值运算。
1. BigInteger 类
BigInteger
用于表示任意大小的整数,没有理论上的大小限制(受限于内存)。
主要特性
-
不可变(immutable)类
-
支持任意精度的整数运算
-
提供模运算、GCD计算、素数测试等高级功能
常用构造方法
BigInteger(String val) // 通过字符串构造
BigInteger(byte[] val) // 通过字节数组构造
常用方法
// 算术运算
BigInteger add(BigInteger val) // 加法
BigInteger subtract(BigInteger val) // 减法
BigInteger multiply(BigInteger val) // 乘法
BigInteger divide(BigInteger val) // 除法
BigInteger mod(BigInteger m) // 取模
BigInteger pow(int exponent) // 幂运算// 比较运算
int compareTo(BigInteger val) // 比较
boolean equals(Object x) // 相等判断// 位运算
BigInteger and(BigInteger val) // 与
BigInteger or(BigInteger val) // 或
BigInteger not() // 非
BigInteger shiftLeft(int n) // 左移
BigInteger shiftRight(int n) // 右移// 其他
BigInteger gcd(BigInteger val) // 最大公约数
boolean isProbablePrime(int certainty) // 素数测试
BigInteger abs() // 绝对值
BigInteger negate() // 取负
示例
BigInteger a = new BigInteger("12345678901234567890");
BigInteger b = new BigInteger("98765432109876543210");BigInteger sum = a.add(b);
BigInteger product = a.multiply(b);
BigInteger gcd = a.gcd(b);
2. BigDecimal 类
BigDecimal
用于高精度的浮点数运算,解决了 double
和 float
类型的精度问题。
主要特性
-
不可变(immutable)类
-
任意精度的有符号十进制数
-
提供对舍入行为的完全控制
-
适合财务计算等需要精确结果的场景
常用构造方法
BigDecimal(String val) // 通过字符串构造(推荐)
BigDecimal(double val) // 通过double构造(不推荐)
BigDecimal(BigInteger val) // 通过BigInteger构造
常用方法
// 算术运算
BigDecimal add(BigDecimal val) // 加法
BigDecimal subtract(BigDecimal val) // 减法
BigDecimal multiply(BigDecimal val) // 乘法
BigDecimal divide(BigDecimal val) // 除法(需要指定舍入模式)
BigDecimal pow(int n) // 幂运算// 舍入控制
BigDecimal setScale(int newScale, RoundingMode roundingMode) // 设置小数位数// 比较运算
int compareTo(BigDecimal val) // 比较
boolean equals(Object x) // 相等判断(同时比较值和精度)// 其他
BigDecimal abs() // 绝对值
BigDecimal negate() // 取负
BigDecimal stripTrailingZeros() // 移除末尾的零
舍入模式(RoundingMode)
Java 提供了多种舍入模式:RoundingMode.UP - 远离零方向舍入RoundingMode.DOWN - 向零方向舍入RoundingMode.CEILING - 向正无穷方向舍入RoundingMode.FLOOR - 向负无穷方向舍入RoundingMode.HALF_UP - 四舍五入RoundingMode.HALF_DOWN - 五舍六入RoundingMode.HALF_EVEN - 银行家舍入法
示例
BigDecimal d1 = new BigDecimal("0.1");
BigDecimal d2 = new BigDecimal("0.2");BigDecimal sum = d1.add(d2); // 精确结果为0.3BigDecimal d3 = new BigDecimal("1.0");
BigDecimal d4 = new BigDecimal("3.0");// 除法必须指定舍入模式
BigDecimal result = d3.divide(d4, 4, RoundingMode.HALF_UP); // 0.3333
3. MathContext 类
MathContext
封装了精度和舍入模式的上下文设置,可用于 BigDecimal
运算。
常用预定义上下文
-
MathContext.DECIMAL32
- 7位精度,HALF_EVEN舍入 -
MathContext.DECIMAL64
- 16位精度,HALF_EVEN舍入 -
MathContext.DECIMAL128
- 34位精度,HALF_EVEN舍入 -
MathContext.UNLIMITED
- 无限精度,无舍入
示例
MathContext mc = new MathContext(5, RoundingMode.HALF_UP);
BigDecimal a = new BigDecimal("3.1415926535", mc); // 3.1416
4. 使用建议
-
构造方式:优先使用字符串构造
BigDecimal
,避免使用double
构造,因为double
本身可能有精度损失。// 不推荐 BigDecimal d = new BigDecimal(0.1);// 推荐 BigDecimal d = new BigDecimal("0.1");
-
除法运算:
BigDecimal
的除法必须指定舍入模式,否则可能抛出ArithmeticException
。 -
比较操作:使用
compareTo()
而不是equals()
进行数值比较,因为equals()
还会比较精度。 -
性能考虑:
BigInteger
和BigDecimal
的运算比基本数据类型慢,只在必要时使用。 -
不可变性:所有运算都返回新对象,原对象不变。
java.math
包为 Java 提供了强大的高精度数学计算能力,特别适合需要精确结果的金融、科学计算等应用场景。