Namespaces
Variants

fdim, fdimf, fdiml

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
fdim
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
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 fdimf ( float x, float y ) ;
(1) (C99 起)
double fdim ( double x, double y ) ;
(2) (C99 起)
long double fdiml ( long double x, long double y ) ;
(3) (C99 起)
定义于头文件 <tgmath.h>
#define fdim( x, y )
(4) (C99 起)
1-3) 返回 x y 的正差值,即当 x > y 时返回 x - y ,否则(当 x≤y 时)返回 +0。
4) 类型泛型宏:若任一参数类型为 long double ,则调用 fdiml 。否则,若任一参数具有整数类型或类型为 double ,则调用 fdim 。否则调用 fdimf

目录

参数

x, y - 浮点数值

返回值

若成功,返回 x y 之间的正差值。

如果发生因上溢导致的范围错误,则返回 +HUGE_VAL +HUGE_VALF +HUGE_VALL

如果发生因下溢导致的范围错误,将返回正确值(四舍五入后)。

错误处理

错误报告方式遵循 Template:rllpt 中的规范。

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

  • 如果任一参数为 NaN,则返回 NaN。

注释

等效于 fmax ( x - y, 0 ) ,但需注意其NaN处理规范有所不同。

示例

#include <errno.h>
#include <fenv.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
    printf("fdim(4, 1) = %f, fdim(1, 4)=%f\n", fdim(4,1), fdim(1,4));
    printf("fdim(4,-1) = %f, fdim(1,-4)=%f\n", fdim(4,-1), fdim(1,-4));
    //错误处理
    errno = 0; feclearexcept(FE_ALL_EXCEPT);
    printf("fdim(1e308, -1e308) = %f\n", fdim(1e308, -1e308));
    if (errno == ERANGE)
        perror("    errno == ERANGE");
    if (fetestexcept(FE_OVERFLOW))
        puts("    FE_OVERFLOW raised");
}

可能的输出:

fdim(4, 1) = 3.000000, fdim(1, 4)=0.000000
fdim(4,-1) = 5.000000, fdim(1,-4)=5.000000
fdim(1e308, -1e308) = inf
    errno == ERANGE: Numerical result out of range
    FE_OVERFLOW raised

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.12.1 fdim 函数 (p: TBD)
  • 7.25 泛型数学 <tgmath.h> (p: TBD)
  • F.10.9.1 fdim 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.12.1 fdim 函数 (p: 187-188)
  • 7.25 泛型数学 <tgmath.h> (p: 272-273)
  • F.10.9.1 fdim 函数 (p: 386)
  • C11 标准 (ISO/IEC 9899:2011):
  • 7.12.12.1 fdim 函数集 (p: 257)
  • 7.25 泛型数学 <tgmath.h> (p: 373-375)
  • F.10.9.1 fdim 函数集 (p: 530)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.12.1 fdim函数 (p: 238)
  • 7.22 泛型数学 <tgmath.h> (p: 335-337)
  • F.9.9.1 fdim函数 (p: 466)

参考

计算整型数值的绝对值 ( |x| )
(函数)
(C99) (C99) (C99)
确定两个浮点值的较大者
(函数)