C++ Standard Library C++ STL Library

C++ <cmath> - modf() Function



The C++ <cmath> modf() function is used to break x into an integral and a fractional part. The integer part is stored in the object pointed by pointer y, and the fractional part is returned by the function. Both parts have the same sign as x.

Syntax

double modf (double x, double* y);
float modf (float x, float* y);
long double modf (long double x, long double* y);
double modf (double x, double* y);
float modf (float x, float* y);
long double modf (long double x, long double* y);
//additional overloads  
double modf (T x, double* y);                         

Parameters

x Specify the floating point value to break into parts.
y Specify the pointer to an object (of the same type as x) to store the integral part with the same sign as x.

Return Value

The fractional part of the x with same sign as of x.

Example:

The example below shows the usage of modf() function.

#include <iostream>
#include <cmath>
using namespace std;
 
int main (){
  double x, y, z;
  x = 10.55;

  z = modf(x, &y);

  cout<<"Number: "<<x<<"\n";
  cout<<"Integer: "<<y<<"\n";
  cout<<"Fraction: "<<z<<"\n";
  return 0;
}

The output of the above code will be:

Number: 10.55
Integer: 10
Fraction: 0.55

❮ C++ <cmath> Library