fdim
来自cppreference.com
| 定义于头文件 <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。
若发生源于下溢的值域错误,则返回准确值(舍入后)。
[编辑] 错误处理
错误按照指定于math_errhandling的方式报告。
若实现支持IEEE浮点算术(IEC 60559)则,
- 若任一参数为NaN则返回NaN
[编辑] 注意
等价于fmax(x-y, 0),除了NaN的处理要求。
[编辑] 示例
运行此代码
#include <stdio.h> #include <math.h> #include <errno.h> #include <fenv.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[编辑] 参考
- C11 standard (ISO/IEC 9899:2011):
- 7.12.12.1 The fdim functions (p: 257)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.9.1 The fdim functions (p: 530)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.12.1 The fdim functions (p: 238)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.9.1 The fdim functions (p: 466)
[编辑] 参阅
| (C99) |
计算整数值的绝对值 (|x|) (函数) |
| (C99) (C99) (C99) |
确定两个浮点值的较大者 (函数) |
| fdim的 C++ 文档
|

