A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
The % operator only works on integers. Apply it to doubles and you get a compile error. fmod is the floating-point equivalent: it computes the remainder of x / y as x - n * y, where n is the quotient truncated toward zero. The result has the same sign as x. This is used in angle normalization, periodic calculations, and any domain where you need the remainder of a non-integer division.
#include <stdio.h>#include <math.h>int main(void) {printf("fmod(10.5, 3.0) = %.4f\n", fmod(10.5, 3.0));printf("fmod(-10.5, 3.0) = %.4f\n", fmod(-10.5, 3.0));printf("fmod(10.5, -3.0) = %.4f\n", fmod(10.5, -3.0));printf("fmod(7.0, 2.5) = %.4f\n", fmod(7.0, 2.5));double angle = 370.0;double normalized = fmod(angle, 360.0);printf("\n370 degrees normalized: %.1f degrees\n", normalized);return 0;}
The sign follows the dividend, matching integer % behavior. fmod(10.5, -3.0) is positive because 10.5 is positive. The angle normalization example is the most common practical use: wrapping degrees into the 0-360 range. For negative angles, you might need to add 360 if the result is negative: fmod(fmod(angle, 360.0) + 360.0, 360.0).
C99 introduced remainder, which rounds the quotient to the nearest integer instead of truncating. fmod(7.0, 4.0) is 3.0 (quotient 1, truncated). remainder(7.0, 4.0) is -1.0 (quotient 2, rounded). The choice depends on whether you want the result closer to zero (fmod) or closer to the nearest multiple (remainder). For most applications, fmod is what you expect.
fmod fills the gap that % leaves for floating-point code. It is in every math library, every physics simulation, every graphics engine. Next: the rounding functions, round, ceil, floor, and trunc, and how they differ in edge cases.
Finish the lesson once you have worked through the material. This awards ★ 20 XP.