跳到主要内容

C++ fmax() 最大值函数

此函数定义在 <cmath> 头文件中。

fmax() 原型 [C++ 11 标准起]

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); // 对算术类型的附加重载

自 C++11 起,如果传递给 fmax() 的任一参数是 long double,则返回类型 Promotedlong double。如果不是,则返回类型 Promoteddouble

fmax() 参数

  • x:fmax() 的第一个参数。
  • y:fmax() 的第二个参数。

fmax() 返回值

fmax() 函数返回 x 和 y 中的最大值。

示例 1:对相同类型参数的 fmax() 函数

#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;
}

当你运行程序时,输出将会是:

fmax(x, y) = -2.05

示例 2:对不同类型参数的 fmax() 函数

#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;
}

当你运行程序时,输出将会是:

fmax(x, y) = 89