In C++, std::remainder(x, y) gives the IEEE remainder. It is x - n * y, but n is the whole number nearest to x / y, not the one cut toward zero. So the result is never larger than half of y.
For 11 / 3 the quotient 3.67 is nearest to 4, so the remainder is 11 - 12 = -1. A tie goes to the even n. This makes remainder() good for finding how far a value is from the closest multiple.
std::remainder() is declared in <cmath> and is part of the group Remainder, scale, and compare. These functions split a number, scale it by a power of two, find its remainder, or pick between two numbers. They work directly on the binary form of a double.
Syntax
Include <cmath> and call std::remainder. Start with these lines.
C++
double a = std::remainder(10.0, 3.0); // 1 (n = 3)
double b = std::remainder(11.0, 3.0); // -1 (n = 4)
double c = std::remainder(5.0, 2.0); // 1 (n = 2, tie to even)
Call
What it does
std::remainder(10.0, 3.0)
10 / 3 is 3.33, nearest whole is 3. Returns 10 - 9 = 1.
std::remainder(11.0, 3.0)
11 / 3 is 3.67, nearest whole is 4. Returns 11 - 12 = -1.
std::remainder(5.0, 2.0)
2.5 is a tie, and the even 2 is chosen. Returns 1.
Compare with std::fmod(), which cuts the quotient toward zero. The two functions agree only when the remainder is at most half of y.
Library declarations
C++
float remainder(float x, float y);
double remainder(double x, double y);
long double remainder(long double x, long double y);
// plus overloads for mixed and integer arguments
Parameters
Parameter
Description
x
Required. The dividend. It can be float, double, long double, or an integer type (converted to double).
y
Required. The divisor. It must not be zero.
Return Value
Return Value
Description
Remainder
The value x - n * y with n the nearest whole number to x / y. It lies between -|y|/2 and |y|/2.
0
Returned when x is an exact multiple of y.
NaN
Returned when y is 0, or when x is infinity or NaN.
C++ Examples
Example 1 — IEEE Remainders of Several Divisions
Print the remainder for a few pairs. Look for the negative answers.
The grid has a step of 5. remainder(12, 5) is 2, which means 12 is 2 above the multiple 10.
Subtracting the offset gives 10. For 14 the offset is -1 (it is 1below15), so subtracting gives 15.
17.5 is exactly halfway between 15 and 20. The tie goes to the even multiple 4 * 5 = 20, and the offset is -2.5.
Frequently Asked Questions
std::remainder(x, y) returns x minus n * y, where n is the whole number closest to x / y. The result is between -y/2 and y/2, so remainder(11.0, 3.0) is -1.
The IEEE remainder of x / y, in the range from -|y|/2 to |y|/2. If y is 0, or x is infinity, the result is NaN.
fmod() uses the quotient cut toward zero, so its answer has the sign of x. remainder() uses the quotient rounded to the nearest whole number, so its answer is between -y/2 and y/2 and can be negative.
11 / 3 is 3.67, which is nearest to 4. So the remainder is 11 - 4 * 3 = -1. It is the distance to the closest multiple of 3, which is 12.
Include <cmath> and write std::remainder(). That always works and picks the C++ overloads. The global name ::remainder often works too, but the standard does not promise it, and it may only take a double.