# C++ cmath fmod , fmodf and fmodl

The C++ <cmath> fmod,fmodf and fmodl function compute the remainder of the arguments passed.The declaration of the functions are given below.

1 float fmod(float x , float y);
2 double fmod(double x , double y);
3 long double fmod(long double x , long double y);
4 float fmodf(float x , float y);
5 long double fmodl(long double x , long double y);

Parameters:
x -The first argument value acting as dividend.

y -The second argument or the divisor.

Return type
floating point -The value of ‘x-ny’.

Some points to note:

i) The fmod(x,y) compute the value ‘x-ny‘,where ‘n’ is some integer value.

ii) For ‘y’ having finite value the returned value has the same sign as ‘x’ and magnitude less than the ‘y’.

iii) If ‘y’ is 0 domain error may occur or the function may return 0,it is implementation defined.

iv) If x<y, fmod returns ‘x’.

v) If x>y, the function returned the remainder of x/y.

vi) If ‘x’ is ±∞ , the function return ‘nan’.

vii) If ‘y’ is ±∞, but ‘x’ is some finite value the function return ‘x’.If ‘x’ and ‘y’ are both ±∞ the function return ‘nan’.

Code example

/*Passing x>y */
cout<< “fmod(34 , 23)=” << fmod( 34 ,23 ) <&lt endl ;

/*Passing ‘y=0’ */
cout<< “fmod(3 , 0)=” << fmod( 3 , 0 ) <&lt endl ;

/*Passing ‘x=∞’ */
cout<< “fmod(INFINITY , 8)=” << fmod( INFINITY , 0 ) <&lt endl ;

/*Passing ‘y=∞’ */
cout<< “fmod(23 , INFINITY)=” << fmod( 23 , INFINITY) <&lt endl ;

Output

fmod(34 , 23)= 14
fmod(3 , 0)= nan
fmod(INFINITY , 8)= nan
fmod(23 , INFINITY)= 23

#### fmodf and fmodl

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

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

Code example :Checking the return type of fmodf and fmodl.

#include <typeinfo>

/*fmodf */
cout<< “*fmodf\n”;
float f=2 , f1=67;
cout<< typeid( fmodf(21 , 3) ).name() << endl /*identify type of fmodf returned value */
<< typeid( fmod(f , f1) ).name() << endl; /*identify type of fmod(float,float) returned value */

/*fmodl*/
cout<< “*fmodl\n”;
long double ld=2 , ld1=7.5;
cout<< typeid( fmodl( 29 , 19) ).name() << endl /*identify type of fmodl returned value */
<< typeid( fmod(ld , ld1) ).name() << endl; /*identify type of fmod(long double , long double) returned value*/

Output(in Code::Block),

*fmodf
f
f
*fmodl
e (means ‘long double’)
e (means ‘long double’)

*Side Note

i)fmod(x , y) returns a ‘nan’ and raises the ‘in valid’ floating-point exception for ‘x’ infinite or ‘y’ zero.

ii) ‘fmod(double ,double)’ behave as though it is define as,

#include <cmath>
#include <fenv>

#pragma STDC FENV_ACCESS ON

double fmod(double x, double y)
{
double result ;

result = remainder( fabs(x), (y = fabs(y)) );

if ( signbit(result) )
result += y;

return copysign(result , x);
}