std::fmax, std::fmaxf, std::fmaxl

来自cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
在标头 <cmath> 定义
(1)
float       fmax (float x, float y );

double      fmax (double x, double y );

longdouble fmax (longdouble x, longdouble y );
(C++23 前)
constexpr/* 浮点数类型 */

            fmax (/* 浮点数类型 */ x,

                   /* 浮点数类型 */ y );
(C++23 起)
float       fmaxf(float x, float y );
(2) (C++11 起)
(C++23 起为 constexpr)
longdouble fmaxl(longdouble x, longdouble y );
(3) (C++11 起)
(C++23 起为 constexpr)
SIMD 重载(C++26 起)
在标头 <simd> 定义
template<class V0, class V1 >

constexpr/*math-common-simd-t*/<V0, V1>

            fmax (const V0& v_x, const V1& v_y );
(S) (C++26 起)
额外重载(C++11 起)
在标头 <cmath> 定义
template<class Integer >
double      fmax ( Integer x, Integer y );
(A) (C++23 起为 constexpr)
1-3) 返回两个浮点数的较大者,把 NaN 当做缺失数据(在 NaN 和数值间选择数值)。标准库提供所有以无 cv 限定的浮点数类型作为各形参的类型的 std::fmax 重载。(C++23 起)
S) SIMD 重载对 v_xv_y 实施逐元素 std::fmax
(参见 math-common-simd-t 的定义。)
(C++26 起)
A) 为所有整数类型提供额外重载,将它们当做 double
(C++11 起)

目录

[编辑]参数

x, y - 浮点数或整数

[编辑]返回值

成功时返回两个浮点数值的较大者。返回值准确且不依赖任何舍入模式。

[编辑]错误处理

此函数不受制于 math_errhandling 中指定的任何错误条件。

如果实现支持 IEEE 浮点数算术(IEC 60559),那么

  • 如果两个实参之一是 NaN,那么返回另一实参的值
  • 只有在两个实参都是 NaN 时才会返回 NaN

[编辑]注解

不要求此函数对零的符号敏感,尽管某些实现额外强制在一个参数是 +0 而另一个是 -0 时返回 +0

额外重载不需要以 (A) 的形式提供。它们只需要能够对它们的第一个实参 num1 和第二个实参 num2 满足以下要求:

  • 如果 num1num2 具有 longdouble 类型,那么 std::fmax(num1, num2)std::fmax(static_cast<longdouble>(num1),
              static_cast<longdouble>(num2))
    的效果相同。
  • 否则,如果 num1 和/或 num2 具有 double 或整数类型,那么 std::fmax(num1, num2)std::fmax(static_cast<double>(num1),
              static_cast<double>(num2))
    的效果相同。
  • 否则,如果 num1num2 具有 float 类型,那么 std::fmax(num1, num2)std::fmax(static_cast<float>(num1),
              static_cast<float>(num2))
    的效果相同。
(C++23 前)

如果 num1num2 具有算术类型,那么 std::fmax(num1, num2)std::fmax(static_cast</*公共浮点数类型*/>(num1),
          static_cast</*公共浮点数类型*/>(num2))
的效果相同,其中 /*公共浮点数类型*/num1num2 的类型中浮点数转换等级浮点数转换子等级最高的浮点数类型,整数类型的实参被视为具有与 double 相等的浮点数转换等级。

如果不存在等级和子等级最高的浮点数类型,那么在重载决议时不会从提供的重载中产生可用的候选。

(C++23 起)

[编辑]示例

#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

[编辑]参阅

(C++11)
检查第一个浮点数实参是否大于第二个
(函数)[编辑]
(C++11)(C++11)(C++11)
两个浮点数的较小者
(函数)[编辑]
返回给定值中较大者
(函数模板)[编辑]
返回范围中最大元
(函数模板)[编辑]
(C++11)
返回两个元素间的较小者和较大者
(函数模板)[编辑]
返回范围中的最小元和最大元
(函数模板)[编辑]
fmax 的 C 文档
close