mirror of
https://github.com/python/cpython.git
synced 2025-08-31 05:58:33 +00:00
gh-112540: Support zero inputs in geometric_mean() (gh-112880)
This commit is contained in:
parent
76929fdeeb
commit
f3bff4ee9d
3 changed files with 35 additions and 9 deletions
|
@ -2302,10 +2302,12 @@ class TestGeometricMean(unittest.TestCase):
|
|||
StatisticsError = statistics.StatisticsError
|
||||
with self.assertRaises(StatisticsError):
|
||||
geometric_mean([]) # empty input
|
||||
with self.assertRaises(StatisticsError):
|
||||
geometric_mean([3.5, 0.0, 5.25]) # zero input
|
||||
with self.assertRaises(StatisticsError):
|
||||
geometric_mean([3.5, -4.0, 5.25]) # negative input
|
||||
with self.assertRaises(StatisticsError):
|
||||
geometric_mean([0.0, -4.0, 5.25]) # negative input with zero
|
||||
with self.assertRaises(StatisticsError):
|
||||
geometric_mean([3.5, -math.inf, 5.25]) # negative infinity
|
||||
with self.assertRaises(StatisticsError):
|
||||
geometric_mean(iter([])) # empty iterator
|
||||
with self.assertRaises(TypeError):
|
||||
|
@ -2328,6 +2330,12 @@ class TestGeometricMean(unittest.TestCase):
|
|||
with self.assertRaises(ValueError):
|
||||
geometric_mean([Inf, -Inf])
|
||||
|
||||
# Cases with zero
|
||||
self.assertEqual(geometric_mean([3, 0.0, 5]), 0.0) # Any zero gives a zero
|
||||
self.assertEqual(geometric_mean([3, -0.0, 5]), 0.0) # Negative zero allowed
|
||||
self.assertTrue(math.isnan(geometric_mean([0, NaN]))) # NaN beats zero
|
||||
self.assertTrue(math.isnan(geometric_mean([0, Inf]))) # Because 0.0 * Inf -> NaN
|
||||
|
||||
def test_mixed_int_and_float(self):
|
||||
# Regression test for b.p.o. issue #28327
|
||||
geometric_mean = statistics.geometric_mean
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue