Skip to content

Commit c25400a

Browse files
authored
Revert gh-139075. The sumprod fix needs more work. (gh-139092)
1 parent 444ebaf commit c25400a

File tree

3 files changed

+16
-19
lines changed

3 files changed

+16
-19
lines changed

Lib/test/test_math.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,9 +1485,7 @@ def __rmul__(self, other):
14851485

14861486
# Error cases that arose during development
14871487
args = ((-5, -5, 10), (1.5, 4611686018427387904, 2305843009213693952))
1488-
self.assertEqual(sumprod(*args), -7.5)
1489-
self.assertEqual(sumprod([-0.01, 1, -1, 0.01], [1, 1, 1, 1]), 0.0)
1490-
self.assertEqual(sumprod([1, 1, 1, 1], [-0.01, 1, -1, 0.01], ), 0.0)
1488+
self.assertEqual(sumprod(*args), 0.0)
14911489

14921490
@requires_IEEE_754
14931491
@unittest.skipIf(HAVE_DOUBLE_ROUNDING,

Misc/NEWS.d/next/Core_and_Builtins/2025-09-17-13-21-26.gh-issue-139074.dVZO5F.rst

Lines changed: 0 additions & 2 deletions
This file was deleted.

Modules/mathmodule.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2937,31 +2937,32 @@ math_sumprod_impl(PyObject *module, PyObject *p, PyObject *q)
29372937

29382938
if (!finished) {
29392939
double flt_p, flt_q;
2940-
2941-
if (PyFloat_CheckExact(p_i)) {
2940+
bool p_type_float = PyFloat_CheckExact(p_i);
2941+
bool q_type_float = PyFloat_CheckExact(q_i);
2942+
if (p_type_float && q_type_float) {
29422943
flt_p = PyFloat_AS_DOUBLE(p_i);
2943-
} else if (PyLong_CheckExact(p_i) || PyBool_Check(p_i)) {
2944-
flt_p = PyLong_AsDouble(p_i);
2945-
if (flt_p == -1.0 && PyErr_Occurred()) {
2944+
flt_q = PyFloat_AS_DOUBLE(q_i);
2945+
} else if (p_type_float && (PyLong_CheckExact(q_i) || PyBool_Check(q_i))) {
2946+
/* We care about float/int pairs and int/float pairs because
2947+
they arise naturally in several use cases such as price
2948+
times quantity, measurements with integer weights, or
2949+
data selected by a vector of bools. */
2950+
flt_p = PyFloat_AS_DOUBLE(p_i);
2951+
flt_q = PyLong_AsDouble(q_i);
2952+
if (flt_q == -1.0 && PyErr_Occurred()) {
29462953
PyErr_Clear();
29472954
goto finalize_flt_path;
29482955
}
2949-
} else {
2950-
goto finalize_flt_path;
2951-
}
2952-
2953-
if (PyFloat_CheckExact(q_i)) {
2956+
} else if (q_type_float && (PyLong_CheckExact(p_i) || PyBool_Check(p_i))) {
29542957
flt_q = PyFloat_AS_DOUBLE(q_i);
2955-
} else if (PyLong_CheckExact(q_i) || PyBool_Check(q_i)) {
2956-
flt_q = PyLong_AsDouble(q_i);
2957-
if (flt_q == -1.0 && PyErr_Occurred()) {
2958+
flt_p = PyLong_AsDouble(p_i);
2959+
if (flt_p == -1.0 && PyErr_Occurred()) {
29582960
PyErr_Clear();
29592961
goto finalize_flt_path;
29602962
}
29612963
} else {
29622964
goto finalize_flt_path;
29632965
}
2964-
29652966
TripleLength new_flt_total = tl_fma(flt_p, flt_q, flt_total);
29662967
if (isfinite(new_flt_total.hi)) {
29672968
flt_total = new_flt_total;

0 commit comments

Comments
 (0)