C programming remainder, remainderf and remainderl


In C programming the <math.h> remainder,remainderf and remainderl function compute the remainder of the arguments passed.The declaration of the functions are given below.

1 float remainder(float x , float y);
2 double remainder(double x , double y);
3 long double remainder(long double x , long double y);
4 float remainderf(float x , float y);
5 long double remainderl(long double x , long double y);

Parameters:
x -The first value.

y -The second argument

Return type
floating point -The value ‘x REM y’.

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.’remainder’.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 remainder(x , y) compute x REM y.

  x REM y,is literally defined as given by the relation ‘x-ny‘,where ‘n’ is nearest integer to the exact value of ‘x/y’.If |n-x/y|=1/2, then ‘n’ is even.

ii) If the the return value is 0 ,the sign is same as the sign of x.

iii) If y is 0 whether domain error occur or the function return 0 is implementation defined.

iv) If y is ±∞, and x is ‘nan’ or ±∞ ,then the function return ‘nan’ , else(y=±∞ , y=nan ) it return ±x.

iv) If x is ±0, and y is ±∞ or some non-zero floating point value,the function return ±0, else(y=0 or y=nan) it return ‘nan’.

v) If ±∞ the function return ‘nan’.

vi) If the argument passed are int type the default version is called-the overloaded version with double as argument type.

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”, remainder( dx , dy) ) ; //calls the 2nd version
printf(“\n%f”, remainder( fx , fy) ) ; //calls the 1st version
printf(“\n%Lf”, remainder( ldx , ldy) ) ;//calls the 3rd version

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

Output in Visual Studio ,

-0.440000
-0.440002
-0.440000
4.350000
nan



remainderf

The ‘remainderf‘ 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 ‘remainderf‘ 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”, remainder( x , y) ); //Calls the 1st version
printf(“\n%f”, remainderf( 90.23 , -3) ); Calls the 4th version

Output in Visual Studio,

0.230003
0.230003


remainderl

The ‘remainderl‘ 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 ‘remainderl‘ 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”, remainder( x , y) ); //Calls the 3rd version
printf(“\n%Lf”, remainderl( 90.23 , -3) ); Calls the 5th version

Output in Visual Studio,

0.230000
0.230000


Related links

->C programming fmod,fmodf and fmodl math.h