The syntax of the IEEEremainder()
method is:
Math.IEEEremainder(double x, double y)
Note: The IEEEremainder()
method is a static method. Hence, we can call the method directly using the class name Math
.
IEEEremainder() Parameters
- x - the dividend which is divided by y
- y - the divisor which divides x
IEEEremainder() Return Values
- returns the remainder according to IEEE 754 standard
Example 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
}
}
Difference between Math.IEEEremainder() and % Operator
The remainder returned by both the Math.IEEEremainder()
method and %
operator is equal to arg1 - arg2 * n
. However, the value of n is different.
- IEEEremainder() - n is closest integer to
arg1/arg2
. And, ifarg1/arg2
returns a value in between two integers, n is even integer (i.e for result 1.5, n = 2). - % operator - n is the integer part of
arg1/arg2
(for result 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
}
}
In the above example, we can see that the remainder values returned by IEEEremainder()
method and the %
operator are different. It is because,
For Math.IEEEremainder()
arg1/arg2
=> 1.8
// for IEEEremainder()
n = 2
arg - arg2 * n
=> 9.0 - 5.0 * 2.0
=> -1.0
For % operator
arg1/arg2
=> 1.8
// for % operator
n = 1
arg1 - arg2 * n
=> 9.0 - 5.0 * 1.0
=> 4.0