C ++中的fmin()函數(shù)采用兩個參數(shù)并返回其中的最小參數(shù)。如果參數(shù)之一是NaN,則返回另一個參數(shù)。
該函數(shù)在<cmath>頭文件中定義。
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()的參數(shù)為long double,則返回類型Promoted為long double。如果不是,則返回類型Promoted為double。
x:fmin()的第一個參數(shù)。
y:fmin()的第二個參數(shù)。
fmin()函數(shù)返回x和y中的最小值。
#include <iostream> #include <cmath> 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
#include <iostream> #include <cmath> 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