C++ fmax()

The function is defined in <cmath> header file.


fmax() prototype [As of C++ 11 standard]

double fmax(double x, double y);
float fmax(float x, float y);
long double fmax(long double x, long double y);
Promoted fmax(Type1 x, Type2 y); // Additional overloads for arithmetic types

Since C++11, if any argument passed to fmax() is long double, the return type Promoted is long double. If not, the return type Promoted is double.


fmax() Parameters

  • x: The first argument of fmax().
  • y: The second argument of fmax().

fmax() Return value

The fmax() function returns the maximum value among x and y.


Example 1: fmax() function for arguments of same type

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x = -2.05, y = NAN, result;

    result = fmax(x, y);
    cout << "fmax(x, y) = " << result << endl;

    return 0;
}

When you run the program, the output will be:

fmax(x, y) = -2.05

Example 2: fmax() function for arguments of different types

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x = 56.13, result;
    int y = 89;
    
    result = fmax(x, y);
    cout << "fmax(x, y) = " << result << endl;

    return 0;
}

When you run the program, the output will be:

fmax(x, y) = 89
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges