C++ cmath modf,modff and modfl function


The C++ <cmath> modf,modff and modfl function disintegrate the argument into integral part and fractional part.The declaration of the functions are given below.

1 float modf(float x, float* ipt);
2 double modf(double x, double* ipt);
3 long double modf(long double x, long double* ipt);
4 float modff(float x, float* ipt);
5 long double modfl(long double x, long double* ipt);

Parameters:
x -The value to be disintegrated into integral part and fractional.

ipt -An object where the integral part of the ‘x’ is stored.

Return type
floating point-The fractional part of x.

Some points to note:

i) Both the integral and fractional part will have the same sign as the value ‘x’.And their type is also same with the ‘x’ argument.

ii) If 0 is passed 0 is returned and ipt stores 0.

iii) If NAN is passed ‘nan’ is returned and ipt stores ‘nan’.

iv) If ±∞ is passed, ±0 is returned and ipt stores ±∞.

Code example

/*Passing floating point value*/
double ipt;
cout<< “modf(2.34 , &ipt)=” << modf(2.34 , &ipt) << endl;
cout<< ipt< < endl;

/*Passing 0 */
cout<< “modf(0 , &ipt)=” << modf(0, &ipt) << endl;
cout<< ipt << endl;

/*Passing NAN */
cout<< “modf(NAN , &ipt)=” << modf(NAN , &ipt) << endl;
cout<< ipt << endl;

/*Passing INFINITY */
cout<< “modf(-INFINITY , &ipt)=” << modf(-INFINITY , &ipt) << endl;
cout<< ipt << endl;

Output

modf(2.34 ,&ipt)= .34
2
modf(0 ,&ipt)= 0
0
modf(NAN , &ipt)=NAN
NAN
modf(-INFINITY , &ipt)= -0
-inf


 


modff and modfl

*Note

i)The 4th version modff is same as the 1st version float modf(float, float).The ‘f‘ character stands for ‘float’ which signify the argument and return type of the function.

ii)The 5th version modfl is same as the 3rd version long double modf(long double , long double).The ‘l‘ character stands for ‘long double’ which signify the argument and return type of the function.

Code example

#include <typeinfo>

/*modff */
cout<< “*modff\n”;
float f=2 , f1;
cout<< typeid( modff(2 ,f1) ).name() << endl /*identify type of modff retunred value */
<< typeid( modf(f ,f1) ).name() << endl; /*identify type of modf(float) returned value */

/*modfl*/
cout<< “*modfl\n”;
long double ld=2 , ld1;
cout<< typeid( modfl(2 , ld1) ).name() << endl /*indentify type of modfl returned value */
<< typeid( modf(ld , ld1) ).name() << endl; /*identify type of modf(long double) returned value*/

Output

*modff
f
f
*modfl
e (means ‘long double’)
e (means ‘long double’)