C programming fmax,fmaxf and fmaxl math.h


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

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

double dx=1200 , dy=1201 , x1=10.00 , x2=10 ;

float fx=1200, fy=1201 ;

long double ldx=1200 , ldy=1201 ;

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

printf(“\n%lf”, fmax( x1 , x2) ) ;
printf(“\n%lf”, fmax( NAN , 90) ) ;

Output in Visual Studio ,

1201.000000
1201.000000
1201.000000
10.000000
90.000000

For the 5th output the first argument is NAN but the second argument is a valid value so the second argument is returned as the output.


 


fmaxf

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

Code example

float x=2e20 , y=2e20 ;

printf(“%f”, fmax( x , y) ); //Calls the 1st version
printf(“\n%f”, fmaxf( 2e20 , 2e20) ); Calls the 4th version

Output in Visual Studio,

2000000030094932439753377710080.000000
2000000030094932439753377710080.000000


fmaxl

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

Code example

long double x=2e20 , y=2e20 ;

printf(“%Lf”, fmax(x , y) ); //Calls the 3rd version
printf(“\n%Lf”, fmaxl(2e20 , 2e20) ); Calls the 5th version

Output in Visual Studio,

2000000000000000039769249677312.000000
2000000000000000039769249677312.000000


*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 programming isnan,isnanf and isnanl math.h


Related links

->C programming fdim,fdimf and fdiml math.h