In C, modf() splits x into a fraction and a whole-number part. The call returns the fraction and writes the whole part through a pointer. modf(3.25, &whole) returns 0.25 and sets whole to 3.
You pass the double and a pointer to a double. Both are required. Both parts keep the sign of x. Include <math.h>.
Syntax
Pass the value and the address of a double. Start with these lines.
C
double whole;
double frac = modf(3.25, &whole);
Call
What it does
modf(3.25, &whole)
Returns 0.25 and stores 3 in whole.
Include <math.h>. On GCC, link with -lm. The pointer must point to a double you own.
Library declarations
C
double modf(double x, double *iptr);
float modff(float x, float *iptr);
long double modfl(long double x, long double *iptr);
Parameters
Parameter
Description
x
Required. The double to split.
iptr
Required. A pointer to a double. The function writes the whole-number part there.
Return Value
Return Value
Description
Fraction
The part after the decimal point, with the same sign as x.
C Examples
Example 1 — Split a Positive Number
Store the whole part through a pointer; print the returned fraction.