Java解决超长数据间计算问题

1.问题

平时使用Java的基本数据类型或其包装类型能解决大部分的问题,但是有时会遇到特别大的数据,其值早已超过基本类型的范围,如运算10000000的阶乘。此时我们改如何存储阶乘的运算结果?

2.基本数据类型

基本数据类型
类型描述字节长度范围
boolean布尔型 true、false
char字符型与编码有关 
byte字节型1-128~127,即-2^7 ~2^7-1
short短整型2-32768 ~ 32767,即-2^15~2^15-1
int整型4-2^31~2^31
long长整型8-9223372036854775808 ~ 9223372036854774807
float单精度型4约±3.40282347E+38F (有效位数6-7位)
double双精度型8约±1.79769313486231570E+308 (有效位数15位)

如表所示,当一个数超过一定范围基本数据类型就无法表示。

3.BigInteger

直接来一个例子:

public class BigIntegerBox {

    public static BigInteger getNFactorial(BigInteger n){
        if (n.intValue() == 0 || n.intValue() == 1){
            return new BigInteger("1");
        }
        BigInteger num = new BigInteger("1");
        for (int i = 1; i <= n.intValue(); i++){
            num = num.multiply(new BigInteger(i + ""));
        }
        return new BigInteger(num + "");
    }

    public static void main(String[] args) {
        long start = new Date().getTime();
        BigInteger number = new BigInteger("1000");
        BigInteger fac = getNFactorial(number);
        System.out.println("1000的阶乘:");
        System.out.println(fac.toString());
        long end = new Date().getTime();
        System.out.println("用时:" + (end-start));
        System.out.println("============================================");
    }
}

运行结果:

1000的阶乘的结果是一个特别大的一个数。截图中只显示了数值的一部分,故基本数据类型已经不能使用了。

3.1用法

BigInteger类型的数据在运算时不能使用加减等操作运算符,只能条用对象的方法才能完成运算。

一、构造函数

BigInteger的构造函数有12种,下面介绍常用的几种:

  1. BigInteger(String val),使用数字字符串形式构造对象。
  2. BigInteger(String val,int radix),将指定基数的数值字符串转换为BigInteger表示形式。

二、常用的运算操作有:

  1. 加: add()
  2. 减:subtract()
  3. 乘:multiply()
  4. 除:divide()
  5. 幂运算:pow()
  6. 取余:remainder()
  7. 取绝对值:abs()

代码实例:

    public static void main(String[] args) {
        BigInteger num1 = new BigInteger("8");
        BigInteger num2 = new BigInteger("2");

        System.out.println("加:8 + 2 = " + num1.add(num2).toString());
        System.out.println("减:8 - 2 = " + num1.subtract(num2).toString());
        System.out.println("乘:8 * 2 = " + num1.multiply(num2).toString());
        System.out.println("除:8 / 2 = " + num1.divide(num2).toString());
        System.out.println("幂运算:8^2 = " + num1.pow(2).toString());
        System.out.println("取余:8 对 3 取余 = " + num1.remainder(new BigInteger("3")).toString());
        System.out.println("取绝对值:|-8|= " + new BigInteger("-8").abs().toString());
    }

运行结果:

3.2BigDecimal

BigDecimal适用于有效位超过16位的数据的运算,与BigInteger类似,也需要调用方法才能完成相应的运算。

BigDecimal的构造函数有17种,下面介绍常用的几种:

一、构造函数

  1. BigDecimal(String),使用数字字符串形式构造对象。
  2. BigDecimal(int),使用整型数据构造对象。
  3. BigDecimal(long),使用长整型数据构造对象。
  4. BigDecimal(double),使用双精度型数据构造对象。

二、常用的运算操作

  1. 加:add(BigDecimal)
  2. 减:subtract(BigDecimal)
  3. 乘:multiply(BigDecimal)
  4. 除:divide(BigDecimal)
  5. 幂运算:pow()
  6. 取余:remainder()
  7. 取绝对值:abs()

代码实例:

public static void main(String[] args) {
        BigDecimal num1 = new BigDecimal("8");
        BigDecimal num2 = new BigDecimal("2");

        System.out.println("加:8 + 2 = " + num1.add(num2).toString());
        System.out.println("减:8 - 2 = " + num1.subtract(num2).toString());
        System.out.println("乘:8 * 2 = " + num1.multiply(num2).toString());
        System.out.println("除:8 / 2 = " + num1.divide(num2).toString());
        System.out.println("幂运算:8^2 = " + num1.pow(2).toString());
        System.out.println("取余:8 对 3 取余 = " + num1.remainder(new BigDecimal("3")).toString());
        System.out.println("取绝对值:|-8|= " + new BigDecimal("-8").abs().toString());
}

运行结果:

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值