Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function - modf()

C Library Function - modf()

C Standard Library - <math.h> C Standard Library -

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

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

Use Cases

modf() The function has a wide range of uses in many applications, including but not limited to:

Notes

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 - <math.h> C Standard Library -