From cdb24a75ea8785746025f5f8036d8f01bc39f1fa Mon Sep 17 00:00:00 2001 From: Ankith Date: Tue, 1 Jul 2025 23:38:15 +0530 Subject: [PATCH] Fix math.lerp docs, improve code --- docs/reST/ref/math.rst | 2 +- src_c/math.c | 21 ++++++--------------- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/docs/reST/ref/math.rst b/docs/reST/ref/math.rst index 96532b9f4e..0fd3fffdab 100644 --- a/docs/reST/ref/math.rst +++ b/docs/reST/ref/math.rst @@ -119,7 +119,7 @@ Conversion can be combined with swizzling or slicing to create a new order The formula is: - ``a * value + (1 - value) * b``. + ``a + (b - a) * value``. .. versionadded:: 2.4.0 diff --git a/src_c/math.c b/src_c/math.c index bd6869dddc..b76d2e5084 100644 --- a/src_c/math.c +++ b/src_c/math.c @@ -4494,12 +4494,12 @@ math_lerp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) PyObject *max = args[1]; PyObject *value = args[2]; - if (PyNumber_Check(args[2]) != 1) { - return RAISE(PyExc_TypeError, - "lerp requires the interpolation amount to be number"); - } - + double a = PyFloat_AsDouble(min); + RAISE_ARG_TYPE_ERROR("min") + double b = PyFloat_AsDouble(max); + RAISE_ARG_TYPE_ERROR("max") double t = PyFloat_AsDouble(value); + RAISE_ARG_TYPE_ERROR("value") if (nargs == 4 && !PyObject_IsTrue(args[3])) { ; // pass if do_clamp is false @@ -4513,16 +4513,7 @@ math_lerp(PyObject *self, PyObject *const *args, Py_ssize_t nargs) } } - if (PyNumber_Check(min) && PyNumber_Check(max)) { - return PyFloat_FromDouble(PyFloat_AsDouble(min) * (1 - t) + - PyFloat_AsDouble(max) * t); - } - else { - return RAISE( - PyExc_TypeError, - "math.lerp requires all the arguments to be numbers. To lerp " - "between two vectors, please use the Vector class methods."); - } + return PyFloat_FromDouble(lerp(a, b, t)); } static PyObject *