Skip to content
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

geometric_mean can raise OverflowError for large input length #72298

Closed
stevendaprano opened this issue Sep 13, 2016 · 8 comments
Closed

geometric_mean can raise OverflowError for large input length #72298

stevendaprano opened this issue Sep 13, 2016 · 8 comments
Assignees
Labels
3.7 stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@stevendaprano
Copy link
Member

stevendaprano commented Sep 13, 2016

BPO 28111
Nosy @mdickinson, @vstinner, @stevendaprano, @iritkatriel
Files
  • geometric_mean_long_input.py
  • geometric_mean_long_input_v2.py
  • 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:

    assignee = 'https://github.com/stevendaprano'
    closed_at = <Date 2021-08-20.10:01:31.769>
    created_at = <Date 2016-09-13.02:20:33.234>
    labels = ['3.7', 'type-bug', 'library']
    title = 'geometric_mean can raise OverflowError for large input length'
    updated_at = <Date 2021-08-20.10:01:31.768>
    user = 'https://github.com/stevendaprano'

    bugs.python.org fields:

    activity = <Date 2021-08-20.10:01:31.768>
    actor = 'mark.dickinson'
    assignee = 'steven.daprano'
    closed = True
    closed_date = <Date 2021-08-20.10:01:31.769>
    closer = 'mark.dickinson'
    components = ['Library (Lib)']
    creation = <Date 2016-09-13.02:20:33.234>
    creator = 'steven.daprano'
    dependencies = []
    files = ['44944', '44946']
    hgrepos = []
    issue_num = 28111
    keywords = []
    message_count = 8.0
    messages = ['276149', '276201', '276733', '277992', '277993', '277999', '399949', '399950']
    nosy_count = 4.0
    nosy_names = ['mark.dickinson', 'vstinner', 'steven.daprano', 'iritkatriel']
    pr_nums = []
    priority = 'normal'
    resolution = 'out of date'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue28111'
    versions = ['Python 3.7']

    @stevendaprano
    Copy link
    Member Author

    stevendaprano commented Sep 13, 2016

    >>> statistics.geometric_mean([0.7 for _ in range(5000)])
    Traceback (most recent call last):
      File "/Users/mdickinson/Python/cpython-git/Lib/statistics.py", line 362, in float_nroot
        isinfinity = math.isinf(x)
    OverflowError: int too large to convert to float

    See bpo-27975 and bpo-27181

    @stevendaprano stevendaprano self-assigned this Sep 13, 2016
    @stevendaprano stevendaprano added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 13, 2016
    @mdickinson
    Copy link
    Member

    mdickinson commented Sep 13, 2016

    For reference, here's the full traceback. Issue bpo-27975 isn't relevant here - the problem is an out-of-range integer being passed to the math.pow operation.

    taniyama:cpython-git mdickinson$ ./python.exe
    Python 3.7.0a0 (default, Sep 13 2016, 08:36:28) 
    [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import statistics
    >>> statistics.geometric_mean(0.7 for _ in range(5000))
    Traceback (most recent call last):
      File "/Users/mdickinson/Python/cpython-git/Lib/statistics.py", line 343, in float_nroot
        isinfinity = math.isinf(x)
    OverflowError: int too large to convert to float
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "/Users/mdickinson/Python/cpython-git/Lib/statistics.py", line 578, in geometric_mean
        s = 2**p * _nth_root(2**q, n)
      File "/Users/mdickinson/Python/cpython-git/Lib/statistics.py", line 330, in nth_root
        return _nroot_NS.float_nroot(x, n)
      File "/Users/mdickinson/Python/cpython-git/Lib/statistics.py", line 345, in float_nroot
        return _nroot_NS.bignum_nroot(x, n)
      File "/Users/mdickinson/Python/cpython-git/Lib/statistics.py", line 472, in bignum_nroot
        b = 2**q * _nroot_NS.nroot(2**r, n)
      File "/Users/mdickinson/Python/cpython-git/Lib/statistics.py", line 365, in nroot
        r1 = math.pow(x, 1.0/n)
    OverflowError: int too large to convert to float

    @mdickinson
    Copy link
    Member

    mdickinson commented Sep 16, 2016

    See msg276732 in bpo-27761 for a possible solution.

    @mdickinson mdickinson changed the title geometric_mean can raise OverflowError when checking for inf geometric_mean can raise OverflowError for large input length Sep 16, 2016
    @mdickinson
    Copy link
    Member

    mdickinson commented Oct 3, 2016

    The attached patch provides a fix, and is based partly on Tim Peters' suggestions and code from the bpo-27761 discussion. It needs tests. For convenience, it includes the fix for bpo-28327, since it needs the _frexp_gen function added there.

    @mdickinson mdickinson added the 3.7 label Oct 3, 2016
    @mdickinson
    Copy link
    Member

    mdickinson commented Oct 3, 2016

    And here's the actual patch. :-)

    @mdickinson
    Copy link
    Member

    mdickinson commented Oct 3, 2016

    Whoops; that patch was incomplete (it was missing the change to the geometric_mean function itself). Here's an updated patch.

    @iritkatriel
    Copy link
    Member

    iritkatriel commented Aug 20, 2021

    I can't reproduce this now (tried on mac and windows):

    >>> statistics.geometric_mean([0.7 for _ in range(5000)])
    0.7

    The current geometric_mean was added in PR12638. Is this issue about a previous version?

    @mdickinson
    Copy link
    Member

    mdickinson commented Aug 20, 2021

    Thanks, @Irit. Yes, this is about a different version of geometric_mean that didn't end up making it into any release.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants