C programming modf ,modff and modfl math.h


In C programming the <math.h> modf,modff and modfl functions 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.

All the functions are the same,they disintegrate the argument into integral part and fractional part.The 1st,2nd and the 3rd function have the same name i.e.’modf’.The 4th and 5th function have different names.What does having a different names mean is explained in detail below.

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

double d=12.0546 , ipt=0 , x1=-INFINITY ;

float f=12.0546 ;

long double ld=12.0546 ;

printf(“%lf”, modf( d , &ipt ) ) ; //calls the 2nd version
printf(“\n%f”, modf( f , &ipt ) ) ; //calls the 1st version
printf(“\n%Lf”, modf( ld , &ipt ) ) ;//calls the 3rd version

printf(“\nipt=%lf”, ipt ) ;

printf(“\n%lf”, modf( x1 , &ipt ) ) ;
printf(“\n%lf”, modf( INFINITY , &ipt ) ) ;
printf(“\nipt=%lf”, ipt ) ;

Output in Visual Studio,

0.054600
0.054600
0.054600
ipt=12.000000
-0.000000
0.000000
ipt=inf

Here you can clearly see that ‘ipt’ store the integral value.And in passing INFINITY we get the value of ‘ipt’ as ‘inf'(infinity) which of course make sense.



modff

The ‘modff‘ function is same as the 1st function.This means the parameter and return type of the function is float type.The ‘f‘ character appended to ‘modff‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=11.7771 , ipt=0;

printf(“%f”, modf( x , &ipt) ); //Calls the 1st version
printf(“\n%f”, modff( 11.7771 , &ipt ) ); Calls the 4th version
printf(“\nipt=%f”, ipt ) ;

Output in Visual Studio,

0.7771
0.7771
ipt=11


modfl

The ‘modfl‘ function is same as the 3rd function.This means the parameter and return type of the function is long double type.The ‘l‘ character appended to ‘modfl‘ stands for long double and it signify the parameter type and return type of the function.

Code example

long double x=11.7771 , ipt=0 ;

printf(“%f”, modf( x , &ipt) ); //Calls the 3rd version
printf(“\n%f”, modfl( 11.7771 , &ipt ) ); Calls the 5th version
printf(“\nipt=%f”, ipt ) ;

Output in Visual Studio,

0.7771
0.7771
ipt=11