Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

modf — splitting integral and fractional parts

The modf function from <math.h> returns the fractional part of a double and writes its integral part through a pointer. Both parts remain double values.

Version: C23; repository compiler flag, production runtime not attested

Syntax

#include <math.h>
double modf(double value, double *iptr);

modf(value, iptr) splits a number into integral and fractional parts. It returns the fractional part and stores the integral part in *iptr. Both parts have type double and retain the argument's sign.

For finite values, the integral part corresponds to truncation toward zero, not rounding downward: -3.75 produces -3.0 and -0.75. The integral part is not converted to an integer type.

Pass the address of a writable double object, such as &integral; neither NULL nor a pointer to int is valid. The related functions modff and modfl use float and long double, respectively.

Examples

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

int main(void)
{
    double integral;
    double fractional = modf(-3.75, &integral);

    printf("integral=%.2f\nfractional=%.2f\n", integral, fractional);
    return 0;
}

Result

integral=-3.00
fractional=-0.75

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources