New submission from Mark Dickinson: 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. ---------- messages: 277995 nosy: mark.dickinson priority: normal severity: normal status: open title: statistics.geometric_mean enters infinite loop for Decimal inputs type: behavior versions: Python 3.6, Python 3.7 _______________________________________ Python tracker <rep...@bugs.python.org> <http://bugs.python.org/issue28351> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com