當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java BigInteger remainder()用法及代碼示例


java.math.BigInteger.remainder(BigInteger big)方法返回一個BigInteger,其值等於(此BigInteger%big(BigInteger作為參數傳遞))。參數。

用法:

public BigInteger remainder(BigInteger big)

參數:該函數接受單個必填參數big,該參數指定了我們要用來劃分此bigInteger對象的BigInteger對象。


返回值:該方法返回的BigInteger等於此BigInteger%big(作為參數傳遞的BigInteger)。

異常該方法引發ArithmeticException –當big = 0時

例子:

Input: BigInteger1=321456, BigInteger2=31711
Output: 4346
Explanation: BigInteger1.remainder(BigInteger2)=4346. The divide operation 
between 321456 and 31711 returns 4346 as remainder.

Input: BigInteger1=59185482345, BigInteger2=59185482345
Output: 0
Explanation: BigInteger1.remainder(BigInteger2)=0. The divide operation between 
59185482345 and 59185482345 returns 0 as remainder.

示例1:以下示例程序旨在說明BigInteger類的remainder()方法

// Java program to demonstrate remainder() 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 remainder() method 
        BigInteger result = b1.remainder(b2); 
  
        // print result 
        System.out.println("Result of remainder "
                           + "operation between " + b1 
                           + " and " + b2 + " equal to " + result); 
    } 
}
輸出:
Result of remainder operation between 321456 and 31711 equal to 4346

示例2:當兩者的值相等時。

// Java program to demonstrate remainder() 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 remainder() method 
        BigInteger result = b1.remainder(b2); 
  
        // print result 
        System.out.println("Result of remainder "
                           + "operation between " + b1 
                           + " and " + b2 + " equal to " + result); 
    } 
}
輸出:
Result of remainder operation between 3515219485 and 3515219485 equal to 0

示例3:程序在BigInteger作為參數傳遞為零時顯示異常。

// Java program to demonstrate remainder() 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("0"); 
  
        // apply remainder() method 
        BigInteger result = b1.remainder(b2); 
  
        // print result 
        System.out.println("Result of remainder "+ 
                "operation between " + b1 
                + " and " + b2 + 
                " equal to " + result); 
    } 
}
Output:
Exception in thread "main" java.lang.ArithmeticException: BigInteger divide by zero
    at java.math.MutableBigInteger.divideKnuth(MutableBigInteger.java:1179)
    at java.math.MutableBigInteger.divideKnuth(MutableBigInteger.java:1163)
    at java.math.BigInteger.remainderKnuth(BigInteger.java:2167)
    at java.math.BigInteger.remainder(BigInteger.java:2155)
    at GFG.main(GFG.java:16)

參考:
BigInteger remainder() Docs



相關用法


注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 BigInteger remainder() Method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。