C++ cmath fmax,fmaxf and fmaxl


The C++ <cmath> fmax,fmaxf and fmaxl determine the maximum numeric values of their arguments.The declaration of the functions are given below.

1 float fmax(float x , float y);
2 double fmax(double x , double y);
3 long double fmax(long double x , long double y);
4 float fmaxf(float x , float y);
5 long double fmaxl(long double x , long double y);

Parameters:
x -The first argument.

y -The second argument.

Return type
floating point -The greater value of x and y is returned.

Some points to note:

i) If x≥y ,x is returned.

ii) If x is +∞ and y is some valid value ,+∞ is returned.If x is -∞ and y is some valid value ,y is returned.

iii) If one of the arguments is NAN,the other argument is returned.If one is NAN and the other ±∞ , ±∞ is returned.

Code example

cout<< “fmax(23 , 0)=” << fmax(23 , 0) << endl;

/*Passing ∞ */
cout<< “fmax(INFINITY , 9934)=” << fmax(INFINITY , 9934) << endl;

/*passing NaN */
cout<< “fmax(NAN , 0)=” << fmax(NAN , 0) << endl;

Output

fmax(23 , 0)= 23
fmax(INFINITY , 9934)= inf
fmax(NAN , 0)= 0





fmaxf and fmaxl

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

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

Code example

#include <typeinfo>

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

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

Output

*fmaxf
f
f
*fmaxl
e (means ‘long double’)
e (means ‘long double’)
 


*Side Note

The fmax function may be defined as:

 double fmax(double x , double y)
 { return ( isgreaterequal(x , y) ||
  isnan( y ) ) ? x : y;
 }

Link: C++ cmath isnan()


Related links

->C programming fmax,fmaxf and fmaxl