C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Math modf()This function is used to divide a number into integral and fractional part. For example : 2.16 = 2 + 16 Syntax
Suppose a number is 'x' and 'ptr' is the pointer to an integral part. 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 1Let's see a simple 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: 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 2Let's see a simple example when the value of x is negative.
#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: Value of x is : -78.34 integral part of x is :-78 fractional part of x is :-0.339996
Next TopicC++ Math Functions
|