当需要处理超过 long 数值范围的大整数时,java.math 包中的 BigInteger 类提供任意精度的整数运算。
四则运算与取整求余取模
//加法 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 来说,取模运算或者求余运算的方法都是:
求整数商:c = a/b;计算模或者余数: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 }float 和 double 进行运算时会出现精度丢失,java.math 包中的 BigDecimal 类提供任意精度的整数运算。
转载于:https://www.cnblogs.com/zongmin/p/11344198.html
相关资源:java-BigInteger-BigDecimal类源码