fmax()和fmin()函数在中定义 数学库 头文件。
null
- fmax()函数: 此函数的语法为:
double fmax (double x, double y); float fmax (float x, float y); long double fmax (long double x, long double y);
该函数的输入是两个float类型的值,double或long double。函数返回两个输入值中的最大值。
下面是示例C++程序,显示FMax()函数的工作:
// CPP program to show working
// of fmax() function.
#include <cmath>
#include <iomanip>
#include <iostream>
using
namespace
std;
int
main()
{
double
val;
// Find maximum value when both the inputs
// are positive.
val = fmax(10.0, 1.0);
cout << fixed << setprecision(4)
<<
"fmax(10.0, 1.0) = "
<< val <<
""
;
// Find maximum value when inputs have
// opposite sign.
val = fmax(-10.0, 1.0);
cout << fixed << setprecision(4)
<<
"fmax(-10.0, 1.0) = "
<< val <<
""
;
// Find maximum value when both the inputs
// are negative.
val = fmax(-10.0, -1.0);
cout << fixed << setprecision(4)
<<
"fmax(-10.0, -1.0) = "
<< val <<
""
;
return
0;
}
输出:fmax(10.0, 1.0) = 10.0000 fmax(-10.0, 1.0) = 1.0000 fmax(-10.0, -1.0) = -1.0000
- fmin()函数: 此函数的语法为:
double fmin (double x, double y); float fmin (float x, float y); long double fmin (long double x, long double y);
该函数的输入是两个float类型的值,double或long double。该函数返回两个输入值中的最小值。
下面是示例C++程序,显示fmin()函数的工作:
// CPP program to show working
// of fmin() function.
#include <cmath>
#include <iomanip>
#include <iostream>
using
namespace
std;
int
main()
{
double
val;
// Find minimum value when both the inputs
// are positive.
val = fmin(10.0, 1.0);
cout << fixed << setprecision(4)
<<
"fmin(10.0, 1.0) = "
<< val <<
""
;
// Find minimum value when inputs have
// opposite sign.
val = fmin(-10.0, 1.0);
cout << fixed << setprecision(4)
<<
"fmin(-10.0, 1.0) = "
<< val <<
""
;
// Find minimum value when both the inputs
// are negative.
val = fmin(-10.0, -1.0);
cout << fixed << setprecision(4)
<<
"fmin(-10.0, -1.0) = "
<< val <<
""
;
return
0;
}
输出:fmin(10.0, 1.0) = 1.0000 fmin(-10.0, 1.0) = -10.0000 fmin(-10.0, -1.0) = -10.0000
注: 考虑函数的参数是不同类型的情况。在这种情况下,参数首先由函数隐式类型转换,然后返回所需的最大值/最小值。 下面是示例C++程序,说明了这种情况:
// CPP program to show working
// of fmin() and fmax()function
// when input values are of
// different data types.
#include <cmath>
#include <iomanip>
#include <iostream>
using
namespace
std;
int
main()
{
double
val;
// Find minimum value when one of the inputs
// is of type float and other is of type
// double.
val = fmin(10.0f, 1.0);
cout << fixed << setprecision(4)
<<
"fmin(10.0f, 1.0) = "
<< val <<
""
;
// Find maximum value when one of the inputs
// is of type float and other is of type
// double.
val = fmax(10.0, -1.0f);
cout << fixed << setprecision(4)
<<
"fmax(10.0, -1.0f) = "
<< val <<
""
;
return
0;
}
输出:fmin(10.0f, 1.0) = 1.0000 fmax(10.0, -1.0f) = 10.0000
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END