該函數返回兩個數字之間的最大值。
條件:
考慮兩個數字 'x' 和 'y'。
如果(x>y): 它返回 x。
如果(y>x): 它返回 y。
如果 (x=nan): 它返回 y。
如果 (y=nan): 它返回 x。
用法
float fmax(float x, float y);
double fmax(double x, double y);
long double fmax(long double x, long double y);
promoted fmax(Arithmetic x, Arithmetic y);
注意:如果任何參數具有整數類型,則將其強製轉換為 double。如果任何其他參數是 long double,則將其強製轉換為 long double。
參數
(x,y):計算最大值的值。
返回值
它返回兩個數字之間的最大值。
例子1
讓我們看一個簡單的例子。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double x=3.3;
float y=6.9;
std::cout <<"Values of x and y are:"<<x<<","<<y<< std::endl;
cout<<"Maximum value is:"<<fmax(x,y);
return 0;
}
輸出:
Values of x and y are:3.3,6.9 Maximum value is:6.9
在本例中,y 的值大於 x 的值。因此,fmax() 函數返回 y 的值。
例子2
當其中一個值為 nan 時,讓我們看一個簡單的示例。
#include <iostream>
#include<math.h>
using namespace std;
int main()
{
double x=1.3;
float y=NAN;
std::cout <<"Values of x and y are:"<<x<<","<<y<< std::endl;
cout<<"Maximum value is:"<<fmax(x,y);
return 0;
}
輸出:
Values of x and y are:1.3,nan Maximum value is:1.3
本例中y的值為nan。因此,fmax()函數返回x的值。
相關用法
- C++ Math fma()用法及代碼示例
- C++ Math fmin()用法及代碼示例
- C++ Math fmod()用法及代碼示例
- C++ Math fabs()用法及代碼示例
- C++ Math fdim()用法及代碼示例
- C++ Math fpclassify()用法及代碼示例
- C++ Math frexp()用法及代碼示例
- C++ Math floor()用法及代碼示例
- C++ Math scalbn()用法及代碼示例
- C++ Math acosh()用法及代碼示例
- C++ Math asinh()用法及代碼示例
- C++ Math isgreater()用法及代碼示例
- C++ Math islessgreater()用法及代碼示例
- C++ Math log2()用法及代碼示例
- C++ Math nearbyint()用法及代碼示例
- C++ Math tan()用法及代碼示例
- C++ Math log()用法及代碼示例
- C++ Math nextafter()用法及代碼示例
- C++ Math isfinite()用法及代碼示例
- C++ Math erfc()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Math fmax()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。