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 wantfmodforfmodl, you gotta define one of these macros. The_DEFAULT_SOURCEone only works since glibc 2.19, and if you're on glibc 2.19 or older you need_BSD_SOURCEor_SVID_SOURCEinstead. 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
xoryis a NaN, a NaN is returned. NaN in, NaN out, it's giving chaos. ๐ญ - If
xis an infinity, a domain error occurs, and a NaN is returned. - If
yis zero, a domain error occurs, and a NaN is returned. (obviously, dividing by zero, we been knew ๐ซถ) - If
xis+0(-0), andyis 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:
xis an infinity โerrnois set toEDOM(but see BUGS, bestie, there's lore ๐). An invalid floating-point exception (FE_INVALID) is raised. - Domain error:
yis zero โerrnois set toEDOM. 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
doublealso conforms toSVr4,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
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms