fma, fmaf, fmal
来自cppreference.com
| 在标头 <math.h> 定义
|
||
| |
(1) | (C99 起) |
| |
(2) | (C99 起) |
| |
(3) | (C99 起) |
| |
(4) | (C99 起) |
| |
(5) | (C99 起) |
| |
(6) | (C99 起) |
| 在标头 <tgmath.h> 定义
|
||
| |
(7) | (C99 起) |
1-3) 计算
(x * y) + z,如同用无限精度,而仅舍入一次到结果类型。4-6) 若定义宏常量
FP_FAST_FMA、FP_FAST_FMAF 或 FP_FAST_FMAL,则对应函数 fma、fmaf 或 fmal 分别求值快于(并且精度高于)double、float 和 long double 实参的表达式 x * y + z。若定义,则这些宏求值为整数 1。7) 泛型宏:若任何实参拥有
long double 类型,则调用 fmal。否则若任何实参拥有整数类型或 double 类型,则调用 fma。否则调用fmaf。参数
| x, y, z | - | 浮点数 |
返回值
若成功,则返回 (x * y) + z 的值,如同计算为无限精度再舍入一次以适合目标类型(或者说是作为单次三元浮点数运算计算)。
若出现上溢所致的值域错误,则返回 HUGE_VAL、±HUGE_VALF 或 ±HUGE_VALL。
若出现下溢所致的值域错误,则返回(舍入后的)正确结果。
错误处理
报告 math_errhandling 中指定的错误
若实现支持 IEEE 浮点数算术(IEC 60559),则
- 若
x为零而y为无穷大或x为无穷大而y为零,且- 若
z非 NaN,则返回 NaN 并引发 FE_INVALID - 若
z为 NaN,则返回 NaN 并可能引发 FE_INVALID。
- 若
- 若
x * y为准确的无穷大且z为带相反符号的无穷大,则返回 NaN 并引发 FE_INVALID。 - 若
x或y为 NaN,则返回 NaN。 - 若
z为 NaN,且x * y不是0 * Inf或Inf * 0,则返回 NaN(而无 FE_INVALID)。
注解
此运算经常在硬件中实现为融合乘加 CPU 指令。若硬件支持,则期待定义相应的 FP_FAST_FMA* 宏,但多数实现即使在不定义这些宏时也利用该 CPU 指令。
POSIX 指定 x * y 非法且 z 为 NaN 的情形是定义域错误。
由于其无限的中间精度,fma 是其他正确舍入数学运算,如 sqrt 或甚至除法(在 CPU 不支持的平台上,例如 Itanium)的常用构建块。
同所有浮点数表达式,表达式 (x*y) + z 可编译为融合乘加,除非 #pragma STDC FP_CONTRACT 为关闭。
示例
运行此代码
#include <fenv.h>
#include <float.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
int main(void)
{
// 演示 fma 和内建运算符间的区别
double in = 0.1;
printf("0.1 double is %.23f (%a)\n", in, in);
printf("0.1*10 is 1.0000000000000000555112 (0x8.0000000000002p-3),"
" or 1.0 if rounded to double\n");
double expr_result = 0.1 * 10 - 1;
printf("0.1 * 10 - 1 = %g : 1 subtracted after "
"intermediate rounding to 1.0\n", expr_result);
double fma_result = fma(0.1, 10, -1);
printf("fma(0.1, 10, -1) = %g (%a)\n", fma_result, fma_result);
// fma 用于 double-double
printf("\nin double-double arithmetic, 0.1 * 10 is representable as ");
double high = 0.1 * 10;
double low = fma(0.1, 10, -high);
printf("%g + %g\n\n", high, low);
// 错误处理
feclearexcept(FE_ALL_EXCEPT);
printf("fma(+Inf, 10, -Inf) = %f\n", fma(INFINITY, 10, -INFINITY));
if(fetestexcept(FE_INVALID))
puts(" FE_INVALID raised");
}
可能的输出:
0.1 double is 0.10000000000000000555112 (0x1.999999999999ap-4)
0.1*10 is 1.0000000000000000555112 (0x8.0000000000002p-3), or 1.0 if rounded to double
0.1 * 10 - 1 = 0 : 1 subtracted after intermediate rounding to 1.0
fma(0.1, 10, -1) = 5.55112e-17 (0x1p-54)
in double-double arithmetic, 0.1 * 10 is representable as 1 + 5.55112e-17
fma(+Inf, 10, -Inf) = -nan
FE_INVALID raised
引用
- C23 标准(ISO/IEC 9899:2024):
- 7.12.13.1 The fma functions (第 TBD 页)
- 7.25 Type-generic math <tgmath.h> (第 TBD 页)
- F.10.10.1 The fma functions (第 TBD 页)
- C17 标准(ISO/IEC 9899:2018):
- 7.12.13.1 The fma functions (第 188-189 页)
- 7.25 Type-generic math <tgmath.h> (第 272-273 页)
- F.10.10.1 The fma functions (第 386 页)
- C11 标准(ISO/IEC 9899:2011):
- 7.12.13.1 The fma functions (第 258 页)
- 7.25 Type-generic math <tgmath.h> (第 373-375 页)
- F.10.10.1 The fma functions (第 530 页)
- C99 标准(ISO/IEC 9899:1999):
- 7.12.13.1 The fma functions (第 239 页)
- 7.22 Type-generic math <tgmath.h> (第 335-337 页)
- F.9.10.1 The fma functions (第 466 页)
参阅
(C99)(C99)(C99) |
计算浮点数除法运算的带符号余数 (函数) |
(C99)(C99)(C99) |
计算除法运算的带符号余数,以及商的后三位 (函数) |
fma 的 C++ 文档
| |