BigInteger
add
实现两个BigInteger相加,返回相加后的结果,不影响原来的数据
public BigInteger add(BigInteger val)
1 2 3 4 5 6 7 8 9 10 11
| BigInteger num1 = new BigInteger("1"); BigInteger num2 = new BigInteger("2");
BigInteger num3 = num1.add(num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); System.out.println("num3 = " + num3);
|
subtract
实现两个BigInteger相减,返回相减后的结果,不影响原来的数据
public BigInteger subtract(BigInteger val)
1 2 3 4 5 6 7 8 9 10 11
| BigInteger num1 = new BigInteger("1"); BigInteger num2 = new BigInteger("2");
BigInteger num3 = num1.subtract(num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); System.out.println("num3 = " + num3);
|
multiply
实现两个BigInteger相乘,返回相乘后的结果,不影响原来的数据
public BigInteger multiply(BigInteger val)
1 2 3 4 5 6 7 8 9 10 11
| BigInteger num1 = new BigInteger("1"); BigInteger num2 = new BigInteger("2");
BigInteger num3 = num1.multiply(num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); System.out.println("num3 = " + num3);
|
divide
实现两个BigInteger相除,返回相除后的结果,不影响原来的数据
public BigInteger divide(BigInteger val)
1 2 3 4 5 6 7 8 9 10 11
| BigInteger num1 = new BigInteger("4"); BigInteger num2 = new BigInteger("2");
BigInteger num3 = num1.divide(num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); System.out.println("num3 = " + num3);
|
mod
实现实现两个BigInteger取模,返回取模后的结果,不影响原来的数据
public BigInteger mod(BigInteger m)
1 2 3 4 5 6 7 8 9 10 11
| BigInteger num1 = new BigInteger("4"); BigInteger num2 = new BigInteger("2");
BigInteger num3 = num1.mod(num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); System.out.println("num3 = " + num3);
|
gcd
求两个BigInteger的最小公因数,不影响原来的数据
public BigInteger gcd(BigInteger val)
1 2 3 4 5 6 7 8 9 10 11
| BigInteger num1 = new BigInteger("4"); BigInteger num2 = new BigInteger("2");
BigInteger num3 = num1.gcd(num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); System.out.println("num3 = " + num3);
|
abs
求出BigInteger的绝对值,不影响原来的数据
public BigInteger abs()
1 2 3 4 5 6 7 8
| BigInteger num1 = new BigInteger("-4");
BigInteger num2 = num1.abs(); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2);
|
negate
求出BigInteger的相反数,不影响原来的数据
public BigInteger negate()
1 2 3 4 5 6 7 8
| BigInteger num1 = new BigInteger("-4");
BigInteger num2 = num1.negate(); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2);
|
max
求出两个BigInteger中的较大者
public BigInteger max(BigInteger val)
1 2 3 4 5 6 7 8 9 10 11
| BigInteger num1 = new BigInteger("-4"); BigInteger num2 = new BigInteger("2");
BigInteger num3 = num1.max(num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); System.out.println("num3 = " + num3);
|
min
求出两个BigInteger中的较小者
public BigInteger min(BigInteger val)
1 2 3 4 5 6 7 8 9 10 11
| BigInteger num1 = new BigInteger("-4"); BigInteger num2 = new BigInteger("2");
BigInteger num3 = num1.min(num2); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2); System.out.println("num3 = " + num3);
|
pow
快速幂
public BigInteger pow(int exponent)
1 2 3 4 5 6 7 8
| BigInteger num1 = new BigInteger("2");
BigInteger num2 = num1.pow(64); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2);
|
modPow
快速幂求模
public BigInteger modPow(BigInteger exponent, BigInteger m)
exponent为幂,m为模
1 2 3 4 5 6 7 8
| BigInteger num1 = new BigInteger("2"); BigInteger num2 = num1.modPow(new BigInteger("64"), new BigInteger("1000")); System.out.println("num1 = " + num1); System.out.println("num2 = " + num2);
|
Nmber接口方法
BigInteger实现了Number接口,可以使用对应的方法
请参考:Number接口方法
BigDecimal
BigDecimal的基本使用方法和BigInteger差不多,因此下面有很多方法直接跳转到BigInteger对应的位置阅读,学会了使用BigInteger,使用BigDecimal不是什么难的问题
add
请参考:add
subtract
请参考:subtract
multiply
请参考:multiply
divide
请参考:divide
abs
请参考:abs
negate
请参考:negate
max
请参考:max
min
请参考:min
pow
请参考:pow
Number接口方法
BigDecimal实现了Number接口,可以使用对应的方法
请参考:Number接口方法