C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Math fmax()

C++ Math fmax()

该函数返回两个数字之间的最大值。

条件:

考虑两个数字" x"和" y"。
If(x> y): 返回x。
If(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的值。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4