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
Call
What 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
Parameter
Description
x
Required. The dividend, the number that is divided. It can be float, double, long double, or an integer type (converted to double).
y
Required. The divisor, the number you divide by. It must not be zero.
Return Value
Return Value
Description
Remainder
The value x - n * y, where n is the quotient cut toward zero. It has the sign of x.
NaN
Returned when y is 0, or when x is infinity or NaN.
x
If 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.
The array holds pairs of numbers, and each call is fmod(p[0], p[1]).
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.
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.
The sum 22.5 + 1.5 is exactly 24, which is one full day. The remainder is 0, which means midnight.
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.
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.