Skip to content
This repository has been archived by the owner on Mar 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #9 from styleseat/master
Browse files Browse the repository at this point in the history
Fix ZeroDivisionError in harmonic_mean
  • Loading branch information
avalente committed Jun 17, 2016
2 parents 36740c9 + b469638 commit 366fc7e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
3 changes: 2 additions & 1 deletion appmetrics/statistics.py
Expand Up @@ -399,7 +399,8 @@ def harmonic_mean(data):
if not data:
raise StatisticsError('harmonic_mean requires at least one data point')

return len(data) / sum(map(lambda x: 1.0 / x if x else 0.0, data))
divisor = sum(map(lambda x: 1.0 / x if x else 0.0, data))
return len(data) / divisor if divisor else 0.0


def skewness(data):
Expand Down
3 changes: 3 additions & 0 deletions appmetrics/tests/test_statistics.py
Expand Up @@ -216,6 +216,9 @@ def test_harmonic_mean():
fun = lambda data, expected: nt.assert_almost_equal(mm.harmonic_mean(data), expected)
yield fun, data, expected

def test_harmonic_mean_zero_divisor():
assert mm.harmonic_mean([0.0]) == 0.0

@nt.raises(StatisticsError)
def test_harmonic_mean_empty():
mm.harmonic_mean([])
Expand Down

0 comments on commit 366fc7e

Please sign in to comment.