BigInteger&BigDecimal类

BigInteger类

当需要处理超过 long 数值范围的大整数时,java.math 包中的 BigInteger 类提供任意精度的整数运算。

构造方式

//构造方法,将BigInteger的十进制字符串表示形式转换为BigInteger
public BigInteger(String val);   

//构造方法,将指定基数的BigInteger的字符串表示形式转换为BigInteger
public BigInteger(String val, int radix);

//静态方法,将一个long类型值转为BigInteger
public static BigInteger valueOf(long val);

常用方法

四则运算与取整求余取模

//加法
public BigInteger add(BigInteger val);

//减法
public BigInteger subtract(BigInteger val);

//乘法
public BigInteger multiply(BigInteger val);

//除法(取整)
public BigInteger divide(BigInteger val);

//求余
public BigInteger remainder(BigInteger val);

//取整和求余,返回的是一个数组
public BigInteger[] divideAndRemainder(BigInteger val);

//取模
public BigInteger mod(BigInteger val); 

求余和取模对比

对于整型数 a,b 来说,取模运算或者求余运算的方法都是:

  1. 求整数商:c = a/b;
  2. 计算模或者余数:r = a - c*b.

取模运算和求余运算在第一步不同:取余运算在取 c 的值时,向 0 方向舍入;而取模运算在计算 c 的值时,向负无穷方向舍入。因此,取模时结果的符号与 b 一致,求余时结果的符号与 a 一致。如果 a,b 都是正整数的话,求模与求余没有区别。

数学函数

//绝对值
public BigInteger abs();

//取负
public BigInteger negate();

//求幂
public BigInteger pow(int exponent);

//最大公约数
public BigInteger gcd(BigInteger val);

//最大值
public BigInteger max(BigInteger val);

//最小值
public BigInteger min(BigInteger val);

获取基本类型的值

//不同于基本数值类型的包装类,此处并不是直接强转的 
//如果太大intValue和longValue将分别返回低的32位和64位,longValue和doubleValue可能会被转换为无穷
//返回大整数的int值
public int intValue(); 

//返回大整数的long型值
public long longValue();

//返回大整数的float类型的值
public float floatValue();

//返回大整数的double类型的值
public double doubleValue();

//下面四种方法转换时不会舍入或者转换,会进行数据长度的校验,长度不够将会抛出异常
public int longValueExact();
public long intValueExact();
public float shortValueExact();
public double byteValueExact();

位操作相关

//按位与
public BigInteger and(BigInteger val);

//按位或
public BigInteger or(BigInteger val);

//按位非
public BigInteger not();

//按位异或
public BigInteger xor(BigInteger val);

//按位与非(等效and(val.not()))
public BigInteger andNot(BigInteger val);

//左移,相当于this << n,右边添0
public BigInteger leftShift(int n);

//右移,相当于this >> n,左边负数添1,正数添0
public BigInteger rightShift(int n);

//计算(this & (1<<n)) != 0	
public boolean testBit(int n);

//计算this|(1<<n) 
public BigInteger setBit(int n);

//计算this&~(1<<n)
public BigInteger clearBit(int n);

//计算this^(1<<n)
public BigInteger flipBit(int n);

素数

//判断是否为素数
public boolean isProbablePrime(int certainty);
  • 如果此 BigInteger 可能为素数,则返回 true,如果它一定为合数,则返回 false。如果 certainty <= 0,则返回 true,所以不要设置 certainty <= 0。certainty 是调用方允许的不确定性的度量。如果该调用返回 true,则此 BigInteger 是素数的概率超出 1 - 1/(2^certainty),此方法的执行时间与此参数的值是成比例的。
//返回有可能是素数的数
public static BigInteger probablePrime(int bitLength,Random rnd);
  • 此方法返回的 BigInteger 是合数的概率不超出 2^-100。bitLength 是返回的 BigInteger 的 bitLength,rnd 是随机比特源,用这些随机比特选择用来进行质数测试的候选数。
//返回大于此BigInteger的可能为素数的第一个整数
public BigInteger nextProbablePrime();
  • 此方法返回的数是合数的概率不超出 2^-100

其他方法

//获取符号位
public int signum();

//返回此 BigInteger 的十进制字符串表示形式
public String toString();

//返回此 BigInteger 的给定基数的字符串表示形式
public String toString(int radix);

//返回一个 byte 数组,该数组包含此 BigInteger 的二进制补码表示形式
public byte[] toByteArray();

//左边比右边数大,返回1,相等返回0,比右边小返回-1
public int compareTo(Big val);
public static void main(String[] args) {
    BigInteger a = new BigInteger("13");
    BigInteger b = BigInteger.valueOf(4);
    int n = 3;

    //加
    System.out.println(a.add(b));			//17
    //减
    System.out.println(a.subtract(b));		//9
    //乘
    System.out.println(a.multiply(b));		//52
    //除
    System.out.println(a.divide(b));		//3
    //取模
    System.out.println(a.mod(b));			//1
    //求余
    System.out.println(a.remainder(b));	    //1
    //求幂
    System.out.println(a.pow(n));			//2197
    //取绝对值
    System.out.println(a.abs());			//13
    //取相反数
    System.out.println(a.negate());		   //-13
}


BigDecimal类

float 和 double 进行运算时会出现精度丢失,java.math 包中的 BigDecimal 类提供任意精度的整数运算。

构造方式

//创建一个具有参数所指定以String表示的数值的对象。
public BigDecimal(String value);  //使用字符串方式,其它构造器最好不要使用

//静态方式创建
public static BigDecimal valueOf(long val);
public static BigDecimal valueOf(double val);

常用方法

//加法
public BigDecimal add(BigDecimal augend);      

//减法
public BigDecimal subtract(BigDecimal subtrahend);

//乘法
public BigDecimal multiply(BigDecimal multiplicand);

//除法
public BigDecimal divide(BigDecimal divisor);

//求余数
public BigDecimal remainder(BigDecimal divisor);

//求相反数
public BigDecimal negate();

//左边比右边数大,返回1,相等返回0,比右边小返回-1
public int compareTo(BigDecimal val);

//绝对值
public BigDecimal abs();

//值转换为字符串
public String toString();

//值转换为double
public double doubleValue();

//值转换为float
public float floatValue(); 
    
//值转换为long
public long longValue();    

//值转换为int
public int intValue();
public static void main(String[] args) {
    BigDecimal f1 = new BigDecimal("6");
    BigDecimal f2 = BigDecimal.valueOf(1.2);

    System.out.println(f1.add(f2));  //7.2
    System.out.println(f1.subtract(f2));  //4.8
    System.out.println(f1.multiply(f2));  //7.2
    System.out.println(f1.divide(f2));  //5

    int a = f1.intValue();
    System.out.println(a);  //6

    double b = f2.doubleValue();
    System.out.println(b);  //1.2
}


参考

  1. https://www.cnblogs.com/noteless/p/9877957.html
  2. https://www.cnblogs.com/noteless/p/9896139.html
原文地址:https://www.cnblogs.com/zongmin/p/11344198.html