fmax() and fmin() functions are defined in cmath header file.
- fmax() function: The syntax of this function is:
double fmax (double x, double y); float fmax (float x, float y); long double fmax (long double x, long double y);
- The input to this function are two values of type float, double or long double. The function returns maximum of the two input values. Below is sample C++ program to show working of fmax() function:
CPP
// CPP program to show working // of fmax() function. #include <cmath> #include <iomanip> #include <iostream> using namespace std;
int main()
{ double val;
// Find maximum value when both the inputs
// are positive.
val = fmax(10.0, 1.0);
cout << fixed << setprecision(4)
<< "fmax(10.0, 1.0) = " << val << "\n" ;
// Find maximum value when inputs have
// opposite sign.
val = fmax(-10.0, 1.0);
cout << fixed << setprecision(4)
<< "fmax(-10.0, 1.0) = " << val << "\n" ;
// Find maximum value when both the inputs
// are negative.
val = fmax(-10.0, -1.0);
cout << fixed << setprecision(4)
<< "fmax(-10.0, -1.0) = " << val << "\n" ;
return 0;
} |
Output:
fmax(10.0, 1.0) = 10.0000 fmax(-10.0, 1.0) = 1.0000 fmax(-10.0, -1.0) = -1.0000
Time Complexity: O(1)
Space Complexity: O(1)
- fmin() function: The syntax of this function is:
double fmin (double x, double y); float fmin (float x, float y); long double fmin (long double x, long double y);
- The input to this function are two values of type float, double or long double. The function returns minimum of the two input values. Below is sample C++ program to show working of fmin() function:
CPP
// CPP program to show working // of fmin() function. #include <cmath> #include <iomanip> #include <iostream> using namespace std;
int main()
{ double val;
// Find minimum value when both the inputs
// are positive.
val = fmin(10.0, 1.0);
cout << fixed << setprecision(4)
<< "fmin(10.0, 1.0) = " << val << "\n" ;
// Find minimum value when inputs have
// opposite sign.
val = fmin(-10.0, 1.0);
cout << fixed << setprecision(4)
<< "fmin(-10.0, 1.0) = " << val << "\n" ;
// Find minimum value when both the inputs
// are negative.
val = fmin(-10.0, -1.0);
cout << fixed << setprecision(4)
<< "fmin(-10.0, -1.0) = " << val << "\n" ;
return 0;
} |
Output:
fmin(10.0, 1.0) = 1.0000 fmin(-10.0, 1.0) = -10.0000 fmin(-10.0, -1.0) = -10.0000
Time Complexity: O(1)
Space Complexity: O(1)
Note : Consider a case when the arguments of the function are of different types. In that case, the arguments are first implicity typecast by the function and then the required maximum/minimum value is returned.
Below is sample C++ program that illustrates this case:
C++
// CPP program to show working // of fmin() and fmax()function // when input values are of // different data types. #include <cmath> #include <iomanip> #include <iostream> using namespace std;
int main()
{ double val;
// Find minimum value when one of the inputs
// is of type float and other is of type
// double.
val = fmin(10.0f, 1.0);
cout << fixed << setprecision(4)
<< "fmin(10.0f, 1.0) = " << val << "\n" ;
// Find maximum value when one of the inputs
// is of type float and other is of type
// double.
val = fmax(10.0, -1.0f);
cout << fixed << setprecision(4)
<< "fmax(10.0, -1.0f) = " << val << "\n" ;
return 0;
} |
Output
fmin(10.0f, 1.0) = 1.0000 fmax(10.0, -1.0f) = 10.0000
Time Complexity: O(1)
Space Complexity: O(1)