[Python-ideas] Add nullifier argument to functools.reduce?

Warren Weckesser warren.weckesser at gmail.com
Tue Aug 26 06:09:24 CEST 2014


On Mon, Aug 25, 2014 at 9:43 PM, Stephen J. Turnbull <stephen at xemacs.org>
wrote:

> Warren Weckesser writes:
>
>  > I don't agree that this change "significantly changes the semantics of
>  > reduce".  The nullifier is optional.
>
> Many programs deal with domains that seem to have nullifiers but don't.
> See Steven's example of floating point multiplication where the "tail"
> might contain Inf or NaN.  You can argue that this is a "consenting
> adults" issue, but I consider this possibility an attractive nuisance,
> and I'd rather it not be in the stdlib.
>
>

Gotcha.  To spell it out: one might naively think 0 is a nullifier for
floating point multiplication, but 0*inf is nan, not 0.  That means
reduce(lambda x,y: x*y, [1e-50]*10 + [float('inf')], nullifier=0), which
underflows to 0 during the intermediate steps, would incorrectly return 0.
So yeah, consenting adults, know your data, etc.  :)  (nan, on the other
hand, appears to be a true nullifier for the product in IEEE 754 floating
point arithmetic.)

Warren


IMHO YMMV
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140826/e4f996ab/attachment.html>


More information about the Python-ideas mailing list