Java Math IEEEremainder() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.lang.Math

Definition

In Java, Math.IEEEremainder(f1, f2) returns f1 - f2 * n, where n is the whole number closest to the exact quotient f1 / f2. If the quotient is exactly halfway between two whole numbers, n is the even one. This is the remainder operation defined by the IEEE 754 standard.

The % operator is different: it truncates the quotient toward zero, so its result takes the sign of the dividend. IEEEremainder has a smaller size (never more than half of |f2|) and can be negative even for positive inputs: Math.IEEEremainder(11, 3) is -1.0, while 11 % 3.0 is 2.0.

A static method of the java.lang.Math class, available since the early versions of Java (JDK 1.0). Call it on the class name, as in Math.IEEEremainder(...). No import is needed, because classes in java.lang are always visible.

Syntax

Pass two double values: the dividend, then the divisor. int and float arguments are widened.

Java
double a = Math.IEEEremainder(10, 3);     // 1.0
double b = Math.IEEEremainder(11, 3);     // -1.0  (n is 4)
double c = Math.IEEEremainder(5, 2);      // 1.0   (tie: n is the even 2)
double d = Math.IEEEremainder(370, 360);  // 10.0  (angle wrapped)
CallWhat it does
Math.IEEEremainder(10, 3)10 / 3 is about 3.33, so n is 3: 10 - 9 = 1.0.
Math.IEEEremainder(11, 3)11 / 3 is about 3.67, so n is 4: 11 - 12 = -1.0.
Math.IEEEremainder(angle, 360)Angle in the range -180 to 180 degrees.
Math.IEEEremainder(5, 0)Division by zero: returns NaN (no exception).

There are no int or float overloads. For integers use % or Math.floorMod; for a double remainder with the dividend's sign use %.

Library declarations
Java
// java.lang.Math
public static double IEEEremainder(double f1, double f2)

Parameters

ParameterDescription
f1The dividend: the value to reduce.
f2The divisor: the value to divide by.

Return Value

Return ValueDescription
doublef1 - f2 * n, where n is the integer nearest to f1 / f2 (ties go to the even integer). The size is at most |f2| / 2. A zero result has the sign of f1.

Compare with Similar Methods

How the double remainder tools compare:

MethodHow it differs
f1 % f2Truncating remainder: sign of the dividend, size up to |f2|.
floorMod(x, y)Integer remainder with the sign of the divisor; for int/long only.
rint(a)The "nearest, ties to even" rounding that IEEEremainder uses for n.
ceilMod(x, y)Integer remainder paired with rounding up. Java 18+.

Java Examples

Example 1 — IEEEremainder Next to the % Operator

For each pair, print the whole number n that IEEEremainder picks, its result, and the result of %.

Java
public class MathIeeeRemainderBasics {
    public static void main(String[] args) {
        double[][] pairs = {{10, 3}, {11, 3}, {5, 2}, {7, 2}};
        for (double[] p : pairs) {
            double n = Math.rint(p[0] / p[1]);
            System.out.println("IEEEremainder(" + p[0] + ", " + p[1] + ") = "
                + Math.IEEEremainder(p[0], p[1]) + "  n=" + n + "  % gives " + (p[0] % p[1]));
        }
    }
}

How It Works

  1. Nearest below. 10 / 3 is 3.33, so n is 3 and the remainder is 10 - 3 * 3 = 1.0, the same value % gives.
  2. Nearest above. 11 / 3 is 3.67, so n is 4 and the remainder is 11 - 12 = -1.0; % stops at n = 3 and reports 2.0.
  3. Ties go to even. 5 / 2 is 2.5, so n is the even 2 and the result is 1.0, but 7 / 2 is 3.5 and the even neighbour is 4, so the result is 7 - 8 = -1.0.

Example 2 — Wrap Angles to the Range -180 to 180 Degrees

A heading can be any number of degrees. IEEEremainder(angle, 360) gives the shortest turn back to zero.

Java
public class MathIeeeRemainderAngles {
    public static void main(String[] args) {
        double[] angles = {370, 350, 190, -270, 180, 720};
        for (double a : angles) {
            System.out.println("angle " + a + " -> " + Math.IEEEremainder(a, 360)
                + " (plain % gives " + (a % 360) + ")");
        }
    }
}

How It Works

  1. Small turns stay small. 370 is 1.03 full turns, so n is 1 and the leftover is 10.0; 720 is exactly two turns, so the leftover is 0.0.
  2. Shortest way round. 350 degrees is 10 degrees short of a full turn, so IEEEremainder reports -10.0 where % reports 350.0; likewise 190 becomes -170.0.
  3. Negative angles and the tie. -270 / 360 is -0.75, so n is -1 and the result is -270 + 360 = 90.0. At exactly 180 the quotient is 0.5, a tie, so the even n = 0 keeps 180.0.

Example 3 — Zero Divisor, Infinity, NaN, and Signed Zero

The special cases never throw an exception, plus a famous floating-point surprise with 0.1.

Java
public class MathIeeeRemainderSpecial {
    public static void main(String[] args) {
        System.out.println("by zero:       " + Math.IEEEremainder(5.0, 0.0));
        System.out.println("infinite f1:   " + Math.IEEEremainder(Double.POSITIVE_INFINITY, 2.0));
        System.out.println("infinite f2:   " + Math.IEEEremainder(5.0, Double.POSITIVE_INFINITY));
        System.out.println("NaN:           " + Math.IEEEremainder(Double.NaN, 2.0));
        System.out.println("zero result:   " + Math.IEEEremainder(-6.0, 3.0));
        System.out.println("1.0 vs 0.1:    " + Math.IEEEremainder(1.0, 0.1));
        System.out.println("1.0 % 0.1:     " + (1.0 % 0.1));
    }
}

How It Works

  1. NaN cases. A zero divisor, an infinite dividend, or a NaN argument have no meaningful remainder, so the result is NaN and nothing is thrown.
  2. Infinite divisor. Any finite number is closer to 0 times infinity than to 1 times infinity, so n is 0 and the remainder is the dividend itself, 5.0. A zero result keeps the sign of the first argument: -6.0 and 3.0 give -0.0.
  3. The 0.1 surprise. The double 0.1 is a hair above one tenth, so 1.0 is slightly less than 10 times it: IEEEremainder returns n = 10 and a tiny negative leftover, while % truncates to n = 9 and returns nearly a whole 0.1.

Frequently Asked Questions

It returns the remainder of two doubles as defined by IEEE 754: f1 - f2 * n, where n is the integer closest to f1 / f2 (ties go to the even integer).
The % operator truncates the quotient toward zero, so its result has the sign of the dividend. IEEEremainder rounds the quotient to the nearest integer, so the result is at most half of the divisor in size and can be negative for positive inputs. For example Math.IEEEremainder(11, 3) is -1.0 while 11 % 3.0 is 2.0.
When either argument is NaN, when the dividend is infinite, or when the divisor is zero. If the dividend is finite and the divisor is infinite, the result is the dividend.
Use Math.IEEEremainder(angle, 360). It returns the shortest signed turn: 350 degrees becomes -10.0 and 190 becomes -170.0.
There is no int overload; integer arguments are widened to double. For integer remainders use the % operator or Math.floorMod, which stay exact for large long values.

Next: incrementExact()

Add 1 to an int or long and throw ArithmeticException on overflow.

incrementExact() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful