BigInteger

前言

我们想对数据进行一些操作时、加减乘除取余在某些特殊的情况下无法使用,保存的数据超过了float型、long型、double型的范围,Java对这类数据提供了支持类---------BigInteger

使用

  1. 里面的参数时:字符串形式表示整数
        BigInteger b1 = new BigInteger("2222222222");
        BigInteger b2 = new BigInteger("6666666666");
        BigInteger b3 = new BigInteger("7777777777");
  1. 常用方法参数
    BigInteger add(BigInteger val)               加法
    BigInteger subtract(BigInteger val)        减法
    BigInteger multiply(BigInteger val)         乘法
    BigInteger divide(BigInteger val)           除法
    BigInteger mod(BigInteger m)              模运算
    BigInteger remainder(BigInteger val)  求余
    divide中和 "/"一致,结果去掉小数
    mod和remainder两种运算
    mod被除数可以为负数,除数不能为负数
b1.mod(new BigInteger("-2"));
Exception in thread "main" java.lang.ArithmeticException: BigInteger: modulus not positive
	at java.math.BigInteger.mod(BigInteger.java:2415)
	at com.common.BigIntegerTest.main(BigIntegerTest.java:23)

Process finished with exit code 1

mod源码

 public BigInteger mod(BigInteger m) {
        if (m.signum <= 0)
            throw new ArithmeticException("BigInteger: modulus not positive");

        BigInteger result = this.remainder(m);
        return (result.signum >= 0 ? result : result.add(m));
    }

remainder的被除数和除数都可以为负数

System.out.println(b1.remainder(new BigInteger("-2")));
0

Process finished with exit code 0

remainder的运算结果是把余数作为结果
mod的远算结果是余数+模数作为结果

思考为什么?
这要从两个运算的概念开始探讨
取模运算和取余运算两个概念有重叠的部分但又不完全一致。
主要的区别在于对负整数进行除法运算时操作不同。
取模主要是用于计算机术语中。
取余则更多是数学概念。

假设有式子 a ÷ b = c ··· r
当a和b符号一致时的情况:a,b均大于0时,求模运算和求余运算所得的c的值一致,r >= 0。
当a,b均小于0时,只能进行求余运算,因为求模运算除数b必须为正,r也是大于等于0。

当a和b符号不一致时,c不一样,r也不一样。
求余运算结果,r不为0时, r 的符号和a一致。即余数符号和被除数符号必须相同。
求模运算结果,r不为0时,r 的符号和b一致,而因为求模运算时,b必须大于0,所以r一定大于等于0,即模运算的结果一定是非负数。

本文若有错误请指正,互相学习,加油!

### JavaBigInteger 的基本概念与使用 `BigInteger` 是 Java 提供的一个类,位于 `java.math` 包中。它允许开发者处理任意精度的整数数据,而不会受到标准整数类型(如 `int` 或 `long`)范围的限制。 以下是关于 `BigInteger` 类的一些核心方法及其使用的说明: #### 1. **remainder 方法** `remainder()` 方法用于计算当前 `BigInteger` 对象除以另一个指定 `BigInteger` 对象后的余数值[^1]。 ```java import java.math.BigInteger; public class RemainderExample { public static void main(String[] args) { BigInteger num1 = new BigInteger("10"); BigInteger num2 = new BigInteger("3"); BigInteger remainder = num1.remainder(num2); System.out.println("Remainder of 10/3: " + remainder); // 输出:1 } } ``` --- #### 2. **compareTo 方法** `compareTo()` 方法用于比较两个 `BigInteger` 对象的大小关系[^2]。返回值如下: - 如果调用者大于参数,则返回正整数; - 如果两者相等,则返回零; - 如果调用者小于参数,则返回负整数。 ```java import java.math.BigInteger; public class CompareToExample { public static void main(String[] args) { BigInteger num1 = new BigInteger("5"); BigInteger num2 = new BigInteger("7"); int result = num1.compareTo(num2); if (result > 0) { System.out.println("num1 is greater than num2."); } else if (result == 0) { System.out.println("num1 and num2 are equal."); } else { System.out.println("num1 is less than num2."); // 输出此句 } } } ``` --- #### 3. **shiftLeft 方法** `shiftLeft(int n)` 方法实现了按位左移操作,相当于将当前 `BigInteger` 值乘以 \(2^n\) 并返回新的 `BigInteger` 实例[^3]。 ```java import java.math.BigInteger; public class ShiftLeftExample { public static void main(String[] args) { BigInteger num = new BigInteger("8"); // 十进制表示形式为 8 BigInteger shiftedNum = num.shiftLeft(2); // 左移两位,即 8 * 2^2 = 32 System.out.println(shiftedNum); // 输出:32 } } ``` --- #### 4. **isProbablePrime 方法** `isProbablePrime(int certainty)` 方法用来判断某个 `BigInteger` 是否可能是一个质数[^4]。如果置信度达到一定程度,认为它是质数则返回 `true`;否则返回 `false`。 ```java import java.math.BigInteger; import java.util.Random; public class IsProbablePrimeExample { public static void main(String[] args) { Random rand = new Random(); BigInteger primeCandidate = new BigInteger(10, rand); // 随机生成一个大整数 boolean probablePrime = primeCandidate.isProbablePrime(10); // 设置置信度为 10 System.out.println(primeCandidate + " 可能是质数吗?" + probablePrime); } } ``` --- #### 5. **testBit 方法** `testBit(int n)` 方法测试第 `n` 位是否被设置为 1[^5]。如果是,则返回 `true`;如果不是,则返回 `false`。 ```java import java.math.BigInteger; public class TestBitExample { public static void main(String[] args) { BigInteger num = new BigInteger("10"); // 二进制表示为 1010 boolean bitTestResult = num.testBit(1); // 测试第二位(索引从 0 开始) System.out.println(bitTestResult); // 输出:true } } ``` --- ### 总结 以上展示了几个常用的 `BigInteger` 方法以及它们的具体应用实例。这些功能使得 `BigInteger` 成为了处理超大数据量运算的理想工具之一,在加密算法等领域尤为常见。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值