[issue41458] Avoid overflow/underflow in math.prod()

Mark Dickinson report at bugs.python.org
Thu Aug 6 07:13:38 EDT 2020


Mark Dickinson <dickinsm at gmail.com> added the comment:

If we want to do this (and I'm still not convinced that we do), I think there's a simpler way: use `frexp` to decompose each float into a fraction and an exponent, multiply the fractions (which barring zeros will all be in [0.5, 1.0)), and keep track of the accumulated exponents separately. Then combine everything at the end.

There's a possibility of the accumulated product of the fractions underflowing, but only every 1000 floats or so, so it's enough to check every 500 floats (say) whether the product is smaller than 2**-500 or not, and scale by 2**1000 (adjusting the exponent correspondingly) if not.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41458>
_______________________________________


More information about the Python-bugs-list mailing list