java.math.BigInteger.mod(BigInteger big)方法返回一个值等于(此BigInteger mod函数big(BigInteger作为参数传递))的BigInteger。换句话说,我们可以说该方法执行后返回值(this %big)步骤.mod操作将此BigInteger除以作为参数传递的另一个BigInteger之后找到余数。
java.math.BigInteger.mod(BigInteger big)和java.math.BigInteger.remainder(BigInteger val)都可以找到余数,但mod操作始终返回非负的BigInteger。
用法:
public BigInteger mod(BigInteger big)
参数:该函数接受单个必填参数big,该参数指定了我们要用来划分此bigInteger对象的BigInteger对象。
返回值:该方法返回的BigInteger等于此BigInteger mod big(作为参数传递的BigInteger)。
异常:当big≤0时,该方法引发ArithmeticException
例子:
Input: BigInteger1=321456, BigInteger2=31711 Output: 4346 Explanation: BigInteger1.mod(BigInteger2)=4346. The divide operation between 321456 and 31711 returns 4346 as remainder. Input: BigInteger1=59185482345, BigInteger2=59185482345 Output: 0 Explanation: BigInteger1.mod(BigInteger2)=0. The divide operation between 59185482345 and 59185482345 returns 0 as remainder.
下面的程序说明BigInteger类的mod()方法:
示例1:
// Java program to demonstrate mod() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigInteger objects
BigInteger b1, b2;
b1 = new BigInteger("321456");
b2 = new BigInteger("31711");
// apply mod() method
BigInteger result = b1.mod(b2);
// print result
System.out.println("Result of mod
operation between " + b1
+ " and " + b2 +
" equal to " + result);
}
}
Result of mod operation between 321456 and 31711 equal to 4346
示例2:当两者的价值相等时。
// Java program to demonstrate mod() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigInteger objects
BigInteger b1, b2;
b1 = new BigInteger("3515219485");
b2 = new BigInteger("3515219485");
// apply mod() method
BigInteger result = b1.mod(b2);
// print result
System.out.println("Result of mod
operation between " + b1
+ " and " + b2 +
" equal to " + result);
}
}
Result of mod operation between 3515219485 and 3515219485 equal to 0
示例3:当BigInteger作为参数传递时,程序显示异常小于零。
// Java program to demonstrate mod() method of BigInteger
import java.math.BigInteger;
public class GFG {
public static void main(String[] args)
{
// Creating 2 BigInteger objects
BigInteger b1, b2;
b1 = new BigInteger("3515219485");
b2 = new BigInteger("-1711");
// apply mod() method
BigInteger result = b1.mod(b2);
// print result
System.out.println("Result of mod
operation between " + b1
+ " and " + b2 +
" equal to " + result);
}
}
输出:
Exception in thread "main" java.lang.ArithmeticException: BigInteger: modulus not positive at java.math.BigInteger.mod(BigInteger.java:2458) at GFG.main(GFG.java:17)
参考: https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#mod(java.math.BigInteger)
相关用法
- Java BigInteger pow()用法及代码示例
- Java BigInteger xor()用法及代码示例
- Java BigInteger or()用法及代码示例
- Java BigInteger and()用法及代码示例
- Java BigInteger not()用法及代码示例
- Java BigInteger abs()用法及代码示例
- Java BigInteger remainder()用法及代码示例
- Java BigInteger equals()用法及代码示例
- Java BigInteger doubleValue()用法及代码示例
- Java BigInteger toByteArray()用法及代码示例
- Java BigInteger valueOf()用法及代码示例
- Java BigInteger sqrt()用法及代码示例
- Java BigInteger negate()用法及代码示例
- Java BigInteger testBit()用法及代码示例
- Java BigInteger getLowestSetBit()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 BigInteger mod() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。