fmax() and fmin() in C++



The fmax() and fmin() functions in C++ are used to check the maximum or minimum of two floating-point numbers. These functions are defined under the <cmath> header file of the C++ standard library.

C++ fmax() function

The fmax() is used to compare and return the larger of two floating-point values. These floating point values can be float, double, and long double.

Syntax

data_type fmax(data_type value_1, data_type value_2);

Here, data_type could be float, double and long double.

C++ fmin() function

The fmin() is used to compare and return the smaller of two floating-point values. These floating point values can be float, double, and long double.

Syntax

data_type fmin(data_type value_1, data_type value_2);

Note: If data_type of two different arguments is different, then C++ does implicit type promotion before calling the function.

For example, in fmax(float, long double), both values will be upgraded to long double.

Example of fmax() and fmin() Functions

Here is the following example code showcasing the use of fmax() and fmin() functions in C++.

#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
   double res;
   
   // using of fmax() function
   cout << "fmax(50.0, 10.0) = " << fmax(50.0, 10.0) << endl;
   cout << "fmax(-50.0, 10.0) = " << fmax(-50.0, 10.0) << endl; 
   cout << "fmax(-50.0, -10.0) = " << fmax(-50.0, -10.0) << endl;
   
   // using of fmin() function
   cout << "fmin(50.0, 10.0) = " << fmin(50.0, 10.0) << endl;
   cout << "fmin(-50.0, 10.0) = " << fmin(-50.0, 10.0) << endl;
   cout << "fmin(-50.0, -10.0) = " << fmin(-50.0, -10.0) << endl;

}

Output

fmax(50.0, 10.0) = 50
fmax(-50.0, 10.0) = 10
fmax(-50.0, -10.0) = -10
fmin(50.0, 10.0) = 10
fmin(-50.0, 10.0) = -50
fmin(-50.0, -10.0) = -50
Akansha Kumari
Akansha Kumari

Hi, I am Akansha, a Technical Content Engineer with a passion for simplifying complex tech concepts.

Updated on: 2025-06-02T15:18:31+05:30

582 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements