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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。