C programming math.h function - double modf(double x, double *integer)

https‮/:‬/www.theitroad.com

The C programming modf function is defined in the math.h header file and is used to break down a given value x into its integer and fractional parts. The modf function returns the fractional part of x as a double, and stores the integer part of x in the memory location pointed to by the integer argument.

The modf function takes two arguments of type double. The first argument, x, represents the value to be broken down into its integer and fractional parts. The second argument, integer, is a pointer to a double variable where the integer part of x will be stored.

Here's an example usage of the modf function to break down a given input into its integer and fractional parts:

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

int main() {
    double x = 3.14;
    double integer_part;
    double fractional_part = modf(x, &integer_part);
    
    printf("The integer part of %f is %f\n", x, integer_part);
    printf("The fractional part of %f is %f\n", x, fractional_part);
    
    return 0;
}

In this example, the modf function is used to break down the value 3.14 into its integer and fractional parts. The integer part is stored in the integer_part variable, which is passed to the modf function as a pointer. The fractional part is stored in the fractional_part variable, which is returned by the modf function. The integer and fractional parts are then printed to the console using the printf function.

Note that the modf function can also be used to break down negative numbers, in which case the integer part will be negative and the fractional part will be between 0 and -1.