[Python-ideas] Fused multiply-add (FMA)

Steven D'Aprano steve at pearwood.info
Mon Jan 16 06:04:48 EST 2017


On Mon, Jan 16, 2017 at 11:01:23AM +0100, Stephan Houben wrote:

[...]
> So the following would not be a valid FMA fallback
> 
> double bad_fma(double x, double y, double z) {
>   return x*y + z;
> }
[...]
> Upshot: if we want to provide a software fallback in the Python code, we
> need to do something slow and complicated like musl does.

I don't know about complicated. I think this is pretty simple:

from fractions import Fraction

def fma(x, y, z):
    # Return x*y + z with only a single rounding.
    return float(Fraction(x)*Fraction(y) + Fraction(z))


When speed is not the number one priority and accuracy is important, 
its hard to beat the fractions module.


-- 
Steve


More information about the Python-ideas mailing list