This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: statistics.geometric_mean can enter infinite loop for Decimal inputs
Type: behavior Stage: resolved
Components: Versions: Python 3.8
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: cheryl.sabella, mark.dickinson, rhettinger, steven.daprano
Priority: normal Keywords:

Created on 2016-10-03 19:33 by mark.dickinson, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg277995 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2016-10-03 19:33
On my machine, the following code enters an infinite loop:

Python 3.7.0a0 (default:14c52bb996be, Oct  3 2016, 20:20:58) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from statistics import geometric_mean
>>> from decimal import Decimal
>>> x = [0.5636536271446626, 0.7185039116960741, 0.5265438727361142]
>>> geometric_mean(map(Decimal, x))

The nth_root implementation for Decimals does a repeated Newton iteration until convergence, with convergence being defined as the current iteration value being exactly equal to the last one. It's very easy for the iteration to end up alternating between two (or more) nearby values, and that's what happens above. This isn't a rare corner case: if you generate triples of random floats, convert to Decimal and apply geometric mean, you'll hit something like the above within just a few trials.

I don't think there's any need for an iteration here: I'd suggest simply computing the nth root directly after increasing the Decimal context precision by a suitable amount. If we do use Newton iteration, it should likely restrict itself to doing a single polishing step, as in the issue #28111 fix and #27181 discussion.
msg339683 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-04-08 23:15
The new version of geometric_mean does not enter an infinite loop for this example, so I am going to close this issue as out of date.
History
Date User Action Args
2022-04-11 14:58:37adminsetgithub: 72537
2019-04-08 23:15:18cheryl.sabellasetstatus: open -> closed

versions: + Python 3.8, - Python 3.7
nosy: + rhettinger, cheryl.sabella

messages: + msg339683
resolution: out of date
stage: resolved
2016-10-04 16:31:06steven.dapranosetversions: - Python 3.6
2016-10-04 12:06:01berker.peksagsetnosy: + steven.daprano
2016-10-03 19:42:26mark.dickinsonsettitle: statistics.geometric_mean enters infinite loop for Decimal inputs -> statistics.geometric_mean can enter infinite loop for Decimal inputs
2016-10-03 19:33:07mark.dickinsoncreate