C++官网参考链接:https://cplusplus.com/reference/cmath/fma/
函数
<cmath> <ctgmath>
fma
C99
double fma(double x, double y, double z);
float fmaf(float x, float y, float z);
long double fmal(long double x, long double y, long double z);
C++11
double fma(double x, double y, double z);
float fma(float x, float y, float z);
long double fma(long double x, long double y, long double z);
double fma(Type1 x, Type2 y, Type3 z); // additional overloads
乘-加
返回x*y+z。
该函数在不损失任何中间结果的精度的情况下计算结果。
可以在实现中定义以下宏常量,以表明该函数通常比在x*y+z中执行算术运算提供了效率改进(例如当使用硬件乘-加指令时):
macro | description |
---|---|
FP_FAST_FMA | For arguments of type double , it generally executes about as fast as, or faster than, x*y+z . |
FP_FAST_FMAF | For arguments of type float , it generally executes about as fast as, or faster than, x*y+z . |
FP_FAST_FMAL | For arguments of type long double , it generally executes about as fast as, or faster than, x*y+z . |
C99
头文件tgmath.h>提供了该函数的类型泛型宏版本。
C++11
这个头文件(<cmath>)为其他算术类型(arithmetic types)的组合(Type1、Type2和Type3)提供了额外的重载:这些重载在计算之前有效地将其实参转换为double类型,除非至少有一个实参是long double类型(在这种情况下,所有实参都被转换为long double类型)。
形参
x,y
要相乘的值。
z
要相加的值。
返回值
返回x*y+z。
用例
/* fma example */
#include <stdio.h> /* printf */
#include <math.h> /* fma, FP_FAST_FMA */
int main ()
{
double x,y,z,result;
x = 10.0, y = 20.0, z = 30.0;
#ifdef FP_FAST_FMA
result = fma(x,y,z);
#else
result = x*y+z;
#endif
printf ("10.0 * 20.0 + 30.0 = %f\n", result);
return 0;
}
输出:
/* 这里你可以通过在条件编译后面使用printf语句打印执行的是哪条语句!!! */
另请参考
fmin Minimum value (function) (最小值(函数))
fdim Positive difference (function) (正差(函数))