# fmax() and fmin() in C++

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
```
3. 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
```

My Personal Notes arrow_drop_up A Programmer and A Machine learning Enthusiast

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.