Fmax
定义于头文件 <cmath>
中。
描述
返回两个浮点参数中较大的一个,将 NaN 视为缺失数据(在 NaN 和数值之间,选择数值)。该库为所有 cv-unqualified 浮点类型提供了 std::fmax
的重载,作为参数 x
和 y
的类型。
声明
- C++23
- C++11
// 1)
constexpr /* floating-point-type */
fmax ( /* floating-point-type */ x,
/* floating-point-type */ y );
// 2)
constexpr float fmaxf( float x, float y );
// 3)
constexpr long double fmaxl( long double x, long double y );
// 4)
template< class Arithmetic1, class Arithmetic2 >
constexpr /* common-floating-point-type */ fmax( Arithmetic1 x, Arithmetic2 y );
// 1)
float fmax ( float x, float y );
// 2)
double fmax ( double x, double y );
// 3)
long double fmax ( long double x, long double y );
// 4)
float fmaxf( float x, float y );
// 5)
long double fmaxl( long double x, long double y );
// 6)
template< class Arithmetic1, class Arithmetic2 >
/* common-floating-point-type */ fmax( Arithmetic1 x, Arithmetic2 y );
参数
x
,y
- 浮点或整数值
返回值
如果成功,返回两个浮点值中较大的一个。返回的值是精确的,不依赖于任何舍入模式。
错误处理
此函数不受 math_errhandling 中指定的任何错误条件的影响。
如果实现支持 IEEE 浮点运算(IEC 60559)
如果两个参数中有一个是 NaN,则返回另一个参数的值。只有当两个参数都是 NaN 时,才返回 NaN。
备注
此函数不要求对零的符号敏感,尽管某些实现会额外强制规定,如果一个参数是 +0
另一个是 -0
,则返回 +0
。
不需要严格按照 附加重载 提供附加重载。它们只需要足以确保对于它们的第一个参数 num1
和第二个参数 num2
如果 num1
或 num2
的类型是 long double,则
std::fmax(num1, num2)
与以下表达式效果相同:
std::fmax(static_cast<long double>(num1), static_cast<long double>(num2))
。
否则,如果 num1
和/或 num2
的类型是 double 或整数类型,则
std::fmax(num1, num2)
与以下表达式效果相同:
std::fmax(static_cast<double>(num1), static_cast<double>(num2))
。
否则,如果 num1
或 num2
的类型是 float,则
std::fmax(num1, num2)
与
std::fmax(static_cast<float>(num1), static_cast<float>(num2))
效果相同。 (直到 C++23)
如果 num1
和 num2
具有算术类型,则
std::fmax(num1, num2)
与以下表达式效果相同:
std::fmax(static_cast</* common-floating-point-type */>(num1), static_cast</* common-floating-point-type */>(num2))
其中 /* common-floating-point-type */ 是 num1
和 num2
类型之间具有最高浮点转换等级和最高浮点转换子等级的浮点类型,整数类型的参数被认为具有与 double 相同的浮点转换等级。
如果不存在具有最高等级和子等级的浮点类型,则重载决议不会从提供的重载中产生可用的候选函数。
示例
#include <cmath>
#include <iostream>
int main()
{
std::cout
<< "fmax(2,1) = "
<< std::fmax(2, 1) << '\n'
<< "fmax(-Inf,0) = "
<< std::fmax(-INFINITY, 0) << '\n'
<< "fmax(NaN,-1) = "
<< std::fmax(NAN, -1) << '\n';
}
fmax(2,1) = 2
fmax(-Inf,0) = 0
fmax(NaN,-1) = -1