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.

Author mark.dickinson
Recipients mark.dickinson
Date 2016-10-03.19:33:07
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1475523187.92.0.24883134157.issue28351@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2016-10-03 19:33:07mark.dickinsonsetrecipients: + mark.dickinson
2016-10-03 19:33:07mark.dickinsonsetmessageid: <1475523187.92.0.24883134157.issue28351@psf.upfronthosting.co.za>
2016-10-03 19:33:07mark.dickinsonlinkissue28351 messages
2016-10-03 19:33:07mark.dickinsoncreate