C Library Function - modf()
Description
C library function *double modf(double x, double integer) The return value is the fractional part (the part after the decimal point), and integer is set to the integral part.
modf() is a function in the C standard library a function in the C standard library that splits a floating-point number into an integral part and a fractional part. It is typically used in scenarios where the integral and fractional parts of a floating-point number need to be separated.
Declaration
The following is the declaration of the modf() function.
#include
double modf(double x, double *iptr);
float modff(float x, float *iptr);
long double modfl(long double x, long double *iptr);
Parameters
- x -- Floating-point value.
- integer -- A pointer to an object that stores the integral part.
Return Value
This function returns the fractional part of x, with the same sign as x.
Example
The following example demonstrates the use of the modf() function.
Example
#include
#include
int
main
(
)
{
double
x
,
fractpart
,
intpart
;
x
=
8.123456
;
fractpart
=
modf
(
x
,
&
intpart
)
;
printf
(
"integer part = %lf\n"
,
intpart
)
;
printf
(
"fractional part = %lf \n"
,
fractpart
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
整数部分 = 8.000000
小数部分 = 0.123456
Splitting multiple values
The following example shows how to split multiple floating-point numbers:
Example
#include
#include
int
main
(
)
{
double
values
[
]
=
{
123.456
,
123.456
,
0.0
,
1.0
,
1.0
}
;
int
num_values
=
sizeof
(
values
)
/
sizeof
(
values
[
0
]
)
;
for
(
int
i
=
0
;
i
<
num_values
;
i
++
)
{
double
x
=
values
[
i
]
;
double
intpart
;
double
fracpart
;
fracpart
=
modf
(
x
,
&
intpart
)
;
printf
(
"Original value: %f\n"
,
x
)
;
printf
(
"Integer part: %f\n"
,
intpart
)
;
printf
(
"Fractional part: %f\n"
,
fracpart
)
;
printf
(
"\n"
)
;
}
return
0
;
}
Let's compile and run the above program, which will produce the following result:
Original value: 123.456000
Integer part: 123.000000
Fractional part: 0.456000
Original value: -123.456000
Integer part: -123.000000
Fractional part: -0.456000
Original value: 0.000000
Integer part: 0.000000
Fractional part: 0.000000
Original value: 1.000000
Integer part: 1.000000
Fractional part: 0.000000
Original value: -1.000000
Integer part: -1.000000
Fractional part: -0.000000
Code Analysis
- Define an array containing multiple floating-point numbers
values. - Use
for, and loop through each value, calling themodf(x, &intpart)Perform the splitting. - Print the original value, integral part, and fractional part of each value.
Use Cases
modf() The function has a wide range of uses in many applications, including but not limited to:
- Separate the integral and fractional parts of floating-point numbers for individual processing.
- Scenarios where calculations require the integral and fractional parts to be handled separately.
- Scenarios in scientific computing and numerical analysis that require precise handling of the fractional part.
Notes
modf()When handling positive and negative numbers, the function keeps the signs of the integral and fractional parts consistent. For example, for -123.456, the integral part is -123.0 and the fractional part is -0.456.
Summary
modf() The function is used to split a floating-point number into its integral and fractional parts, and is an important tool for handling floating-point operations. Through proper use of modf(), it enables precise decomposition of floating-point numbers in scientific computing, numerical analysis, and engineering applications, and ensures separate handling of the integral and fractional parts.
C Standard Library -