Java核心技术慢慢学
如果基本的整数和浮点数精度不能够满足需求, 那么可以使用 jaVa.math 包中的两个 很有用的类: Biglnteger 和 BigDecimaL 这两个类可以处理包含任意长度数字序列的数值。 Biglnteger 类实现了任意精度的整数运算, BigDecimal 实现了任意精度的浮点数运算。
常用的方法:
BigInteger a = BigInteger.valueOf(100);
//加法运算
BigInteger b = BigInteger.valueOf(100);
BigInteger c = a.add(b);
//减法运算
c = a.subtract(b);
//乘法运算
c = a.multiply(b);
//除法运算
c = a.divide(b);
//取余运算
c = a.mod(b);
//对比运算,如果这个大整数与另一个大整数 other 相等, 返回 0;
//如果这个大整数小于另一个大整数, 返回负数;否则,返回正数
a.compareTo(c);
//BigDecimal同样存在相同方法,但是可以设置精度
System.out.println(BigDecimal.valueOf(1, 100));
//输出结果为:1E-100
数组复制操作:
int[] a = new int[10];
for (int i = 0;i < 10;i++)
a[i] = i;
int[] b = Arrays.copyOf(a, 10);
for (int i : b)
System.out.print(i + " ");
//输出结果:0 1 2 3 4 5 6 7 8 9