# Author juraj.sukop juraj.sukop 2017-01-16.10:01:35 -1.0 Yes <1484560897.19.0.0955404368118.issue29282@psf.upfronthosting.co.za>
Content
```Fused multiply-add (henceforth FMA) is an operation which calculates the product of two numbers and then the sum of the product and a third number with just one floating-point rounding. More concretely:

r = x*y + z

The value of `r` is the same as if the RHS was calculated with infinite precision and the rounded to a 32-bit single-precision or 64-bit double-precision floating-point number .

Even though one FMA CPU instruction might be calculated faster than the two separate instructions for multiply and add, its main advantage comes from the increased precision of numerical computations that involve the accumulation of products. Examples which benefit from using FMA are: dot product , compensated arithmetic , polynomial evaluation , matrix multiplication, Newton's method and many more .

C99 includes  `fma` function to `math.h` and emulates the calculation if the FMA instruction is not present on the host CPU . PEP 7 states that "Python versions greater than or equal to 3.6 use C89 with several select C99 features" and that "Future C99 features may be added to this list in the future depending on compiler support" .

This proposal is then about adding new `fma` function with the following signature to `math` module:

math.fma(x, y, z)

'''Return a float representing the result of the operation `x*y + z` with single rounding error, as defined by the platform C library. The result is the same as if the operation was carried with infinite precision and rounded to a floating-point number.'''

Attached is a simple module for Python 3 demonstrating the fused multiply-add operation. On my machine, `example.py` prints:

40037.524591982365   horner_double
40037.48821639768    horner_fma
40037.49486325783    horner_compensated
40037.49486325783    horner_decimal

 https://en.wikipedia.org/wiki/Multiply%E2%80%93accumulate_operation
 S. Graillat, P. Langlois, N. Louvet. Accurate dot products with FMA. 2006
 S. Graillat, Accurate Floating Point Product and Exponentiation. 2007.
 S. Graillat, P. Langlois, N. Louvet. Improving the compensated Horner scheme with a Fused Multiply and Add. 2006
 J.-M. Muller, N. Brisebarre, F. de Dinechin, C.-P. Jeannerod, V. Lefèvre, G. Melquiond, N. Revol, D. Stehlé, S. Torres. Handbook of Floating-Point Arithmetic. 2010. Chapter 5
 ISO/IEC 9899:TC3, "7.12.13.1 The fma functions", Committee Draft - Septermber 7, 2007
 https://git.musl-libc.org/cgit/musl/tree/src/math/fma.c
 https://www.python.org/dev/peps/pep-0007/```
History
Date User Action Args
2017-01-16 10:01:37juraj.sukopsetrecipients: + juraj.sukop
2017-01-16 10:01:37juraj.sukopsetmessageid: <1484560897.19.0.0955404368118.issue29282@psf.upfronthosting.co.za>