Skip to content

statistics.harmonic_mean returns zero for identical positive subnormal floats #158755

Description

@augusto-rehfeldt

Bug report

Bug description:

Documented behaviour: The harmonic_mean docstring states: "The harmonic mean is the reciprocal of the arithmetic mean of the reciprocals of the data." For identical positive observations, this is the observation itself.

Expected: 1e-320

Actual: 0.0

import math
import statistics
from fractions import Fraction

data = [1e-320, 1e-320]
if len(data) < 2 or not all(math.isfinite(x) and x > 0 for x in data):
    print("REFUTATION REJECTED: invalid input")
else:
    expected = float(len(data) / sum(1 / Fraction(x) for x in data))
    actual = statistics.harmonic_mean(data)
    if actual != expected:
        print(f"REFUTATION CONFIRMED: input={data!r}, actual={actual!r}, expected={expected!r}")
    else:
        print("REFUTATION REJECTED: actual equals documented expectation")

Output on Python 3.14.6 (Windows-11-10.0.26220-SP0), standard library statistics:

REFUTATION CONFIRMED: input=[1e-320, 1e-320], actual=0.0, expected=1e-320

This report was found and written by an automated property-testing tool I run (bugforge). The reproducer above was executed and its output is pasted unedited; no person reviewed the report before it was filed. The search script is in https://github.com/augusto-rehfeldt/bugforge-results/tree/main/statistics-20261003-081656-c3

CPython versions tested on:

3.14

Operating systems tested on:

Windows

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions