C++ std::remainder() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cmath>

Definition

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)
CallWhat 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

ParameterDescription
xRequired. The dividend. It can be float, double, long double, or an integer type (converted to double).
yRequired. The divisor. It must not be zero.

Return Value

Return ValueDescription
RemainderThe value x - n * y with n the nearest whole number to x / y. It lies between -|y|/2 and |y|/2.
0Returned when x is an exact multiple of y.
NaNReturned 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.

C++
#include <cmath>
#include <iostream>

int main() {
    double pairs[][2] = {{10, 3}, {11, 3}, {5, 2}, {7, 2}, {-11, 3}};

    for (auto& p : pairs) {
        std::cout << "remainder(" << p[0] << ", " << p[1] << ") = "
                  << std::remainder(p[0], p[1]) << '\n';
    }
    return 0;
}

How It Works

  1. Each pair is divided, then the quotient is rounded to the nearest whole number.
  2. 10 / 3 rounds to 3, leaving 1. 11 / 3 rounds up to 4, leaving 11 - 12 = -1.
  3. The ties 5 / 2 = 2.5 and 7 / 2 = 3.5 go to the even numbers 2 and 4, giving 1 and -1. -11 / 3 mirrors the second case and gives 1.

Example 2 — remainder() Next to fmod()

The two functions often give different answers. This table shows the same division done both ways.

C++
#include <cmath>
#include <iostream>

int main() {
    double pairs[][2] = {{11, 3}, {10, 4}, {-7, 4}};

    for (auto& p : pairs) {
        std::cout << p[0] << " / " << p[1]
                  << ": fmod " << std::fmod(p[0], p[1])
                  << ", remainder " << std::remainder(p[0], p[1]) << '\n';
    }
    return 0;
}

How It Works

  1. fmod() cuts the quotient toward zero. remainder() rounds it to the nearest whole number.
  2. For 11 / 3 the quotient is 3.67. fmod uses 3 and leaves 2, while remainder uses 4 and leaves -1.
  3. For 10 / 4 both give 2, because the tie goes to the even 2. For -7 / 4, fmod gives -3 and remainder gives 1.

Example 3 — Snap a Value to the Nearest Multiple of 5

The remainder tells you how far a value is from the closest multiple. Subtract it to move the value onto the grid.

C++
#include <cmath>
#include <iostream>

int main() {
    double step = 5.0;

    for (double x : {12.0, 14.0, 17.5}) {
        double offset = std::remainder(x, step);
        std::cout << x << " -> offset " << offset
                  << " -> snapped " << x - offset << '\n';
    }
    return 0;
}

How It Works

  1. The grid has a step of 5. remainder(12, 5) is 2, which means 12 is 2 above the multiple 10.
  2. Subtracting the offset gives 10. For 14 the offset is -1 (it is 1 below 15), so subtracting gives 15.
  3. 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.

Next: remquo()

Keep going through the <cmath> functions. IEEE remainder plus the low bits of the quotient.

remquo() 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.

9 people found this page helpful