函式
<cmath> <ctgmath>

fma

     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);
     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 算術運算提供更高的效率(例如當使用硬體乘加指令時):

描述
FP_FAST_FMA對於 double 型別的引數,其執行速度通常與 x*y+z 一樣快或更快。
FP_FAST_FMAF對於 float 型別的引數,其執行速度通常與 x*y+z 一樣快或更快。
FP_FAST_FMAL對於 long double 型別的引數,其執行速度通常與 x*y+z 一樣快或更快。

標頭檔案 <tgmath.h> 提供了此函式的型別通用宏版本。
此標頭檔案 (<cmath>) 中為其他算術型別 (Type1Type2Type3) 的組合提供了額外的過載:這些過載在計算前會有效地將其引數轉換為 double,除非至少有一個引數是 long double 型別(在這種情況下,所有引數都將被轉換為 long double)。

引數

x, y
要相乘的值。
z
要相加的值。

返回值

x*y+z 的結果

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* 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;
}

輸出

10.0 * 20.0 + 30.0 = 230.000000


另見