Skip to content

Overflow handling in statistics.mean() and statistics.fmean() #132667

@olliefr

Description

@olliefr

I'm not sure if the following should be considered a bug or a documentation issue.

In the example that follows, statistics.mean() and statistics.fmean() are called with the same floating-point values.

The former evidently takes some effort to avoid the overflow while the latter does not.

If this works as intended, shouldn't this be mentioned in the statistics docs in some way? Because judging from this (arguably limited) example, when given the same input mean() appears to handle pathological cases better than fmean().

Python 3.12.8 | packaged by Anaconda, Inc. | (main, Dec 11 2024, 16:31:09) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

>>> import statistics, sys

>>> statistics.mean([sys.float_info.max] * 2)
1.7976931348623157e+308

>>> statistics.fmean([sys.float_info.max] * 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/oliver/miniconda3/lib/python3.12/statistics.py", line 510, in fmean
    total = fsum(data)
            ^^^^^^^^^^
OverflowError: intermediate overflow in fsum

Metadata

Metadata

Assignees

Labels

docsDocumentation in the Doc dir

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions