[Python-ideas] Fused multiply-add (FMA)
Guido van Rossum
gvanrossum at gmail.com
Mon Jan 16 13:06:33 EST 2017
Does numpy support this?
--Guido (mobile)
On Jan 16, 2017 7:27 AM, "Stephan Houben" <stephanh42 at gmail.com> wrote:
> Hi Steve,
>
> Very good!
> Here is a version which also handles the nan's, infinities,
> negative zeros properly.
>
> ===============
> import math
> from fractions import Fraction
>
> def fma2(x, y, z):
> if math.isfinite(x) and math.isfinite(y) and math.isfinite(z):
> result = float(Fraction(x)*Fraction(y) + Fraction(z))
> if not result and not z:
> result = math.copysign(result, x*y+z)
> else:
> result = x * y + z
> assert not math.isfinite(result)
> return result
> ===========================
>
> Stephan
>
>
> 2017-01-16 12:04 GMT+01:00 Steven D'Aprano <steve at pearwood.info>:
>
>> 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
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170116/b8054034/attachment.html>
More information about the Python-ideas
mailing list