Java BigInteger mod()方法

Java BigInteger mod()方法

java.math.BigInteger.mod(BigInteger big) 方法返回一个BigInteger,其值等于(这个BigInteger modfunction big(作为参数传递的BigInteger)),换句话说,我们可以说这个方法返回执行(这个% big)步骤后的值。

java.math.BigInteger.mod(BigInteger big)和java.math.BigInteger.remainder(BigInteger val)这两个函数都可以找到余数,但是mod操作总是返回一个非负的BigInteger。

语法

public BigInteger mod(BigInteger big)

参数: 该函数接受一个强制参数big,该参数指定了我们想要除以这个大整数对象的大整数对象。

返回值: 该方法返回等于此BigInteger mod big(作为参数传递的BigInteger)的BigInteger。

异常: 当big≤0时,该方法会抛出ArithmeticException。

例子

输入: BigInteger1=321456, BigInteger2=31711
输出: 4346
解释: BigInteger1.mod(BigInteger2)=4346。321456和31711之间的除法运算 321456和31711之间的除法运算,返回4346作为余数。

输入: BigInteger1=59185482345, BigInteger2=59185482345
输出: 0
解释: BigInteger1.mod(BigInteger2)=0。59185482345和59185482345之间的除法运算,余数为0。

下面的程序说明了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小于0时,程序显示异常。

// 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).

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程