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)
Call
What 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
Parameter
Description
f1
The dividend: the value to reduce.
f2
The divisor: the value to divide by.
Return Value
Return Value
Description
double
f1 - 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:
Method
How it differs
f1 % f2
Truncating remainder: sign of the dividend, size up to |f2|.
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.
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.
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.
by zero: NaN
infinite f1: NaN
infinite f2: 5.0
NaN: NaN
zero result: -0.0
1.0 vs 0.1: -5.551115123125783E-17
1.0 % 0.1: 0.09999999999999995
How It Works
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.
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.
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.