modf() c++

modf() in C++

The modf() function in C++ returns the signed fractional part of the floating-point number x, and stores the integral part of x in the object pointed to by iptr.

Here's the syntax:

double modf(double x, double* iptr);
  • x: The input floating-point number.
  • iptr: A pointer to a double where the integral part of x will be stored.

The function returns the fractional part of x, and the integral part is written to *iptr.

Example:

#include <iostream>
#include <cmath>

int main() {
    double x = 3.14;
    double intpart, fracpart;
    fracpart = modf(x, &intpart);
    std::cout << "Integral part: " << intpart << std::endl;
    std::cout << "Fractional part: " << fracpart << std::endl;
    return 0;
}

In this example, modf() is used to separate the integral and fractional parts of the number x, and then the parts are printed to the console.