mirror of
https://github.com/python/cpython.git
synced 2025-08-30 21:48:47 +00:00
bpo-20499: Rounding error in statistics.pvariance (GH-28230)
This commit is contained in:
parent
f235dd0784
commit
4a5cccb02b
3 changed files with 51 additions and 56 deletions
|
@ -1250,20 +1250,14 @@ class TestSum(NumericTestCase):
|
|||
# Override test for empty data.
|
||||
for data in ([], (), iter([])):
|
||||
self.assertEqual(self.func(data), (int, Fraction(0), 0))
|
||||
self.assertEqual(self.func(data, 23), (int, Fraction(23), 0))
|
||||
self.assertEqual(self.func(data, 2.3), (float, Fraction(2.3), 0))
|
||||
|
||||
def test_ints(self):
|
||||
self.assertEqual(self.func([1, 5, 3, -4, -8, 20, 42, 1]),
|
||||
(int, Fraction(60), 8))
|
||||
self.assertEqual(self.func([4, 2, 3, -8, 7], 1000),
|
||||
(int, Fraction(1008), 5))
|
||||
|
||||
def test_floats(self):
|
||||
self.assertEqual(self.func([0.25]*20),
|
||||
(float, Fraction(5.0), 20))
|
||||
self.assertEqual(self.func([0.125, 0.25, 0.5, 0.75], 1.5),
|
||||
(float, Fraction(3.125), 4))
|
||||
|
||||
def test_fractions(self):
|
||||
self.assertEqual(self.func([Fraction(1, 1000)]*500),
|
||||
|
@ -1284,14 +1278,6 @@ class TestSum(NumericTestCase):
|
|||
data = [random.uniform(-100, 1000) for _ in range(1000)]
|
||||
self.assertApproxEqual(float(self.func(data)[1]), math.fsum(data), rel=2e-16)
|
||||
|
||||
def test_start_argument(self):
|
||||
# Test that the optional start argument works correctly.
|
||||
data = [random.uniform(1, 1000) for _ in range(100)]
|
||||
t = self.func(data)[1]
|
||||
self.assertEqual(t+42, self.func(data, 42)[1])
|
||||
self.assertEqual(t-23, self.func(data, -23)[1])
|
||||
self.assertEqual(t+Fraction(1e20), self.func(data, 1e20)[1])
|
||||
|
||||
def test_strings_fail(self):
|
||||
# Sum of strings should fail.
|
||||
self.assertRaises(TypeError, self.func, [1, 2, 3], '999')
|
||||
|
@ -2101,6 +2087,13 @@ class TestPVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin):
|
|||
self.assertEqual(result, exact)
|
||||
self.assertIsInstance(result, Decimal)
|
||||
|
||||
def test_accuracy_bug_20499(self):
|
||||
data = [0, 0, 1]
|
||||
exact = 2 / 9
|
||||
result = self.func(data)
|
||||
self.assertEqual(result, exact)
|
||||
self.assertIsInstance(result, float)
|
||||
|
||||
|
||||
class TestVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin):
|
||||
# Tests for sample variance.
|
||||
|
@ -2141,6 +2134,13 @@ class TestVariance(VarianceStdevMixin, NumericTestCase, UnivariateTypeMixin):
|
|||
self.assertEqual(self.func(data), 0.5)
|
||||
self.assertEqual(self.func(data, xbar=2.0), 1.0)
|
||||
|
||||
def test_accuracy_bug_20499(self):
|
||||
data = [0, 0, 2]
|
||||
exact = 4 / 3
|
||||
result = self.func(data)
|
||||
self.assertEqual(result, exact)
|
||||
self.assertIsInstance(result, float)
|
||||
|
||||
class TestPStdev(VarianceStdevMixin, NumericTestCase):
|
||||
# Tests for population standard deviation.
|
||||
def setUp(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue