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

Steven D'Aprano report at bugs.python.org
Sun Oct 6 21:41:18 EDT 2019


Steven D'Aprano <steve+python at pearwood.info> added the comment:

Thanks Warren, the resistance example is excellent, would you like to write up a PR to add it to the docs?

In the original design, I prohibited negative numbers because that seems to be what everyone says you should do, but as far as I can tell it is mathematically well defined so long as the sum of positive values doesn't equal the sum of negative values. And in that case, we could return a NAN.

I don't know that there's any physical meaning to the harmonic mean of negative values (circuits with negative resistances?), but I'm almost tempted to make this "consenting adults" and remove the restriction altogether. That would simplify the implementation and remove any arguments about early-out on zero.

I acknowledge Warren's point about permutations of data, but that applies to anything with an early-out. I don't think that's quite persuasive enough to justify losing the early exit on zero.

Warren, if you feel really strongly about this, feel free to try to change my mind. But for now, I think that documenting that zero triggers an early-out is the right solution.

By the way, I've just noticed that harmonic_mean([INF, INF]) raises. Should it return INF instead?

----------

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


More information about the Python-bugs-list mailing list