<div dir="ltr">My understanding is that NumPy does NOT currently support a direct FMA operation "natively." However, higher-level routines like `numpy.linalg.solve` that are linked to MKL or BLAS DO take advantage of FMA within the underlying libraries.</div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 16, 2017 at 10:06 AM, Guido van Rossum <span dir="ltr"><<a href="mailto:gvanrossum@gmail.com" target="_blank">gvanrossum@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto">Does numpy support this?<br><br><div data-smartmail="gmail_signature">--Guido (mobile)</div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Jan 16, 2017 7:27 AM, "Stephan Houben" <<a href="mailto:stephanh42@gmail.com" target="_blank">stephanh42@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Steve,<div><br></div><div>Very good!</div><div>Here is a version which also handles the nan's, infinities,</div><div>negative zeros properly.</div><div><br></div><div>===============</div><div><div>import math</div><div>from fractions import Fraction</div><div><br></div><div>def fma2(x, y, z):</div><div> if math.isfinite(x) and math.isfinite(y) and math.isfinite(z):</div><div> result = float(Fraction(x)*Fraction(y) + Fraction(z))</div><div> if not result and not z:</div><div> result = math.copysign(result, x*y+z)</div><div> else:</div><div> result = x * y + z</div><div> assert not math.isfinite(result)</div><div> return result</div><div>===========================</div><div><br></div><div>Stephan</div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-01-16 12:04 GMT+01:00 Steven D'Aprano <span dir="ltr"><<a href="mailto:steve@pearwood.info" target="_blank">steve@pearwood.info</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, Jan 16, 2017 at 11:01:23AM +0100, Stephan Houben wrote:<br>
<br>
[...]<br>
<span>> So the following would not be a valid FMA fallback<br>
><br>
> double bad_fma(double x, double y, double z) {<br>
> return x*y + z;<br>
> }<br>
</span>[...]<br>
<span>> Upshot: if we want to provide a software fallback in the Python code, we<br>
> need to do something slow and complicated like musl does.<br>
<br>
</span>I don't know about complicated. I think this is pretty simple:<br>
<br>
from fractions import Fraction<br>
<br>
def fma(x, y, z):<br>
# Return x*y + z with only a single rounding.<br>
return float(Fraction(x)*Fraction(y) + Fraction(z))<br>
<br>
<br>
When speed is not the number one priority and accuracy is important,<br>
its hard to beat the fractions module.<br>
<span class="m_-6458649721746544018m_2404065159388076217HOEnZb"><font color="#888888"><br>
<br>
--<br>
Steve<br>
</font></span><div class="m_-6458649721746544018m_2404065159388076217HOEnZb"><div class="m_-6458649721746544018m_2404065159388076217h5">______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br>
</div></div></blockquote></div><br></div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofco<wbr>nduct/</a><br></blockquote></div></div>
</div></div><br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons. Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div>