<cmath> (math.h)

function
<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
Multiply-add
Returns x*y+z.

The function computes the result without losing precision in any intermediate result.

The following macro constants may be defined in an implementation to signal that this function generally provides an efficiency improvement over performing the arithmetic operations in x*y+z (such as when a hardware multiply-add instruction is used):

macrodescription
FP_FAST_FMAFor arguments of type double, it generally executes about as fast as, or faster than, x*y+z.
FP_FAST_FMAFFor arguments of type float, it generally executes about as fast as, or faster than, x*y+z.
FP_FAST_FMALFor arguments of type long double, it generally executes about as fast as, or faster than, x*y+z.

Header <tgmath.h> provides a type-generic macro version of this function.
Additional overloads are provided in this header (<cmath>) for other combinations of arithmetic types (Type1, Type2 and Type3): These overloads effectively cast its arguments to double before calculations, except if at least one of the arguments is of type long double (in which case all of them are casted to long double instead).

Parameters

x, y
Values to be multiplied.
z
Value to be added.

Return Value

The result of x*y+z

Example

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;
}


Output:

10.0 * 20.0 + 30.0 = 230.000000

See also