C++ 中的 fmax() 和 fmin()
在本部分中,我们将介绍如何将 fmax() 和 fmin() 转换为 C++。fmax() 和 fmin() 存在于 cmath 头文件中。
此函数采用 float、double 或 long double 类型的两个值,并分别使用 fmax() 和 fmin() 返回最大值或最小值。
如果参数类型不同,比如某人想比较 float 和 double,或将 long double 与 float 比较,那么此函数会隐式地将其类型强制转换为该值,然后返回相应的值。
示例
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
main() {
double res;
//uses of fmax()
res = fmax(50.0, 10.0); //compare for both positive value
cout << fixed << setprecision(4) << "fmax(50.0, 10.0) = " << res << endl;
res = fmax(-50.0, 10.0); //comparison between opposite sign
cout << fixed << setprecision(4) << "fmax(-50.0, 10.0) = " << res << endl;
res = fmax(-50.0, -10.0); //compare when both are negative
cout << fixed << setprecision(4) << "fmax(-50.0, -10.0) = " << res << endl;
//uses of fmin()
res = fmin(50.0, 10.0); //compare for both positive value
cout << fixed << setprecision(4) << "fmin(50.0, 10.0) = " << res << endl;
res = fmin(-50.0, 10.0); //comparison between opposite sign
cout << fixed << setprecision(4) << "fmin(-50.0, 10.0) = " << res << endl;
res = fmin(-50.0, -10.0); //compare when both are negative
cout << fixed << setprecision(4) << "fmin(-50.0, -10.0) = " << res << endl;
}输出
fmax(50.0, 10.0) = 50.0000 fmax(-50.0, 10.0) = 10.0000 fmax(-50.0, -10.0) = -10.0000 fmin(50.0, 10.0) = 10.0000 fmin(-50.0, 10.0) = -50.0000 fmin(-50.0, -10.0) = -50.0000
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP