Java大数类BigInteger、BigDecimal的使用

Java中提供了两个大数类:BigIntegerBigDecimal,一个是表示整数,一个表示浮点数的。理论上只要内存够,表示的数就能无限大

BigInteger

获取BigInteger对象

获取BigInteger对象可以通过构造函数或者提供的静态方法。

传入一个只包含数字的字符串。第二个参数为进制,不填的话默认为10进制。

BigInteger num1 = new BigInteger("123456");
BigInteger num2 = new BigInteger("1001",2);

使用valueOf()方法传入一个long类型的数

BigInteger num3 = BigInteger.valueOf(3l);

BigInteger常用的方法

import java.math.BigInteger;
public class Main{
    public static void main(String[] args) {
        BigInteger one = BigInteger.ONE;
        BigInteger zero = BigInteger.ZERO;
        BigInteger ten = BigInteger.TEN;
        BigInteger two = BigInteger.valueOf(2l);
        BigInteger seven = BigInteger.valueOf(7l);
        BigInteger num1 = new BigInteger("123456");
        BigInteger num2 = new BigInteger("10101", 2);
        System.out.println(num1);
        System.out.println(num2);
        BigInteger num3 = BigInteger.valueOf(3l);
        System.out.println(num3);
        // 大数的加减乘除取模运算,参数都是一个BigInteger对象,运行结果返回一个BigInteger对象
        System.out.println("num1.add(num2) = " + num1.add(num2));
        System.out.println("num1.subtract(num2) = " + num1.subtract(num2));
        System.out.println("num1.multiply(num3) = " + num1.multiply(num3));
        System.out.println("num1.divide(num2) = " + num1.divide(num2));
        System.out.println("num1.mod(num2) = " + num1.mod(num2));
        // 相当于求 x^n  参数类型是int
        System.out.println("num1.pow(2) = " + num1.pow(2));
        // x^n % m
        System.out.println("num1.modPow(two,two) = " + num1.modPow(two, two));
        // (1/x)%m
        System.out.println("seven.modInverse(BigInteger.valueOf(20)) = " + seven.modInverse(BigInteger.valueOf(20)));

        // 返回最大值或者最小值
        System.out.println("num1.max(two) = " + num1.max(two));
        System.out.println("num1.min(two) = " + num1.min(two));
        // 比较大小
        System.out.println("num1.compareTo(num2) = " + num1.compareTo(num2));
        // 绝对值
        System.out.println("num1.abs() = " + num1.abs());
        // 求gcd
        System.out.println("num1.gcd(num2) = " + num1.gcd(num2));
        // 参数 certainty - 这一措施的调用者能容忍的不确定性:如果调用返回true,则此BigInteger是素数超过概率(1 - 1/2确定性)。此方法的执行时间正比于该参数的值。
        System.out.println("num1.isProbablePrime(10) = " + num1.isProbablePrime(1));

        // 下面四个是位运算
        System.out.println("num1.andNot(num2) = " + num1.andNot(num2));
        System.out.println("num1.and(num2) = " + num1.and(num2));
        System.out.println("num1.or(num2) = " + num1.or(num2));
        System.out.println("num1.xor(num2) = " + num1.xor(num2));

        //转成字符串, 可传入一个参数,转成对应进制的字符串
        System.out.println("num1.toString() = " + num1.toString());
        System.out.println("num1.toString(2) = " + num1.toString(2));
    }
}

BigDecimal

获取BigDecimal对象

获取和方法和BigInteger大体类似

BigDecimal one = BigDecimal.ONE ;
BigDecimal zero = BigDecimal.ZERO  ;
BigDecimal ten = BigDecimal.TEN;
BigInteger bigInteger = BigInteger.ONE;
BigDecimal num1 = new BigDecimal(bigInteger );
BigDecimal num2 = new BigDecimal(10);
BigDecimal num3 = new BigDecimal(30.12);
BigDecimal num4 = new BigDecimal("123456");
BigDecimal num5= new BigDecimal(100l);
BigDecimal num6 = BigDecimal.valueOf(10l);
BigDecimal num7= BigDecimal.valueOf(123);

BigDecimal常用的方法

import java.math.BigDecimal;
import java.math.BigInteger;

public class Main {

    public static void main(String[] args) {
        BigDecimal one = BigDecimal.ONE ;
        BigDecimal zero = BigDecimal.ZERO  ;
        BigDecimal ten = BigDecimal.TEN;
        BigInteger bigInteger = BigInteger.ONE;
        BigDecimal num1 = new BigDecimal(bigInteger );
        BigDecimal num2 = new BigDecimal(10);
        BigDecimal num3 = new BigDecimal(30.12);
        BigDecimal num4 = new BigDecimal("123456");
        BigDecimal num5= new BigDecimal(100l);
        BigDecimal num6 = BigDecimal.valueOf(10l);
        BigDecimal num7= BigDecimal.valueOf(123);
        
        BigDecimal num = BigDecimal.valueOf(123441l);

        // 四则运算
        System.out.println("num.add(num1) = " + num.add(num1));
        System.out.println("num.subtract(num2) = " + num.subtract(num2));
        System.out.println("num.divide(num3) = " + num.divide(num3));
        System.out.println("num.multiply(num1) = " + num.multiply(num1));
        // 幂运算
        System.out.println("num1.pow(10) = " + num1.pow(10));

        System.out.println("num.abs() = " + num.abs());
        System.out.println("num.min(num4) = " + num.min(num4));
        System.out.println("num.max(num5) = " + num.max(num5));
        System.out.println("num.compareTo(num7) = " + num.compareTo(num7));
        System.out.println("num1.toBigInteger() = " + num1.toBigInteger());
        System.out.println("num.toString() = " + num.toString());
    }
}

上一篇:Game of Taking Stones HDU - 5973


下一篇:python – 有一个numpy biginteger?