English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ fmax() function usage and example

C++ Library Function <cmath>

C ++uses two parameters and returns the largest one. If one of the parameters is NAN, it returns the other parameter.

The function in<cmath>defined in the header file.

fmax() prototype [from 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); // other overloaded arithmetic types

From C ++ 11Starting from C, if the parameters passed to fmax() are of type long double, the returned type Promoted is long double. If not, the returned type Promoted is double.

fmax() parameters

  • x: The first parameter of fmax().

  • y: The second parameter of fmax().

fmax() return value

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

Example1: The fmax() function is used for parameters of the 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;
}

The output when running the program is:

fmax(x, y) = -2.05

Example2: The fmax() function is used for parameters 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;
}

The output when running the program is:

fmax(x, y) = 89

C++ Library Function <cmath>