From c021753b76791e568013d2d0b1112a1127c3f985 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Sat, 23 May 2026 16:00:52 -0700 Subject: [PATCH 1/5] gh-150318: Fix ``quantiles(method='exclusive')`` returning unsorted cut points for duplicate floats When all data points are identical floats, the interpolation formula in the exclusive branch can produce results off by 1 ULP due to floating point rounding. Hence, adjacent cut points differ and the returned list violates the non-decreasing rule. This short-circuits the interpolation, returning the data value directly instead. --- Lib/statistics.py | 8 +++++++- Lib/test/test_statistics.py | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/Lib/statistics.py b/Lib/statistics.py index 01ca6c51dafcafe..6c8c01d32b39d72 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1217,7 +1217,13 @@ def quantiles(data, *, n=4, method='exclusive'): j = i * m // n # rescale i to m/n j = 1 if j < 1 else ld-1 if j > ld-1 else j # clamp to 1 .. ld-1 delta = i*m - j*n # exact integer math - interpolated = (data[j - 1] * (n - delta) + data[j] * delta) / n + # When the endpoints are equal or delta is zero, avoid + # the interpolation formula which can be off by 1 ULP + # due to floating-point rounding + if (data[j - 1] == data[j]) or not delta: + interpolated = data[j - 1] + else: + interpolated = (data[j - 1] * (n - delta) + data[j] * delta) / n result.append(interpolated) return result diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 677a87b51b91925..551d40ef8c402ce 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -2652,6 +2652,24 @@ def test_equal_inputs(self): self.assertEqual(quantiles(data, method='inclusive'), [10.0, 10.0, 10.0]) + def test_monotonic_with_duplicate_floats(self): + quantiles = statistics.quantiles + for x in (3.141592653589793, # irrational-ish + 1/3, # repeating binary fraction + 0.1, # non-exact decimal + 2.0, # exact power of two + 1e300, # large magnitude + 1e-300, # small magnitude + float.fromhex('0x1.fffffffffffffp+1023'), # near max float + sys.float_info.min, # smallest normal + ): + for n in range(2, 20): + result = quantiles([x, x], n=n, method='exclusive') + self.assertEqual(result, sorted(result), + msg=f'x={x}, n={n}') + self.assertTrue(all(v == x for v in result), + msg=f'x={x}, n={n}') + def test_equal_sized_groups(self): quantiles = statistics.quantiles total = 10_000 From 18cff2beec326b6d8847faad226e129efeb526c0 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 23 May 2026 23:14:28 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2026-05-23-23-14-26.gh-issue-150318.Utah1I.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2026-05-23-23-14-26.gh-issue-150318.Utah1I.rst diff --git a/Misc/NEWS.d/next/Library/2026-05-23-23-14-26.gh-issue-150318.Utah1I.rst b/Misc/NEWS.d/next/Library/2026-05-23-23-14-26.gh-issue-150318.Utah1I.rst new file mode 100644 index 000000000000000..6c0e2340132f7a2 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-23-23-14-26.gh-issue-150318.Utah1I.rst @@ -0,0 +1 @@ +Fix ``quantiles(method='exclusive')`` returning unsorted cut points for duplicate floats. From c98e0413d2c1c74ee523187f43576faf791192f5 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Sun, 24 May 2026 00:01:53 -0700 Subject: [PATCH 3/5] Make values float --- Lib/statistics.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/statistics.py b/Lib/statistics.py index 6c8c01d32b39d72..d8df4e324704c54 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1221,7 +1221,7 @@ def quantiles(data, *, n=4, method='exclusive'): # the interpolation formula which can be off by 1 ULP # due to floating-point rounding if (data[j - 1] == data[j]) or not delta: - interpolated = data[j - 1] + interpolated = float(data[j - 1]) else: interpolated = (data[j - 1] * (n - delta) + data[j] * delta) / n result.append(interpolated) From 8e58cf5f63543d1b13982f7e217db78d563fa12b Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Tue, 18 Aug 2026 17:09:26 -0700 Subject: [PATCH 4/5] Include inclusive too --- Lib/statistics.py | 7 +++++-- Lib/test/test_statistics.py | 13 +++++++------ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Lib/statistics.py b/Lib/statistics.py index d8df4e324704c54..fb74d291b3a50ff 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1206,7 +1206,10 @@ def quantiles(data, *, n=4, method='exclusive'): result = [] for i in range(1, n): j, delta = divmod(i * m, n) - interpolated = (data[j] * (n - delta) + data[j + 1] * delta) / n + if (data[j] == data[j + 1]) or not delta: + interpolated = data[j] / 1 + else: + interpolated = (data[j] * (n - delta) + data[j + 1] * delta) / n result.append(interpolated) return result @@ -1221,7 +1224,7 @@ def quantiles(data, *, n=4, method='exclusive'): # the interpolation formula which can be off by 1 ULP # due to floating-point rounding if (data[j - 1] == data[j]) or not delta: - interpolated = float(data[j - 1]) + interpolated = data[j - 1] / 1 else: interpolated = (data[j - 1] * (n - delta) + data[j] * delta) / n result.append(interpolated) diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 551d40ef8c402ce..cb9a4f90eb3768d 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -2663,12 +2663,13 @@ def test_monotonic_with_duplicate_floats(self): float.fromhex('0x1.fffffffffffffp+1023'), # near max float sys.float_info.min, # smallest normal ): - for n in range(2, 20): - result = quantiles([x, x], n=n, method='exclusive') - self.assertEqual(result, sorted(result), - msg=f'x={x}, n={n}') - self.assertTrue(all(v == x for v in result), - msg=f'x={x}, n={n}') + for method in ('exclusive', 'inclusive'): + for n in range(2, 20): + result = quantiles([x, x], n=n, method=method) + self.assertEqual(result, sorted(result), + msg=f'x={x}, n={n}, method={method!r}') + self.assertTrue(all(v == x for v in result), + msg=f'x={x}, n={n}, method={method!r}') def test_equal_sized_groups(self): quantiles = statistics.quantiles From b149202d52c30f9f125ac9499a1cf4d2d4ac3d76 Mon Sep 17 00:00:00 2001 From: Eduardo Villalpando Mello Date: Fri, 11 Sep 2026 17:40:02 -0700 Subject: [PATCH 5/5] Address PR feedback --- Lib/statistics.py | 21 ++++++++------- Lib/test/test_statistics.py | 27 +++++++++++++++---- ...-05-23-23-14-26.gh-issue-150318.Utah1I.rst | 3 ++- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/Lib/statistics.py b/Lib/statistics.py index a47e0cb377f0a9c..7ccd8d862cb7bff 100644 --- a/Lib/statistics.py +++ b/Lib/statistics.py @@ -1206,10 +1206,13 @@ def quantiles(data, *, n=4, method='exclusive'): result = [] for i in range(1, n): j, delta = divmod(i * m, n) - if (data[j] == data[j + 1]) or not delta: - interpolated = data[j] / 1 + a, b = data[j], data[j + 1] + if a == b and type(a) is type(b): + interpolated = a / 1 + elif 2 * delta <= n: + interpolated = a + (b - a) * delta / n else: - interpolated = (data[j] * (n - delta) + data[j + 1] * delta) / n + interpolated = b - (b - a) * (n - delta) / n result.append(interpolated) return result @@ -1220,13 +1223,13 @@ def quantiles(data, *, n=4, method='exclusive'): j = i * m // n # rescale i to m/n j = 1 if j < 1 else ld-1 if j > ld-1 else j # clamp to 1 .. ld-1 delta = i*m - j*n # exact integer math - # When the endpoints are equal or delta is zero, avoid - # the interpolation formula which can be off by 1 ULP - # due to floating-point rounding - if (data[j - 1] == data[j]) or not delta: - interpolated = data[j - 1] / 1 + a, b = data[j - 1], data[j] + if a == b and type(a) is type(b): + interpolated = a / 1 + elif 2 * delta <= n: + interpolated = a + (b - a) * delta / n else: - interpolated = (data[j - 1] * (n - delta) + data[j] * delta) / n + interpolated = b - (b - a) * (n - delta) / n result.append(interpolated) return result diff --git a/Lib/test/test_statistics.py b/Lib/test/test_statistics.py index 71edf017007d27b..50fd1a43bd727d6 100644 --- a/Lib/test/test_statistics.py +++ b/Lib/test/test_statistics.py @@ -2657,14 +2657,31 @@ def test_monotonic_with_duplicate_floats(self): 1e-300, # small magnitude float.fromhex('0x1.fffffffffffffp+1023'), # near max float sys.float_info.min, # smallest normal + float('inf'), + float('-inf'), ): for method in ('exclusive', 'inclusive'): for n in range(2, 20): - result = quantiles([x, x], n=n, method=method) - self.assertEqual(result, sorted(result), - msg=f'x={x}, n={n}, method={method!r}') - self.assertTrue(all(v == x for v in result), - msg=f'x={x}, n={n}, method={method!r}') + with self.subTest(x=x, n=n, method=method): + result = quantiles([x, x], n=n, method=method) + self.assertEqual(result, sorted(result)) + self.assertTrue(all(v == x for v in result)) + + result = quantiles([0.09999999999999999, 0.1, 0.1], + n=9, method='inclusive') + self.assertEqual(result, sorted(result)) + + def test_mixed_types(self): + data = [Fraction(1, 2), 0.5, 2.0] + for method, expected in [ + ('inclusive', [0.5, 0.5, 0.8, 1.4]), + ('exclusive', [0.5, 0.5, 1.1, 2.3]), + ]: + with self.subTest(method=method): + self.assertEqual( + statistics.quantiles(data, n=5, method=method), + expected, + ) def test_equal_sized_groups(self): quantiles = statistics.quantiles diff --git a/Misc/NEWS.d/next/Library/2026-05-23-23-14-26.gh-issue-150318.Utah1I.rst b/Misc/NEWS.d/next/Library/2026-05-23-23-14-26.gh-issue-150318.Utah1I.rst index 6c0e2340132f7a2..e24e2f49a8db0d1 100644 --- a/Misc/NEWS.d/next/Library/2026-05-23-23-14-26.gh-issue-150318.Utah1I.rst +++ b/Misc/NEWS.d/next/Library/2026-05-23-23-14-26.gh-issue-150318.Utah1I.rst @@ -1 +1,2 @@ -Fix ``quantiles(method='exclusive')`` returning unsorted cut points for duplicate floats. +Fix :func:`statistics.quantiles` returning unsorted cut points for duplicate +floats.