New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
statistics.geometric_mean gives incorrect results for mixed int/float inputs #72514
Comments
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:
... and indeed, we should be starting from |
Here's a fix. I was planning to add tests, but as far as I can tell geometric_mean currently has no tests at all. Steve, is that correct? That seems like something that should be fixed before the 3.6 release. |
Looks good for me. Thanks for catching this: I knew it was a bug, but then I ran into the issue that I could no longer build 3.6 before I could fix it, and between that and various issues in the real world I never got back to this. |
New patch, with a (very slightly) cleaner implementation of |
I can't reproduce this now: >>> statistics.geometric_mean([2, 3, 5, 7])
3.80675409583932
>>> statistics.geometric_mean([2, 3, 5, 7.0])
3.80675409583932
>>> statistics.geometric_mean([2, 3, 5.0, 7.0])
3.80675409583932
>>> statistics.geometric_mean([2, 3.0, 5.0, 7.0])
3.80675409583932
>>> statistics.geometric_mean([2.0, 3.0, 5.0, 7.0])
3.80675409583932
>>> The current geometric_mean was added in PR12638. Is this issue about a previous version? |
Yep. Sorry for failing to close this earlier. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: