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/3] 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/3] =?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/3] 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)