Related Articles
fmax() and fmin() in C++
• Last Updated : 13 Apr, 2018

fmax() and fmin() functions are defined in cmath header file.

1. 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 program to show working``// of fmax() function.`` ` `#include ``#include ``#include `` ` `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
```
2. 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 program to show working``// of fmin() function.`` ` `#include ``#include ``#include `` ` `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
```

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:

 `// CPP program to show working``// of fmin() and fmax()function``// when input values are of``// different data types.`` ` `#include ``#include ``#include `` ` `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
```
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up