The fmod() function computes the floating-point remainder after division—like % for integers, but for double values. The result keeps the sign of the dividend and is essential for wrapping angles, cyclic timers, and texture coordinates.
01
Remainder
Float mod.
02
math.h
Link -lm.
03
Sign of x
Dividend.
04
vs %
Ints only.
05
Wrap
Angles.
06
vs modf
Different.
Fundamentals
Definition and Usage
fmod(x, y) answers: “After dividing x by y, what is left over?” Mathematically, x = n × y + fmod(x, y) for some integer multiple n, where the remainder has the same sign as x.
Example: 10.5 ÷ 3.2 = 3 with remainder 0.9, because 3 × 3.2 = 9.6 and 10.5 − 9.6 = 0.9. So fmod(10.5, 3.2) is 0.9.
💡
Beginner Tip
The % operator only works on integers. For 10.5 and 3.2, you need fmod(10.5, 3.2). Do not confuse fmod with modf, which splits one number into integer and fractional parts.
Foundation
📝 Syntax
Standard C declaration:
C
double fmod(double x, double y);
Related variants
C
float fmodf(float x, float y); /* <math.h> */
long double fmodl(long double x, long double y); /* <math.h> */
Parameters
x — dividend (numerator).
y — divisor (denominator); must not be zero.
Return Value
Remainder of x / y with the sign of x.
|fmod(x, y)| is less than |y| (unless y is zero or x is infinite).
y == 0 returns NaN; may set errno to EDOM.
Headers and linking
#include <math.h>
Compile: gcc fmod.c -std=c11 -o fmod -lm
Cheat Sheet
⚡ Quick Reference
Call
Division
Result
fmod(10.5, 3.2)
10.5 ÷ 3.2
0.9
fmod(7.0, 3.5)
Exact divide
0.0
fmod(-10.5, 3.2)
Negative x
-0.9
fmod(370, 360)
Wrap angle
10.0
10 % 3
Integer only
1 (not fmod)
Float mod
fmod(x, y)
Remainder
Integer
x % y
Ints only
Split x
modf(x, &iptr)
Not fmod
Identity
x - fmod(x,y)
Divisible by y
Hands-On
Examples Gallery
Compile every example with gcc file.c -std=c11 -o out -lm. Remainder math shows up in graphics, physics, and scheduling code.
📚 Getting Started
Compute the remainder of a floating-point division.
Casting to int before % loses decimals and gives 10 % 3 = 1, not 0.9. Use fmod for true floating-point remainder.
Applications
🚀 Common Use Cases
Angle wrapping — keep rotations in [0, 360) or [−π, π).
Texture tiling — repeat UV coordinates across a surface.
Cyclic timers — elapsed time within a repeating period.
Grid snapping — offset within one cell of a lattice.
Physics — position along a periodic path or wave phase.
🧠 How fmod() Works
1
Receive x and y
Dividend and divisor as doubles. y must not be zero.
Input
2
Find quotient n
Compute integer multiple of y closest to x (trunc toward zero).
Compute
3
Return x − n×y
Remainder with sign of x, magnitude less than |y|.
Output
=
🔢
Remainder
Use for wrap-around math: cyclic indices and phases.
Important
📝 Notes
Result sign matches x (dividend), not y.
y == 0 → NaN; check divisor before calling.
Either argument NaN → result NaN.
Infinite x with finite y → NaN.
Not the same as modf() or integer %.
Link with -lm on GCC/Clang Unix-like builds.
Performance
⚡ Optimization
fmod() is slower than integer % because it handles IEEE-754 edge cases. For power-of-two divisors in performance-critical graphics code, bit masks may suffice for integers—but for true floating divisors like 3.2, stick with fmod.
Wrap Up
Conclusion
fmod() computes the floating-point remainder of division, preserving the sign of the dividend. Use it for decimal divisors, angle wrapping, and cyclic logic—not for splitting a number (modf) or integer modulo (%).
Continue with frexp() to break a number into mantissa and exponent, or review floor() for rounding.
fmod(x, y) returns the floating-point remainder of x divided by y. For example, fmod(10.5, 3.2) is 0.9 because 10.5 = 3 * 3.2 + 0.9. The result has the same sign as x (the dividend).
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double fmod(double x, double y).
The % operator works only on integers. fmod() works on floating-point values like 10.5 and 3.2. Use fmod when you need a remainder with decimal divisors.
fmod(x, y) divides x by y and returns the remainder. modf(x, &iptr) splits a single number x into a fractional part and an integer part stored via a pointer. They solve different problems.
The remainder has the same sign as the dividend x, not the divisor y. fmod(-10.5, 3.2) is -0.9; fmod(10.5, -3.2) is 0.9.
fmod(x, 0) returns NaN and may set errno to EDOM. Always check that the divisor is non-zero before calling fmod().
Did you know?
Clock arithmetic is modular math: 14:00 minus 16 hours lands on 22:00 the previous day. fmod does the same idea for continuous values—wrapping a ship’s heading past 360° or scrolling a texture past its tile width.