Namespaces
Variants

fmax, fmaxf, fmaxl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
fmax
(C99)
(C99)
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <math.h>
float fmaxf ( float x, float y ) ;
(1) (C99 起)
double fmax ( double x, double y ) ;
(2) (C99 起)
long double fmaxl ( long double x, long double y ) ;
(3) (C99 起)
定义于头文件 <tgmath.h>
#define fmax( x, y )
(4) (C99 起)
1-3) 返回两个浮点参数中的较大值,将 NaN 视为缺失数据(在 NaN 与数值之间,选择数值)。
4) 类型泛型宏:若任意参数类型为 long double ,则调用 fmaxl 。否则,若任意参数具有整数类型或类型为 double ,则调用 fmax 。否则调用 fmaxf

目录

参数

x, y - 浮点数值

返回值

若成功,返回两个浮点值中的较大者。返回值是精确的,且不依赖于任何舍入模式。

错误处理

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

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

  • 如果两个参数中有一个是 NaN,则返回另一个参数的值。
  • 仅当两个参数都是 NaN 时才返回 NaN。

注释

此函数无需对零的符号敏感,但某些实现会额外规定:若一个参数为+0而另一个为-0,则返回+0。

示例

#include <math.h>
#include <stdio.h>
int main(void)
{
    printf("fmax(2,1)    = %f\n", fmax(2,1));
    printf("fmax(-Inf,0) = %f\n", fmax(-INFINITY,0));
    printf("fmax(NaN,-1) = %f\n", fmax(NAN,-1));
}

输出:

fmax(2,1)    = 2.000000
fmax(-Inf,0) = 0.000000
fmax(NaN,-1) = -1.000000

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.12.2 fmax 函数集 (p: TBD)
  • 7.25 泛型数学 <tgmath.h> (p: TBD)
  • F.10.9.2 fmax 函数集 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.12.2 fmax 函数集 (p: 188)
  • 7.25 泛型数学 <tgmath.h> (p: 397)
  • F.10.9.2 fmax 函数集 (p: 386)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.12.2 fmax 函数集 (p: 257-258)
  • 7.25 泛型数学 <tgmath.h> (p: 373-375)
  • F.10.9.2 fmax 函数集 (p: 530)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.12.2 fmax函数集(页码:238-239)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.9.2 fmax函数集(页码:466)

参见

检查第一个浮点参数是否大于第二个
(函数宏)
(C99) (C99) (C99)
确定两个浮点值中的较小值
(函数)