Hacker News new | past | comments | ask | show | jobs | submit login

It looks like the second. For many of its operations, the statistics module first converts floats to integer ratios (integers are bignums in Python), does intermediate operations using integer arithmetic, and converts back to floats at the end.

Here's the internal _sum function for example, which you can see maps _exact_ratio over the data before summing: https://github.com/python/cpython/blob/3.9/Lib/statistics.py...




There's also fmean() which is for floats:

  >>> data = (1_000_000_000.1, 1.1) * 50_000
  >>> statistics.fmean(data)
  500000000.6
Implementation at https://github.com/python/cpython/blob/6df926f1c46eb6db7b5dc... .


I just found that boltons supports a mean, in boltons.statsutils.mean . It appears to use the naive summation:

  >>> import boltons.statsutils
  >>> data = (1_000_000_000.1, 1.1) * 50_000
  >>> boltons.statsutils.mean(data)
  500000000.60091573
  >>> sum(data) / len(data)
  500000000.60091573




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: