Java IEEEremainder Function

The Java IEEEremainder Function is one of the Math Library functions, and it is useful to find the remainder of the two arguments as prescribed by the IEEE 754 standard. In this article, we will show how to write Math.IEEEremainder function in Java Programming language with an example.

Java IEEEremainder syntax

The basic syntax of the Math.IEEEremainder Function in Java Programming language is as shown below:

static double IEEEremainder(double x, double y); // Return Type is Double

//In order to use in Java program use following code
Math.IEEEremainder(double x, double y);
  • x: Please specify the dividend value here.
  • y: Please specify the divisor value or power here.

The IEEEremainder function will find the remainder of the two arguments. The remainder value is mathematically equal to x – y × n, where n is the integer closest to the exact mathematical value of the quotient x / y. If two integers are equally close to x / y, then n is the even integer. There are some special cases where the result of the Java IEEEremainder will be different, and they are:

  • If the first argument is infinite, Math.IEEEremainder will return the result as NaN.
  • If the second argument is positive or negative zero, it will return NaN.
  • When either the x or y argument is not a Number, it will return the result as NaN.
  • If the first argument is finite and the second argument is infinity, then Math.IEEEremainder will return the first argument value as a result.

Java IEEEremainder Function Example

The return value is nothing but x Modulus y, and if you find it difficult to understand the modulus operator. For example, 6 % 12 = 2. The Java IEEEremainder function returns the remainder of the given number. In this Java example, we will find the remainder of both positive and negative double values and display the output.

package MathFunctions;

public class IEEEremainderMethod {
	public static void main(String[] args) {
		 // get the remainder when x/y -- Mod
		double a = Math.IEEEremainder((5.12 - 4.65 + 3.53), 3);
		System.out.println("Math.IEEEremainder Result = = " + a);	
		
		System.out.println("\nMath.IEEEremainder Result for Zero as First argument = " + Math.IEEEremainder(0, 2));
		System.out.println("Math.IEEEremainder Result for Zero as Second argument = " + Math.IEEEremainder(2, 0));

		System.out.println("\nMath.IEEEremainder Result of Positive Number = " + Math.IEEEremainder(9, 8));
		System.out.println("Math.IEEEremainder Result of Positive Number = " + Math.IEEEremainder(6, 12));
		
		System.out.println("\nMath.IEEEremainder Result of Negative Number = " + Math.IEEEremainder(-98, 6));
		System.out.println("Math.IEEEremainder Result of Negative Number = " + Math.IEEEremainder(164, -5));
	}
}
Java IEEEremainder Function 1

Within this IEEEremainder function example, First, We declared variable a of type Double. Next, we used Java Math.IEEEremainder functions directly on expression. Here, we used System.out.println statement to print the result as output. Please visit the Java Arithmetic Operators article.

double a = Math.IEEEremainder((5.12 - 4.65 + 3.53), 3);
System.out.println("Math.IEEEremainder Result = = " + a);

From the above statement, Math.IEEEremainder((5.12 – 4.65 + 3.53), 3) ==> (4, 3) ==> 0.99 (~ 1)

Next, We used IEEEremainder Function with one positive double value and another argument as zero.

System.out.println("\nMath.IEEEremainder Result for Zero as First argument = " + Math.IEEEremainder(0, 2));
System.out.println("Math.IEEEremainder Result for Zero as Second argument = " + Math.IEEEremainder(2, 0));

Here, We used the Java Function directly on Positive double values.

System.out.println("\nMath.IEEEremainder Result of Positive Number = " + Math.IEEEremainder(9, 8));
System.out.println("Math.IEEEremainder Result of Positive Number = " + Math.IEEEremainder(6, 12));

Next, We used Math.IEEEremainder Function directly on Negative double values.

System.out.println("\nMath.IEEEremainder Result of Negative Number = " + Math.IEEEremainder(-98, 6));
System.out.println("Math.IEEEremainder Result of Negative Number = " + Math.IEEEremainder(164, -5));