📜  C++ fmin()

📅  最后修改于: 2020-09-25 07:40:05             🧑  作者: Mango

C++中的fmin() 函数采用两个参数并返回其中的最小参数。如果参数之一是NaN,则返回另一个参数。

该函数在头文件中定义。

fmin()原型[从C++ 11标准开始]

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

从C++ 11开始,如果传递给fmin()的任何参数为long double ,则返回类型Promotedlong double 。如果不是,则返回类型Promoteddouble

fmin()参数

fmin()返回值

fmin() 函数返回x和y中的最小值。

示例1:fmin() 函数用于相同类型的参数

#include 
#include 

using namespace std;

int main()
{
    double x = -2.05, y = NAN, result;
    
    result = fmin(x, y);
    cout << "fmin(x, y) = " << result << endl;

    return 0;
}

运行该程序时,输出为:

fmin(x, y) = -2.05

示例2:fmin() 函数用于不同类型的参数

#include 
#include 

using namespace std;

int main()
{
    double x = 56.13, result;
    int y = 89;

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

    return 0;
}

运行该程序时,输出为:

fmin(x, y) = 56.13