In this section we will see how to convert fmax() and fmin() in C++. The fmax() and fmin() are present in the cmath header file.
This function takes two values of type float, or double or long double and returns maximum or minimum using fmax() and fmin() respectively.
If the argument types are different, like if someone wants to compare float and double, or long double with float, then the function typecasts into that value implicitly, then returns the corresponding value.
Example
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
double res;
//uses of fmax()
res = fmax(50.0, 10.0); //compare for both positive value
cout << fixed << setprecision(4) << "fmax(50.0, 10.0) = " << res << endl;
res = fmax(-50.0, 10.0); //comparison between opposite sign
cout << fixed << setprecision(4) << "fmax(-50.0, 10.0) = " << res << endl;
res = fmax(-50.0, -10.0); //compare when both are negative
cout << fixed << setprecision(4) << "fmax(-50.0, -10.0) = " << res << endl;
//uses of fmin()
res = fmin(50.0, 10.0); //compare for both positive value
cout << fixed << setprecision(4) << "fmin(50.0, 10.0) = " << res << endl;
res = fmin(-50.0, 10.0); //comparison between opposite sign
cout << fixed << setprecision(4) << "fmin(-50.0, 10.0) = " << res << endl;
res = fmin(-50.0, -10.0); //compare when both are negative
cout << fixed << setprecision(4) << "fmin(-50.0, -10.0) = " << res << endl;
}Output
fmax(50.0, 10.0) = 50.0000 fmax(-50.0, 10.0) = 10.0000 fmax(-50.0, -10.0) = -10.0000 fmin(50.0, 10.0) = 10.0000 fmin(-50.0, 10.0) = -50.0000 fmin(-50.0, -10.0) = -50.0000