Java教程

Java Math IEEEremainder()

Java Math IEEEremainder()

Java Math IEEEremainder() 方法对指定的参数执行除法并根据 IEEE 754 标准返回余数。
IEEEremainder() 方法的语法是:
Math.IEEEremainder(double x, double y)
注意: IEEEremainder() 方法是一个静态方法。因此,我们可以使用类名 Math 直接调用该方法。

IEEEremainder() 参数

x-除以 y 的红利 y-除 x 的除数

IEEEremainder() 返回值

根据 IEEE 754 标准返回余数

示例 1: Java Math.IEEEremainder()

class Main {
  public static void main(String[] args) {
    // declare variables
    double  arg1 = 25.0;
    double arg2 = 3.0;
    // perform Math.IEEEremainder() on arg1 and arg2
    System.out.println(Math.IEEEremainder(arg1, arg2));  // 1.0
  }
}

Math.IEEEremainder() 和 % Operator 的区别

Math.IEEEremainder() 方法和 % 运算符返回的余数等于 arg1-arg2 * n。但是, n 的值是不同的。
IEEEremainder()-n 是最接近 arg1/arg2 的整数。而且,如果 arg1/arg2 返回一个介于两个整数之间的值,则 n 是偶数整数(即对于结果 1.5,n = 2)。 % 运算符-narg1/arg2 的整数部分(对于结果 1.5,n = 1)。
class Main {
  public static void main(String[] args) {
    // declare variables
    double  arg1 = 9.0;
    double arg2 = 5.0;
    // using Math.IEEEremainder()
    System.out.println(Math.IEEEremainder(arg1, arg2));  //-1.0
    // using % operator
    System.out.println(arg1 % arg2);  // 4.0
  }
}
在上面的例子中,我们可以看到 IEEEremainder()方法和 %运算符返回的余数是不同的。这是因为,
对于 Math.IEEEremainder()
   arg1/arg2
=> 1.8
   // for IEEEremainder()
   n = 2
   arg-arg2 * n
=> 9.0-5.0 * 2.0
=>-1.0
对于 % 运算符
   arg1/arg2
=> 1.8
   // for % operator
   n = 1
   arg1-arg2 * n
=> 9.0-5.0 * 1.0
=> 4.0
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4