C library functions -modf()

C standard library - <math.h>

Description

C Library Functionsdouble modf(double x, double *integer)The return value is the fractional part (the portion after the decimal point), and sets integer as the integer part.

modf()is the C standard library<math.h>a function used to decompose a floating-point number into an integer part and a fractional part. It is often used in scenarios where a floating-point number needs to be separated into integer and fractional parts.

Declaration

The following is the declaration of the modf() function.

#include <math.h>

double modf(double x, double *iptr);
float modff(float x, float *iptr);
long double modfl(long double x, long double *iptr);

Parameter

  • x-- floating-point value.
  • integer-- a pointer to an object that stores the integer part.

Return Value

The function returns the fractional part of x, with the same sign as x.

Example

The following example demonstrates the usage of the modf() function.

Example

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

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 us compile and run the above program, which will produce the following result:

整数部分 = 8.000000
小数部分 = 0.123456 

Handling decomposition of multiple values

The following example demonstrates how to handle the decomposition of multiple floating-point numbers:

Example

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

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 us 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 explanation

  • Define an array containing multiple floating-point numbersvalues。
  • UsageforLoop through each value and callmodf(x, &intpart)Perform the decomposition.
  • Print the original value, integer part, and fractional part of each value.

Use Case

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

  • Separating the integer and fractional parts of a floating-point number for individual processing.
  • Scenarios in calculations where the integer part and fractional part need to be processed separately.
  • 科学ComputeandvalueDivide析Mediumrequires精confirmprocessingDecimalPartofScenarios。 Scenarios in scientific computing and numerical analysis that require precise handling of the fractional part.

Notes

  • modf()When the function processes positive and negative numbers, the signs of the integer part and the fractional part remain consistent. For example, for -123.456, the integer part is -123.0 and the fractional part is -0.456.

Summary

modf()The function is used to decompose a floating-point number into an integer part and a fractional part, making it an important tool for handling floating-point arithmetic. Through rational usemodf()It is possible to achieve precise decomposition of floating-point numbers in scientific computing, numerical analysis, and engineering applications, and to ensure separate processing of the integer and fractional parts.

C standard library - <math.h>

other extensions