fmax() and fmin() in C++

13 Apr, 2018
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 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
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 <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
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 <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
My Personal Notes
arrow_drop_upSave
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 <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

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 <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;}
fmax(10.0, 1.0) = 10.0000
fmax(-10.0, 1.0) = 1.0000
fmax(-10.0, -1.0) = -1.0000

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 <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

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 <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;}
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 <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;}
fmin(10.0f, 1.0) = 1.0000
fmax(10.0, -1.0f) = 10.0000

CPP-Library
cpp-math
C++
Programming Language
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.