Skip to content

Commit d836ecf

Browse files
authored
Merge pull request #3454 from Starbuck5/math-optimize-remove-generic-math-num-check
2 parents 861489a + f5702b6 commit d836ecf

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

src_c/math.c

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ vector_generic_math(PyObject *o1, PyObject *o2, int op)
688688
Py_ssize_t i, dim;
689689
double *vec_coords;
690690
double other_coords[VECTOR_MAX_SIZE] = {0};
691-
double tmp;
691+
double tmp = 0.0;
692692
PyObject *other;
693693
pgVector *vec, *ret = NULL;
694694
if (pgVector_Check(o1)) {
@@ -712,11 +712,15 @@ vector_generic_math(PyObject *o1, PyObject *o2, int op)
712712
if (pg_VectorCoordsFromObj(other, dim, other_coords)) {
713713
op |= OP_ARG_VECTOR;
714714
}
715-
else if (RealNumber_Check(other)) {
716-
op |= OP_ARG_NUMBER;
717-
}
718715
else {
719-
op |= OP_ARG_UNKNOWN;
716+
tmp = PyFloat_AsDouble(other);
717+
if (tmp == -1.0 && PyErr_Occurred()) {
718+
PyErr_Clear();
719+
op |= OP_ARG_UNKNOWN;
720+
}
721+
else {
722+
op |= OP_ARG_NUMBER;
723+
}
720724
}
721725

722726
if (op & OP_INPLACE) {
@@ -761,14 +765,12 @@ vector_generic_math(PyObject *o1, PyObject *o2, int op)
761765
case OP_MUL | OP_ARG_NUMBER:
762766
case OP_MUL | OP_ARG_NUMBER | OP_ARG_REVERSE:
763767
case OP_MUL | OP_ARG_NUMBER | OP_INPLACE:
764-
tmp = PyFloat_AsDouble(other);
765768
for (i = 0; i < dim; i++) {
766769
ret->coords[i] = vec_coords[i] * tmp;
767770
}
768771
break;
769772
case OP_DIV | OP_ARG_NUMBER:
770773
case OP_DIV | OP_ARG_NUMBER | OP_INPLACE:
771-
tmp = PyFloat_AsDouble(other);
772774
if (tmp == 0.) {
773775
PyErr_SetString(PyExc_ZeroDivisionError, "division by zero");
774776
Py_DECREF(ret);
@@ -781,7 +783,6 @@ vector_generic_math(PyObject *o1, PyObject *o2, int op)
781783
break;
782784
case OP_FLOOR_DIV | OP_ARG_NUMBER:
783785
case OP_FLOOR_DIV | OP_ARG_NUMBER | OP_INPLACE:
784-
tmp = PyFloat_AsDouble(other);
785786
if (tmp == 0.) {
786787
PyErr_SetString(PyExc_ZeroDivisionError, "division by zero");
787788
Py_DECREF(ret);
@@ -3970,12 +3971,15 @@ vector_elementwiseproxy_generic_math(PyObject *o1, PyObject *o2, int op)
39703971
return NULL;
39713972
}
39723973
}
3973-
else if (RealNumber_Check(other)) {
3974-
op |= OP_ARG_NUMBER;
3975-
other_value = PyFloat_AsDouble(other);
3976-
}
39773974
else {
3978-
op |= OP_ARG_UNKNOWN;
3975+
other_value = PyFloat_AsDouble(other);
3976+
if (other_value == -1.0 && PyErr_Occurred()) {
3977+
PyErr_Clear();
3978+
op |= OP_ARG_UNKNOWN;
3979+
}
3980+
else {
3981+
op |= OP_ARG_NUMBER;
3982+
}
39793983
}
39803984

39813985
ret = _vector_subtype_new(vec);

0 commit comments

Comments
 (0)