modf

Separates a floating point number into integer and fractional parts

double modf (
        double x,
        double *y
)

Arguments

x Floating point number
y Pointer to a buffer for storing the integer part

Return value

The fractional part of x is returned.

Explanation

x is separated into an integer part and a fractional part. The integer part is placed at the storage location pointed to by y. The fractional part is returned by the function.

Notes

The sign for both integer parts and fractional parts will be the same as the sign of x.

Example

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

void main(void)
{
    double x, y, n;
    x = -14.87654321; // Divide x into its fractional
    y = modf( x, &n ); // and integer parts
    printf( "For %f, the fraction is %f and the integer is %.f\n", x, y, n);
}

Output:

For -14.876543, the fraction is -0.876543 and the integer is -14