fmod(3)

๐Ÿ’– fmod โ€” the floating-point remainder function, bestie ๐Ÿ’–

okay so this is page fmod(3) from the Library Functions Manual, and it's literally just three lil functions that do remainder math but โœจfloating pointโœจ. Let's get into it.


๐ŸŒธ NAME ๐ŸŒธ

fmod, fmodf, fmodl โ€” the floating-point remainder function, aka "what's left over when you divide x by y but make it decimals" ๐ŸŽ€


๐Ÿ“š LIBRARY ๐Ÿ“š

Math library, girly. That's libm, and you link it with -lm. Don't forget it, iykyk.


โœจ SYNOPSIS โœจ

#include <math.h>

double fmod(double x, double y);
float fmodf(float x, float y);
long double fmodl(long double x, long double y);

๐Ÿ” Feature Test Macro Requirements for glibc (see feature_test_macros(7)):

For fmodf() and fmodl():

_ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
    || /* Since glibc 2.19: */ _DEFAULT_SOURCE
    || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
๐Ÿ‘‘ Translation: if you're on glibc and you want fmodf or fmodl, you gotta define one of these macros. The _DEFAULT_SOURCE one only works since glibc 2.19, and if you're on glibc 2.19 or older you need _BSD_SOURCE or _SVID_SOURCE instead. No cap. ๐Ÿ’…

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So bestie, here's the vibe: these functions compute the floating-point remainder of dividing x by y.

The return value is:

x - n * y

where n is the quotient of x / y, rounded toward zero to an integer. Like, it just chops off the decimals on the quotient and then does the math backwards. That's it, that's the whole gig. ๐Ÿ™ˆ


okay now here's the important part โ€” if what you actually wanted was the modulus, more specifically the Least Positive Residue, you're gonna have to fix up the result from fmod() yourself, like this:

z = fmod(x, y);
if (z < 0)
     z += fabs(y);

Because fmod() can hand you back a negative number (it matches the sign of x, reminder!) and sometimes you want the positive one, and the universe refuses to just do that for you. ๐Ÿ’€

There's an alternate way to express the same thing:

fmod(fmod(x, y) + y, y)

but the second fmod() usually costs way more than the one branch. So like, slay if you want, but the if statement is the move. ๐Ÿ’–


๐ŸŒˆ RETURN VALUE ๐ŸŒˆ

On success, these functions return the value x - n*y, for some integer n, such that the returned value has the same sign as x and a magnitude less than the magnitude of y. That's the contract, okay? The sign follows x, always.

  • If x or y is a NaN, a NaN is returned. NaN in, NaN out, it's giving chaos. ๐Ÿ˜ญ
  • If x is an infinity, a domain error occurs, and a NaN is returned.
  • If y is zero, a domain error occurs, and a NaN is returned. (obviously, dividing by zero, we been knew ๐Ÿซถ)
  • If x is +0 (-0), and y is not zero, +0 (-0) is returned. Yes the signs are preserved. yes it matters. yes it's iconic.

๐ŸŽ€ ERRORS ๐ŸŽ€

Head over to math_error(7) for the whole "how do I even know if an error happened" situation when calling these functions. It's a whole thing.

The following errors can occur:

  • Domain error: x is an infinity โ€” errno is set to EDOM (but see BUGS, bestie, there's lore ๐Ÿ‘€). An invalid floating-point exception (FE_INVALID) is raised.
  • Domain error: y is zero โ€” errno is set to EDOM. An invalid floating-point exception (FE_INVALID) is raised.

๐Ÿ’… ATTRIBUTES ๐Ÿ’…

For the full explanation of what these terms even mean, go peek at attributes(7). Here's the tea for this one:

  • fmod(), fmodf(), fmodl() โ€” Thread safety: MT-Safe โœ…

(yeah, the original has a whole lil table, but it's just that one row, so you're not missing anything. ๐Ÿซถ)


โญ STANDARDS โญ

C11, POSIX.1-2008. Certified. ๐Ÿ’…


๐Ÿ“œ HISTORY ๐Ÿ“œ

  • C99, POSIX.1-2001.
  • The variant returning double also conforms to SVr4, 4.3BSD, C89. She's been around. She's a legend. ๐Ÿ‘‘

๐Ÿ’€ BUGS ๐Ÿ’€

Okay so before glibc 2.10, the glibc implementation did not set errno to EDOM when a domain error occurred for an infinite x. So if you're on ancient glibc and x is an infinity, don't go trusting errno, she lied. That's the bug, that's why the ERRORS section said "but see BUGS" earlier. ๐Ÿ™ˆ


๐Ÿ“ EXAMPLES ๐Ÿ“

The call fmod(372, 360) returns 12.

The call fmod(-372, 360) returns -12. (see? sign follows x, not y ๐Ÿ”ฅ)

The call fmod(-372, -360) also returns -12. (yup, still follows x โœจ)


๐ŸŒธ SEE ALSO ๐ŸŒธ


anyway that's the whole page, mwah ๐Ÿ’– stay safe out there and always check the sign of your remainder bestie, it's giving main character energy and it will absolutely ruin your math if you don't.

sending love from the Linux man-pages crew โ€” this is version 6.20, dated 2026-02-08, and that's a wrap on fmod(3) ๐ŸŽ€โœจ๐Ÿ‘‘

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms