# C programming fmod , fmodf and fmodl math.h

In C programming the <math.h> 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’.

All the functions are the same,they compute the remainder of the arguments passed.The 1st,2nd and the 3rd function have the same name i.e.’fmod’.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) 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

double dx=89.56 , dy=45 , x1=4.35 , x2=-9 ;

float fx=89.56 , dy=45 ;

long double ldx=89.56 , ldy=45 ;

printf(“%lf”, fmod( dx , dy) ) ; //calls the 2nd version
printf(“\n%f”, fmod( fx , fy) ) ; //calls the 1st version
printf(“\n%Lf”, fmod( ldx , ldy) ) ;//calls the 3rd version

printf(“\n%lf”, fmod( x1 , x2) ) ;
printf(“\n%lf”, fmod( INFINITY , 9) ) ;

Output in Visual Studio ,

44.560000
44.559998
44.560000
4.350000
-nan(ind)

Note here it is stated that if x<y the returned value is ‘x’,but in case of 4th output ‘x2<x1′(y<x) but we still get ‘x1’ as the output because we are considering the absolute value of the x2.And since the absolute value of x2 i.e 9,is greater than x1 we get x1 as the output.

#### fmodf

The ‘fmodf‘ 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 ‘fmodf‘ stands for float and it signify the parameter type and return type of the function.

Code example

float x=90.23 , y=-3 ;

printf(“%f”, fmod( x , y) ); //Calls the 1st version
printf(“\n%f”, fmodf( 90.23 , -3) ); Calls the 4th version

Output in Visual Studio,

0.230003
0.230003

#### fmodl

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

Code example

long double x=90.23 , y=-3 ;

printf(“%Lf”, fmod( x , y) ); //Calls the 3rd version
printf(“\n%Lf”, fmodl( 90.23 , -3) ); Calls the 5th version

Output in Visual Studio,

0.230000
0.230000

*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);
}