modf() Function
The modf()
function in C splits a floating-point value into its fractional and integral parts. The integral part is stored via a pointer provided by the user while the fractional part is returned by the function. Both parts retain the same sign as the original number, making it useful in various mathematical computations where such separation is required.
Syntax of modf()
double modf(double x, double *intpart);
Parameters
Parameter | Description |
---|---|
x | The floating point value to be split into its parts. |
intpart | Pointer to an object where the integral part of x will be stored (retaining the same sign as x ). |
Return Value
The function returns the fractional part of given argument x
, with the same sign.
It is important to note that the integral part is stored via the pointer provided, and no additional memory is allocated by modf()
. Both the integral and fractional parts will share the same sign as the original value.
Examples for modf()
Example 1: Splitting a Positive Double
This example demonstrates how to use modf()
to separate a positive double into its fractional and integral parts.
Program
#include <stdio.h>
#include <math.h>
int main() {
double num = 123.456;
double intPart;
double fracPart;
fracPart = modf(num, &intPart);
printf("Fractional part: %f\n", fracPart);
printf("Integral part: %f\n", intPart);
return 0;
}
Explanation:
- A double variable
num
is initialized with the value123.456
. - The
modf()
function is used to splitnum
into its fractional and integral parts. - The returned fractional part is stored in
fracPart
, while the integral part is stored at the location pointed to byintPart
. - Both parts are printed using
printf()
.
Program Output:
Fractional part: 0.456000
Integral part: 123.000000
Example 2: Splitting a Negative Double
This example shows how modf()
handles negative values by splitting a negative double into its respective parts while preserving the sign.
Program
#include <stdio.h>
#include <math.h>
int main() {
double num = -78.910;
double intPart;
double fracPart;
fracPart = modf(num, &intPart);
printf("Fractional part: %f\n", fracPart);
printf("Integral part: %f\n", intPart);
return 0;
}
Explanation:
- The double variable
num
is initialized with a negative value-78.910
. - The
modf()
function splits the value into fractional and integral components. - The fractional part (with a negative sign) is stored in
fracPart
and the integral part inintPart
. - The results are printed, demonstrating that the sign of both parts matches the original value.
Program Output:
Fractional part: -0.910000
Integral part: -78.000000
Example 3: Demonstrating modf() with User Input
This example demonstrates how to use modf()
by accepting a floating-point number from the user, and then splitting it into its fractional and integral parts.
Program
#include <stdio.h>
#include <math.h>
int main() {
double num, intPart, fracPart;
printf("Enter a floating point number: ");
scanf("%lf", &num);
fracPart = modf(num, &intPart);
printf("Fractional part: %f\n", fracPart);
printf("Integral part: %f\n", intPart);
return 0;
}
Explanation:
- The program prompts the user to enter a floating-point number.
- Once input is received,
modf()
is called to split the number into fractional and integral parts. - The fractional part is returned by
modf()
and the integral part is stored in the variable provided via pointer. - Both parts are then displayed to the user.
Program Output:
Enter a floating point number: 45.678
Fractional part: 0.678000
Integral part: 45.000000