C++ cmath fdim , fdimf and fdiml


1 float fdim(float x , float y);
2 double fdim(double x , double y);
3 long double fdim(long double x , long double y);
4 float fdim(float x , float y);
5 long double fdim(long double x , long double y);

The C++ <cmath> fdim,fdimf and fdiml compute the positive difference between the two arguments.The declaration of the functions are given below

Parameters:
x -The value from which the second(argument) value is to be subtracted.

y -The value which is use for subtraction.

Return type
floating point -The positive difference between x and y.

Some points to note:

i) fdim follows certain rule.It returns the resultant value of ‘x-y‘ if x>y,and it gives +0 if x≤y.

ii) If x is +∞, ∞ is returned,if x is -∞, 0 is returned.

iii) If x is NaN , ‘nan’ is returned.

Code example

cout<< “fdim(23 , 9)=” << fdim(23 , 9) << endl
<< “fdim(3 , 4)=” << fdim(3 , 4) << endl ;

/*Passing infinity*/
cout<< “fdim(INFINITY , 0)=” << fdim(INFINITY , 0) << endl
<< “fdim(-INFINITY , 0)=” << fdim(-INFINITY , 0) << endl;

Output

fdim(23 , 9)= 14
fdim(3 , 4)= 0
fdim(INFINITY , 0)= inf
fdim(-INFINITY , 0)= 0





fdimf and fdiml

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

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

Code example

#include <typeinfo>

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

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

Output

*fdimf
f
f
*fdiml
e (means ‘long double’)
e (means ‘long double’)