Java中的Math floorMod()方法

这个 JAVA朗,数学。地板 是java中的内置数学函数,返回传递给它的整数参数的下限模。因此,地板模量为 (a–(F(a,b)*b)) ,与除数b具有相同的符号,并且在 -abs(b) .

null

floorDiv和floorMod之间的关系是:

floorDiv(a,b)*b+floorMod(a,b)=a

两者之间的价值差异 地板 还有 % 运算符是由于 弗劳尔迪夫 返回小于或等于商和 / 返回最接近零的整数的运算符。

语法:

public static int floorMod(data_type a, data_type b)

参数: 该函数接受两个参数。

  • A. :这是指股息值。
  • B :这是指除数值。

    参数可以是数据类型 智力 长的 .

例外情况: 它抛出一个 算术异常 如果除数为零。

返回值: 此方法返回地板模量x—— (F(x,y)*y)。

下面的程序用来说明java的工作原理。朗,数学。floorMod()方法。 项目1:

// Java program to demonstrate working
// of java.lang.Math.floorMod() method
import java.lang.Math;
class Gfg1 {
public static void main(String args[])
{
int a = 25 , b = 5 ;
System.out.println(Math.floorMod(a, b));
// Divisor and dividend is having same sign
int c = 123 , d = 50 ;
System.out.println(Math.floorMod(c, d));
// Divisor is having negative sign
int e = 123 , f = - 50 ;
System.out.println(Math.floorMod(e, f));
// Dividend is having negative sign
int g = - 123 , h = 50 ;
System.out.println(Math.floorMod(g, h));
}
}


输出:

0
23
-27
27

项目2:

// Java program to demonstrate working
// of java.lang.Math.floorMod() method
import java.lang.Math;
class Gfg2 {
public static void main(String args[])
{
int x = 200 ;
int y = 0 ;
System.out.println(Math.floorMod(x, y));
}
}


输出:

Runtime Error :
Exception in thread "main" java.lang.ArithmeticException: / by zero
    at java.lang.Math.floorDiv(Math.java:1052)
    at java.lang.Math.floorMod(Math.java:1139)
    at Gfg2.main(File.java:13)

参考: https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#floorMod-整型整型-

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享