The modf() function splits a floating-point number into its integral (whole-number) part and fractional part. The fractional part is returned; the integral part is written through a pointer. Handy for formatting money, parsing decimals, and any task that treats whole and fractional pieces separately.
01
Split
Two parts.
02
math.h
Link -lm.
03
Pointer
Stores int part.
04
Returns frac
|frac| < 1.
05
vs fmod
Not remainder.
06
Sign rules
Same as value.
Fundamentals
Definition and Usage
modf(value, &iptr) decomposes value into an integral part stored at *iptr and a fractional part returned by the function, such that:
value = integral + fractional
The fractional part has the same sign as value (or is zero) and its magnitude is less than 1. For 123.456, the integral part is 123.0 and the fractional part is 0.456.
💡
Beginner Tip
Do not confuse modf with fmod. modf splits one number; fmod(x, y) computes the remainder of division. The names look alike but the jobs are different.
Foundation
📝 Syntax
Standard C declaration:
C
double modf(double value, double *iptr);
Related variants
C
float modff(float value, float *iptr); /* <math.h> */
long double modfl(long double value, long double *iptr); /* <math.h> */
Parameters
value — the floating-point number to decompose.
iptr — pointer to double where the integral part is stored (must not be null).
Return Value
The fractional part of value as double.
Integral part written to *iptr.
modf(123.456, &w) returns 0.456 and sets w = 123.0.
Headers and linking
#include <math.h>
Compile: gcc modf.c -std=c11 -o modf -lm
Integral part is truncated toward zero (like trunc), not floored.
Cheat Sheet
⚡ Quick Reference
Call
Fractional return
Integral (*iptr)
modf(123.456, &w)
0.456
123.0
modf(7.0, &w)
0.0
7.0
modf(-3.75, &w)
-0.75
-3.0
modf(0.0, &w)
0.0
0.0
w + modf(v,&w)
Reconstructs v (finite values)
Split
modf(v, &w)
One number → two parts
Remainder
fmod(x, y)
Division leftover
Truncate
trunc(v)
Integral only
Identity
v = w + frac
Parts sum to whole
Hands-On
Examples Gallery
Compile every example with gcc file.c -std=c11 -o out -lm. modf is the standard way to peel off the decimal portion of a floating-point value in C.
📚 Getting Started
Split a positive floating-point number into integral and fractional parts.
Example 1 — Split 123.456
Classic example from the reference: decompose 123.456.
For production money code, prefer integer cents (1999) to avoid float rounding traps. modf still illustrates the split clearly for learners.
Example 5 — modf() vs fmod()
Same-sounding names, different jobs: split vs remainder.
C
#include <math.h>
#include <stdio.h>
int main(void) {
double value = 10.5;
double integral;
printf("value = %.1f\n\n", value);
printf("modf: splits into parts\n");
printf(" fractional = %.1f\n", modf(value, &integral));
printf(" integral = %.1f\n\n", integral);
printf("fmod: remainder of division\n");
printf(" fmod(%.1f, 3.2) = %.1f\n", value, fmod(value, 3.2));
return 0;
}
📤 Output:
value = 10.5
modf: splits into parts
fractional = 0.5
integral = 10.0
fmod: remainder of division
fmod(10.5, 3.2) = 0.9
How It Works
modf(10.5) gives 10 and 0.5. fmod(10.5, 3.2) asks how much is left after dividing by 3.2—completely unrelated to splitting decimals.
Applications
🚀 Common Use Cases
Number formatting — separate whole and decimal portions for display.
Financial UI — dollars vs cents (prefer integer cents in production).
Graphics — split texture coordinates into tile index and offset.
Time parsing — whole hours plus fractional hour remainder.
Custom rounding — process integral and fractional parts with different rules.
🧠 How modf() Works
1
Receive value
Any finite double (or float/long double variant).
Input
2
Truncate toward zero
Integral part stored at *iptr (like trunc).
Split
3
Return fractional part
value - integral, same sign, |frac| < 1.
Output
=
🔢
Two parts
integral + fractional == value.
Important
📝 Notes
Fractional part has the same sign as value (or is zero).
Integral part is truncated toward zero, not floored.
iptr must point to valid writable memory (not NULL).
NaN input → NaN returned; integral part unspecified.
±∞ input → returns ±0; integral part is ±∞.
Not the same as fmod() or integer %.
Link with -lm on GCC/Clang Unix-like builds.
Performance
⚡ Optimization
modf() is implemented in the platform math library and is typically fast. For very hot paths you might cast to integer types when you know the range fits—but modf handles negatives, large magnitudes, and IEEE edge cases correctly. Profile before replacing it.
Wrap Up
Conclusion
modf() splits a floating-point number into integral and fractional parts in one call. The fractional part is returned; the integral part is stored through a pointer. Use it for formatting and decomposition—not for division remainders (fmod).
Continue with pow() for exponentiation, or review trunc() when you only need the whole-number part.
Assume fractional part is always positive for negatives
Use float dollars for real money without integer cents
Expect floor behavior on the integral part
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about modf()
Split floats into parts in C, explained simply.
5
Core concepts
🔢01
Split
Two parts.
Basics
📚02
Pointer
Stores int.
API
📈03
|frac| < 1
Decimal bit.
Rule
📄04
Sign
Both match.
Negative
🔄05
vs fmod
Different.
Compare
❓ Frequently Asked Questions
modf(value, &iptr) splits a floating-point number into two parts: it returns the fractional part and stores the integral (whole-number) part at the address pointed to by iptr. For example, modf(123.456, &whole) returns 0.456 and whole becomes 123.0.
Include <math.h> and link with -lm on many systems: gcc program.c -o program -lm. The function is declared as double modf(double value, double *iptr).
modf splits one number into integer and fractional parts. fmod(x, y) returns the remainder of dividing x by y. They solve completely different problems despite similar names.
Both parts keep the sign of the original value. modf(-3.75, &iptr) returns -0.75 and stores -3.0 in iptr. The integral part is truncated toward zero, not floored.
Yes: value == iptr + modf(value, &iptr) for finite values (within floating-point rounding). That identity is the definition of how modf splits the number.
modff(value, &iptr) uses float types. modfl(value, &iptr) uses long double types. Use the variant matching your variable type.
Did you know?
The name modf comes from “modulus fraction”—the fractional part after removing the integer magnitude. It is one of the oldest math.h utilities, paired in design with functions like frexp that also decompose numbers for low-level numeric work.