Handling Remainders with IEEEremainder(double f1, double f2) Method in Java: A Comprehensive Guide

Rumman Ansari   Software Engineer   2024-07-04 04:24:48   6170  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Description

This java tutorial shows how to use the IEEEremainder(double f1,double f2) method of Math class under java.lang package. This method computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. The remainder value is mathematically equal to f1 – f2 × n, where n is the mathematical integer closest to the exact mathematical value of the quotient f1/f2, and if two mathematical integers are equally close to f1/f2, then n is the integer that is even.

Method Syntax

public static double IEEEremainder(double f1,double f2)

Method Argument

DataType Parameter Description
double f1 the dividend.
double f2 the divisor

Method Returns

The  IEEEremainder(double f1,double f2) method simply returns the remainder when  f1 is divided by  f2. Special cases:

  • If either argument is NaN, or the first argument is infinite, or the second argument is positive zero or negative zero, then the result is NaN.
  • If the first argument is finite and the second argument is infinite, then the result is the same as the first argument.

Compatibility

Requires Java 1.0 and up

Example

This java example source code demonstrates the use of IEEEremainder(double f1,double f2) method of Math class. Basically we just get the remainder result from the values coming from the user input.


import static java.lang.System.*;

import java.util.Scanner;

/*
 * This example source code demonstrates the use of
 *  IEEEremainder(double f1,double f2) method of Math class
 * Get the remainder of the two value from user input
 */

public class MathRemainder {

	public static Scanner scan;
	public static void main(String[] args) {
		// ask for user input
		out.print("Enter a dividend:");
		scan = new Scanner(System.in);
		// use scanner to get user console input, dividend
		double dividend = scan.nextDouble();
		// use scanner to get user console input, divisor
		out.print("Enter a diviso:r ");
		scan = new Scanner(System.in);
		double divisor = scan.nextDouble();
		// get the remainder
		double remainder = Math.IEEEremainder(dividend, divisor);
		out.println("Remainder = "+remainder);
		// close the scanner object to avoid memory leak
		scan.close();

	}

}

output

Running the IEEEremainder method example source code of Math class will give you the following output

Enter a dividend:3
Enter a diviso:r 5
Remainder = -2.0
Press any key to continue . . .