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._decimal_to_ratio() produces non-integer ratio
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: ncoghlan Nosy List: larry, ncoghlan, python-dev, skrah, steven.daprano, wolma
Priority: release blocker Keywords: patch

Created on 2014-02-06 23:42 by skrah, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
decimal_to_ratio.patch steven.daprano, 2014-02-08 13:30
Messages (4)
msg210427 - (view) Author: Stefan Krah (skrah) * (Python committer) Date: 2014-02-06 23:42
As I understand, _decimal_to_ratio() should always produce an
integer ratio.  But it does not for positive exponents:

>>> import statistics
>>> statistics.mean([Decimal("100"), Decimal("200")])
Decimal('150')
>>> statistics.mean([Decimal("1e2"), Decimal("2e2")])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.4/statistics.py", line 308, in mean
    return _sum(data)/n
  File "/usr/local/lib/python3.4/statistics.py", line 166, in _sum
    total += Fraction(n, d)
  File "/usr/local/lib/python3.4/fractions.py", line 163, in __new__
    raise TypeError("both arguments should be "
TypeError: both arguments should be Rational instances
>>> 
>>> statistics._decimal_to_ratio(Decimal("1e2"))     
(1, 0.01)
>>> 1e2.as_integer_ratio()
(100, 1)
msg210633 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2014-02-08 13:30
See also issue 20561, which is a duplicate of this.

Fix and tests for this in the attached patch. Could somebody please commit it for me, my ssh key apparently hasn't been registered yet.
msg210643 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-02-08 13:55
New changeset 0f9309f8c755 by Nick Coghlan in branch 'default':
Close #20536: correctly handle Decimal exponents in statistics
http://hg.python.org/cpython/rev/0f9309f8c755
msg210697 - (view) Author: Wolfgang Maier (wolma) * Date: 2014-02-08 22:51
Steven,
I think

if exp >= 0:
    return int(x), 1

would have been a better patch, but yours is certainly working, just slower I guess.
History
Date User Action Args
2022-04-11 14:57:58adminsetgithub: 64735
2014-02-08 22:51:31wolmasetnosy: + wolma
messages: + msg210697
2014-02-08 13:56:02ncoghlanlinkissue20561 superseder
2014-02-08 13:55:26python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg210643

stage: needs patch -> resolved
2014-02-08 13:51:51ncoghlansetassignee: steven.daprano -> ncoghlan

nosy: + ncoghlan
2014-02-08 13:30:46steven.dapranosetfiles: + decimal_to_ratio.patch
priority: high -> release blocker

assignee: steven.daprano

keywords: + patch
nosy: + larry
messages: + msg210633
resolution: fixed
2014-02-06 23:42:19skrahcreate