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
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
double val;
val = fmax(10.0, 1.0);
cout << fixed << setprecision(4)
<< "fmax(10.0, 1.0) = " << val << "\n" ;
val = fmax(-10.0, 1.0);
cout << fixed << setprecision(4)
<< "fmax(-10.0, 1.0) = " << val << "\n" ;
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
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
double val;
val = fmin(10.0, 1.0);
cout << fixed << setprecision(4)
<< "fmin(10.0, 1.0) = " << val << "\n" ;
val = fmin(-10.0, 1.0);
cout << fixed << setprecision(4)
<< "fmin(-10.0, 1.0) = " << val << "\n" ;
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++
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main()
{
double val;
val = fmin(10.0f, 1.0);
cout << fixed << setprecision(4)
<< "fmin(10.0f, 1.0) = " << val << "\n" ;
val = fmax(10.0, -1.0f);
cout << fixed << setprecision(4)
<< "fmax(10.0, -1.0f) = " << val << "\n" ;
return 0;
}
|
Outputfmin(10.0f, 1.0) = 1.0000
fmax(10.0, -1.0f) = 10.0000
Time Complexity: O(1)
Space Complexity: O(1)