Name

fma, fmaf, fmal
- floating-point multiply and add

Library

libm.lib

Synopsis

  #include <math.h>
  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);

Detailed description

The fma, fmaf, and fmal functions return (x * y) + z, computed with only one rounding error. Using the ordinary multiplication and addition operators, by contrast, results in two roundings: one for the intermediate product and one for the final result.

For instance, the expression 1.2e100 * 2.0e208 - 1.4e308 produces oo due to overflow in the intermediate product, whereas fma(1.2e100, 2.0e208, -1.4e308) returns approximately 1.0e308.

The fused multiply-add operation is often used to improve the accuracy of calculations such as dot products. It may also be used to improve performance on machines that implement it natively. The macros FP_FAST_FMA, FP_FAST_FMAF and FP_FAST_FMAL may be defined in

  #include <math.h>to indicate that fma, fmaf, and fmal (respectively) have comparable or faster speed than a multiply operation followed by an add operation.


Examples

#include <math.h>
int main()
{
   double x1 = 1, x2 = 2, x3 =3, y;
   y = fma( x1, x2, x3 );
   printf( "fma(%f , %f , %f) = %f\n", x1, x2, x3, y );
   y = fmaf( x1, x2, x3 );
   printf( "fmaf(%f , %f , %f) = %f\n", x1, x2, x3, y );
   y = fmal( x1, x2, x3 );
   printf( "fmal(%f , %f , %f) = %f\n", x1, x2, x3, y );
}

         

Output

fma ( 1, 2, 3 ) = 5
fmaf( 1, 2, 3 ) = 5
fmal( 1, 2, 3 ) = 5

         


Implementation notes

In general, these routines will behave as one would expect if x * y + z were computed with unbounded precision and range, then rounded to the precision of the return type. However, on some platforms,

See also

math

Feedback

For additional information or queries on this page send feedback

© 2005-2007 Nokia

Top