C Math fmod() Function

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
<math.h>

What You’ll Learn

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.

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.

📝 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

⚡ Quick Reference

CallDivisionResult
fmod(10.5, 3.2)10.5 ÷ 3.20.9
fmod(7.0, 3.5)Exact divide0.0
fmod(-10.5, 3.2)Negative x-0.9
fmod(370, 360)Wrap angle10.0
10 % 3Integer only1 (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

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.

Example 1 — Basic Floating-Point Remainder

Find the remainder when 10.5 is divided by 3.2.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double dividend = 10.5;
    double divisor = 3.2;
    double remainder = fmod(dividend, divisor);

    printf("%.2f / %.2f -> remainder %.2f\n",
           dividend, divisor, remainder);

    return 0;
}

How It Works

3 × 3.2 = 9.6. Subtract from 10.5 to get 0.9—the leftover after the largest whole number of divisors fit.

Example 2 — Remainder Sign Follows the Dividend

Negative x produces a negative remainder.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double y = 3.2;

    printf("fmod( 10.5, %.1f) = %5.1f\n", y, fmod( 10.5, y));
    printf("fmod(-10.5, %.1f) = %5.1f\n", y, fmod(-10.5, y));
    printf("fmod( 10.5, %.1f) = %5.1f\n", -y, fmod( 10.5, -y));

    return 0;
}

How It Works

The sign of the remainder matches x, not y. This differs from some mathematical conventions—know the C rule.

📈 Practical Patterns

Angle wrapping, identity check, and integer comparison.

Example 3 — Wrap an Angle into 0–360 Degrees

Keep a rotation value inside one full turn.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double angles[] = { 370.0, -45.0, 720.0 };
    size_t i;

    for (i = 0; i < 3; i++) {
        double a = angles[i];
        double wrapped = fmod(a, 360.0);
        if (wrapped < 0.0)
            wrapped += 360.0;

        printf("%6.0f deg -> %6.1f deg (0-360)\n", a, wrapped);
    }

    return 0;
}

How It Works

fmod alone can return negative values for negative angles. Add 360 when needed to land in [0, 360).

Example 4 — Verify the Remainder Identity

Check that x - fmod(x, y) is a multiple of y.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    double x = 10.5;
    double y = 3.2;
    double r = fmod(x, y);
    double quotient_part = x - r;
    double check = quotient_part / y;

    printf("x = %.1f, y = %.1f\n", x, y);
    printf("remainder r = %.1f\n", r);
    printf("x - r = %.1f\n", quotient_part);
    printf("(x - r) / y = %.1f  (whole number multiple)\n", check);

    return 0;
}

How It Works

Subtracting the remainder leaves exactly 3 divisors worth: 9.6 = 3 × 3.2. That is the core identity behind fmod.

Example 5 — fmod() vs Integer % Operator

Why you cannot use % for floating-point remainders.

C
#include <math.h>
#include <stdio.h>

int main(void) {
    /* Integer % truncates operands first if you cast */
    int a = 10;
    int b = 3;

    printf("Integer:  %d %% %d = %d\n", a, b, a % b);
    printf("Float:    fmod(%.1f, %.1f) = %.1f\n",
           10.5, 3.2, fmod(10.5, 3.2));
    printf("Wrong:    (int)10.5 %% (int)3.2 = %d  (truncated!)\n",
           (int)10.5 % (int)3.2);

    return 0;
}

How It Works

Casting to int before % loses decimals and gives 10 % 3 = 1, not 0.9. Use fmod for true floating-point remainder.

🚀 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.

📝 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.

⚡ 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.

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.

💡 Best Practices

✅ Do

  • Use fmod for floating-point remainders
  • Guard against y == 0 before calling
  • Normalize negative wraps (add period when needed)
  • Match type with fmodf / fmodl
  • Include <math.h> and link -lm

❌ Don’t

  • Use % on floats (cast truncates)
  • Confuse fmod with modf
  • Assume remainder sign follows the divisor
  • Ignore NaN from zero divisor
  • Expect fmod(x, y) to always be positive

Key Takeaways

Knowledge Unlocked

Five things to remember about fmod()

Floating-point remainder in C, explained simply.

5
Core concepts
📚 02

Sign of x

Dividend.

Rule
📈 03

vs %

Ints only.

Types
📄 04

Wrap

Angles.

Pattern
🔄 05

vs modf

Different.

Compare

❓ Frequently Asked Questions

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.

Explore C Math Functions

Continue with frexp() or browse the full math function reference.

Math Functions Index →

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.

5 people found this page helpful