C++ std::fmod() Function

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

Definition

In C++, std::fmod(x, y) is the remainder operator for decimals. The % sign only works for whole numbers, so use fmod() when a value is a double. fmod(10.0, 3.0) is 1, because 10 = 3 * 3 + 1.

The quotient is cut toward zero, so the answer always has the sign of x. That makes fmod(-10.0, 3.0) equal to -1, not 2. It is exact: no rounding error is added. For a remainder that is closest to zero, use std::remainder().

std::fmod() 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::fmod. Start with these lines.

C++
double a = std::fmod(10.0, 3.0);     // 1
double b = std::fmod(-10.0, 3.0);    // -1
double c = std::fmod(5.5, 2.0);      // 1.5
CallWhat it does
std::fmod(10.0, 3.0)10 = 3 * 3 + 1. Returns 1.
std::fmod(-10.0, 3.0)The sign follows x, so the remainder is negative. Returns -1.
std::fmod(5.5, 2.0)Works with decimals. 5.5 = 2 * 2 + 1.5. Returns 1.5.

For two int values use the % operator. fmod() is for float, double, and long double.

Library declarations
C++
float       fmod(float x, float y);
double      fmod(double x, double y);
long double fmod(long double x, long double y);
// plus overloads for mixed and integer arguments

Parameters

ParameterDescription
xRequired. The dividend, the number that is divided. It can be float, double, long double, or an integer type (converted to double).
yRequired. The divisor, the number you divide by. It must not be zero.

Return Value

Return ValueDescription
RemainderThe value x - n * y, where n is the quotient cut toward zero. It has the sign of x.
NaNReturned when y is 0, or when x is infinity or NaN.
xIf y is infinity and x is a normal number, the result is x.

C++ Examples

Example 1 — Remainders of Several Divisions

Print the remainder for positive and negative numbers, a decimal, and an exact fit.

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

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

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

How It Works

  1. The array holds pairs of numbers, and each call is fmod(p[0], p[1]).
  2. 10 divided by 3 leaves 1. With a negative x, -10 leaves -1, so the sign follows the first number. A negative y does not change the sign: fmod(10, -3) is still 1.
  3. 5.5 divided by 2 leaves 1.5, and 7 divided by 7 fits exactly, so the remainder is 0.

Example 2 — Wrap an Angle into 0 to 360

An angle can be any size, but you often want it between 0 and 360. Use fmod(), then add 360 if the answer is negative.

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

int main() {
    for (double angle : {725.0, -30.0, 360.0}) {
        double wrapped = std::fmod(angle, 360.0);
        if (wrapped < 0) {
            wrapped += 360.0;
        }
        std::cout << angle << " -> " << wrapped << '\n';
    }
    return 0;
}

How It Works

  1. fmod(725, 360) removes two full turns (720) and leaves 5.
  2. fmod(-30, 360) is -30, because the sign follows x. The if adds 360 and gives 330.
  3. 360 is exactly one full turn, so the remainder is 0 and no fix is needed.

Example 3 — Add Hours on a 24-Hour Clock

It is 22.5 hours (22:30). Add some hours and use fmod() to wrap past midnight.

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

int main() {
    double now = 22.5;

    for (double added : {1.5, 5.25, 49.0}) {
        double result = std::fmod(now + added, 24.0);
        std::cout << now << " + " << added << " -> " << result << '\n';
    }
    return 0;
}

How It Works

  1. The sum 22.5 + 1.5 is exactly 24, which is one full day. The remainder is 0, which means midnight.
  2. 22.5 + 5.25 is 27.75, and fmod(27.75, 24) leaves 3.75, which is 3:45 in the morning of the next day.
  3. 22.5 + 49 is 71.5. Two days (48 hours) are removed, which leaves 23.5. Every sum here is positive, so no fix is needed.

Frequently Asked Questions

std::fmod(x, y) returns the remainder when x is divided by y, for decimal numbers. The result has the same sign as x, so fmod(-10.0, 3.0) is -1.
The remainder of x / y with the sign of x. If y is 0, or x is infinity, the result is NaN.
% works only with integers. fmod() works with float, double, and long double. Both give a result with the sign of the first number.
Check if the result is below zero and add the divisor: r = std::fmod(x, y); if (r < 0) r += y; (for a positive y). This is how you wrap angles and clock times.
Include <cmath> and write std::fmod(). That always works and picks the C++ overloads. The global name ::fmod often works too, but the standard does not promise it, and it may only take a double.

Next: fpclassify()

Keep going through the <cmath> functions. Tells whether a number is normal, zero, subnormal, infinite, or NaN.

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

13 people found this page helpful