mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
bpo-39479:Add math.lcm() function: Least Common Multiple (#18547)
* Update math.rst * Update math.rst * updated whats new * Update test_math.py * Update mathmodule.c * Update mathmodule.c.h * Update ACKS * 📜🤖 Added by blurb_it. * Update 3.9.rst * Update 2020-02-18-12-37-16.bpo-39479.j3UcCq.rst * Update math.rst * Update 2020-02-18-12-37-16.bpo-39479.j3UcCq.rst * Update test_math.py * Update ACKS * Update mathmodule.c.h * Update mathmodule.c * Update mathmodule.c.h * Update mathmodule.c.h Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
This commit is contained in:
parent
4dee92b0ad
commit
f2ee21d858
7 changed files with 134 additions and 1 deletions
|
@ -2017,6 +2017,59 @@ math_factorial(PyObject *module, PyObject *arg)
|
|||
}
|
||||
|
||||
|
||||
/*[clinic input]
|
||||
math.lcm
|
||||
x as a: object
|
||||
y as b: object
|
||||
/
|
||||
least common multiple of x and y
|
||||
[clinic start generated code]*/
|
||||
|
||||
static PyObject *
|
||||
math_lcm_impl(PyObject *module, PyObject *a, PyObject *b)
|
||||
/*[clinic end generated code: output=6f83fb6d671074ba input=efb3d7b7334b7118]*/
|
||||
{
|
||||
PyObject *g, *m, *f, *ab;
|
||||
|
||||
a = PyNumber_Index(a);
|
||||
if (a == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
b = PyNumber_Index(b);
|
||||
if (b == NULL) {
|
||||
Py_DECREF(a);
|
||||
return NULL;
|
||||
}
|
||||
if (_PyLong_Sign(a) == 0 || _PyLong_Sign(b) == 0) {
|
||||
Py_DECREF(a);
|
||||
Py_DECREF(b);
|
||||
return PyLong_FromLong(0);
|
||||
}
|
||||
g = _PyLong_GCD(a, b);
|
||||
if (g == NULL) {
|
||||
Py_DECREF(a);
|
||||
Py_DECREF(b);
|
||||
return NULL;
|
||||
}
|
||||
f = PyNumber_FloorDivide(a, g);
|
||||
Py_DECREF(g);
|
||||
Py_DECREF(a);
|
||||
if (f == NULL) {
|
||||
Py_DECREF(b);
|
||||
return NULL;
|
||||
}
|
||||
m = PyNumber_Multiply(f, b);
|
||||
Py_DECREF(f);
|
||||
Py_DECREF(b);
|
||||
if (m == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
ab = PyNumber_Absolute(m);
|
||||
Py_DECREF(m);
|
||||
return ab;
|
||||
}
|
||||
|
||||
|
||||
/*[clinic input]
|
||||
math.trunc
|
||||
|
||||
|
@ -3362,6 +3415,7 @@ static PyMethodDef math_methods[] = {
|
|||
MATH_ISINF_METHODDEF
|
||||
MATH_ISNAN_METHODDEF
|
||||
MATH_ISQRT_METHODDEF
|
||||
MATH_LCM_METHODDEF
|
||||
MATH_LDEXP_METHODDEF
|
||||
{"lgamma", math_lgamma, METH_O, math_lgamma_doc},
|
||||
MATH_LOG_METHODDEF
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue