C++ Math modf
This function is used to divide a number into integral and fractional part.
For example :
Example
2.16 = 2 + 16
Syntax
Suppose a number is 'x' and 'ptr' is the pointer to an integral part.
Example
float modf(float x, float* ptr);
double modf(double x, double* ptr);
long double modf(long double x, long double* ptr);
double modf(integral x, double* ptr);
Parameter
x : The value which is to be broken into two parts i.e(fractional and integral part).
ptr : It is the pointer to an object where the integral part of x is stored.
Return value
It returns the integral part of x.
Example 1
Let's see a simple example
Example
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x=18.26;
double ptr;
float i=modf(x,&ptr);
std::cout << "Value of x is : " <<x <<std::endl;
cout<<"integral part of x is :"<<ptr<<'\n' ;
cout<<"fractional part of x is :"<<i;
return 0;
}
Output:
Output
Value of x is : 18.26
integral part of x is :18
fractional part of x is :0.26
In this example, modf function breaks a number into fractional and integral part. Fractional part is 0.26 and integral part is 18.
Example 2
Let's see a simple example when the value of x is negative.
Example
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
float x= -78.34;
double ptr;
float n=modf(x,&ptr);
std::cout << "Value of x is : " <<x <<std::endl;
cout<<"integral part of x is :"<<ptr<<'\n' ;
cout<<"fractional part of x is :"<<n;
return 0;
}
Output:
Output
Value of x is : -78.34
integral part of x is :-78
fractional part of x is :-0.339996