[New-bugs-announce] [issue28327] statistics.geometric_mean gives incorrect results for mixed int/float inputs

Mark Dickinson report at bugs.python.org
Sat Oct 1 08:18:35 EDT 2016


New submission from Mark Dickinson:

The following calculations should all be giving the same result:

>>> import statistics
>>> statistics.geometric_mean([2, 3, 5, 7])
3.80675409583932
>>> statistics.geometric_mean([2, 3, 5, 7.0])
1.6265765616977859
>>> statistics.geometric_mean([2, 3, 5.0, 7.0])
2.4322992790977875
>>> statistics.geometric_mean([2, 3.0, 5.0, 7.0])
3.201085872943679
>>> statistics.geometric_mean([2.0, 3.0, 5.0, 7.0])
3.80675409583932

(Correct result is 3.80675409583932.)

The culprit is this line in statistics._product:

    mant, scale = 1, 0  #math.frexp(prod)  # FIXME

... and indeed, we should be starting from `prod` rather than 1 here. But simply using math.frexp has potential for failure if the accumulated integer product overflows a float.

----------
assignee: steven.daprano
messages: 277808
nosy: mark.dickinson, steven.daprano
priority: normal
severity: normal
status: open
title: statistics.geometric_mean gives incorrect results for mixed int/float inputs
type: behavior
versions: Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue28327>
_______________________________________


More information about the New-bugs-announce mailing list