C++ cmath remainder, remainderf and remainderl

The C++ <cmath> 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);

x -The first value.

y -The second argument

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

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

cout<< “remainder(2 , 3)=” << remainder(2 , 3) << endl ;
cout<< “fmod(2 , 3)=” << fmod(2 , 3) << endl ;

//Passing y=0
cout<< “remainder(2 , 0)=” << remainder(2 , 0) << endl ;

//Passing x=0
cout<< “remainder(-0.0 , 23)=” << remainder(-0.0 , 23) << endl ;


remainder(2 , 3)= -1
fmod(2 , 3)= 2
remainder(2 , 0)= nan
remainder(-0.0 , 23)= -0

Link: cmath fmod function

remainderf and remainderl

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

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

Code example

#include <typeinfo>

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

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


e (means ‘long double’)
e (means ‘long double’)