[issue38382] statistics.harmonic_mean fails to raise error with negative input that follows a 0

Raymond Hettinger report at bugs.python.org
Sun Oct 6 15:06:06 EDT 2019


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

Normally early-out behaviors are allowed to skip any checks for subsequent inputs (for example, the any() and all() builtins stop consuming inputs one a final value is found).

However, that reasoning might not apply to a zero input for harmonic_mean().  Technically, the definition of harmonic mean doesn't allow for a zero input value at all (accordingly, MS Excel gives an error for a zero input).  Presumably, the reason that Python's harmonic_mean() returns zero is so that the function degrades gracefully as one of the inputs gets closer to zero.

Options at this point:

* Just document that there is an early-out for zero.

* Stick with the strategy of treating zero as if it were just a very, very small positive input.  The would imply that all of the inputs should be considered and that TypeErrors or StatisticErrors should be raised for later data points (though I'm unclear what this world view implies when the inputs have both a zero and a positive infinity).

* Treat a zero input as an error.  This matches the usual specification of harmonic mean being defined only for non-negative real inputs.

----------
nosy: +mark.dickinson, tim.peters

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


More information about the Python-bugs-list mailing list