From 78c612abc18bed54f8911c84ba6f93eb9f041c31 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 17:49:04 +0530 Subject: [PATCH 01/83] Update 3.9.rst --- Doc/whatsnew/3.9.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index 931f8bf13fbde6..7700d4cc36e0d8 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -216,6 +216,9 @@ Add :func:`math.ulp`: return the value of the least significant bit of a float. (Contributed by Victor Stinner in :issue:`39310`.) +Add :func:`math.lcm`: return the least common multiple of *a* and *b*. +(Contributed by Ananthakrishnan in :issue:`39479`.) + nntplib ------- From 8300b4e836af5e26a1205e0c5c137bceb56d894b Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 17:51:59 +0530 Subject: [PATCH 02/83] Update math.rst --- Doc/library/math.rst | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index c4c180037f8788..bf2d3401dbeccc 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -136,6 +136,15 @@ Number-theoretic and representation functions .. versionadded:: 3.5 +.. function:: lcm(a, b) + + Return the least common multiple of integers *a* and *b*. If either *a* or + *b* is nonzero,then the value of ``lcm(a, b)`` is the smallest positive + integer that is divisible by both *a* and *b*.``lcm(0, 0)`` returns ``0``. + +.. versionadded:: 1.0 + + .. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) Return ``True`` if the values *a* and *b* are close to each other and From 4c215e8dc1a92e97e1d463c76ea29115ba849f68 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 17:54:47 +0530 Subject: [PATCH 03/83] Update test_math.py --- Lib/test/test_math.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index b3301f6a5cf74f..223061c5eb3fa9 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -974,6 +974,49 @@ def __index__(self): with self.assertRaises(TypeError): math.isqrt(value) + def testlcm(self): + lcm = math.lcm + self.assertEqual(lcm(0, 0), 0) + self.assertEqual(lcm(1, 0), 1) + self.assertEqual(lcm(-1, 0), 1) + self.assertEqual(lcm(0, 1), 1) + self.assertEqual(lcm(0, -1), 1) + self.assertEqual(lcm(7, 1), 1) + self.assertEqual(lcm(7, -1), 1) + self.assertEqual(lcm(-23, 15), 1) + self.assertEqual(lcm(120, 84), 12) + self.assertEqual(lcm(84, -120), 12) + self.assertEqual(lcm(1216342683557601535506311712, + 436522681849110124616458784), 32) + c = 652560 + x = 434610456570399902378880679233098819019853229470286994367836600566 + y = 1064502245825115327754847244914921553977 + a = x * c + b = y * c + self.assertEqual(lcm(a, b), c) + self.assertEqual(lcm(b, a), c) + self.assertEqual(lcm(-a, b), c) + self.assertEqual(lcm(b, -a), c) + self.assertEqual(lcm(a, -b), c) + self.assertEqual(lcm(-b, a), c) + self.assertEqual(lcm(-a, -b), c) + self.assertEqual(lcm(-b, -a), c) + c = 576559230871654959816130551884856912003141446781646602790216406874 + a = x * c + b = y * c + self.assertEqual(lcm(a, b), c) + self.assertEqual(lcm(b, a), c) + self.assertEqual(lcm(-a, b), c) + self.assertEqual(lcm(b, -a), c) + self.assertEqual(lcm(a, -b), c) + self.assertEqual(lcm(-b, a), c) + self.assertEqual(lcm(-a, -b), c) + self.assertEqual(lcm(-b, -a), c) + + self.assertRaises(TypeError, lcm, 120.0, 84) + self.assertRaises(TypeError, lcm, 120, 84.0) + self.assertEqual(lcm(MyIndexable(120), MyIndexable(84)), 12) + def testLdexp(self): self.assertRaises(TypeError, math.ldexp) self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) From 5ad99af693d23acedb40d2e1bdab9ebb45300a32 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 17:57:04 +0530 Subject: [PATCH 04/83] Update mathmodule.c --- Modules/mathmodule.c | 73 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index f012b51d86698d..3b8a854c1e5c04 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2016,6 +2016,78 @@ 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(PyObject*module,PyObject*a,PyObject*b) +{ + PyObject * g,*m,*f,*ab; + if (PyFloat_Check(a)||PyFloat_Check(b)) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "lcm() cannot be used with float", + 1) < 0) + { + return NULL; + } + double dx = PyFloat_AS_DOUBLE((PyFloatObject *)a); + double ax= PyFloat_AS_DOUBLE((PyFloatObject *)b); + if (!((Py_IS_FINITE(dx) && dx == floor(dx))||(Py_IS_FINITE(ax) && ax == floor(ax))) + { + PyErr_SetString(PyExc_ValueError, + "lcm() only accepts integral values"); + return NULL; + } + a=PyNumber_Index(a); + if(a==NULL) + return NULL; + b=PyNumber_Index(b); + if (b== NULL) + { + Py_DECREF(a); + return NULL; + } + g=_PyLong_GCD(a,b); + Py_DECREF(a); + Py_DECREF(b); + if(g==NULL) + { + Py_DECREF(g); + return NULL; + } + m=PyNumber_Multiply(a,b); + Py_DECREF(a); + Py_DECREF(b); + if(m==NULL) + { + Py_DECREF(m); + return NULL; + } + f=PyNumber_FloorDivide(m,g); + Py_DECREF(m); + Py_DECREF(g); + if(f==NULL) + { + Py_DECREF(f); + return NULL; + } + ab=PyNumber_Absolute(f); + Py_DECREF(f); + if(ab==NULL) + { + Py_DECREF(ab); + return NULL; + } + return g; +} + + /*[clinic input] math.trunc @@ -3335,6 +3407,7 @@ static PyMethodDef math_methods[] = { {"asin", math_asin, METH_O, math_asin_doc}, {"asinh", math_asinh, METH_O, math_asinh_doc}, {"atan", math_atan, METH_O, math_atan_doc}, + {"lcm", math_lcm, METH_O, math_lcm_doc}, {"atan2", (PyCFunction)(void(*)(void))math_atan2, METH_FASTCALL, math_atan2_doc}, {"atanh", math_atanh, METH_O, math_atanh_doc}, MATH_CEIL_METHODDEF From c3731390d19ce02b7f9fefdaf11a0c95c7d77173 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 18:44:04 +0530 Subject: [PATCH 05/83] Update Doc/library/math.rst Co-Authored-By: Dong-hee Na --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index bf2d3401dbeccc..c0555df953814a 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -142,7 +142,7 @@ Number-theoretic and representation functions *b* is nonzero,then the value of ``lcm(a, b)`` is the smallest positive integer that is divisible by both *a* and *b*.``lcm(0, 0)`` returns ``0``. -.. versionadded:: 1.0 +.. versionadded:: 3.9 .. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) From a23ba1451a92d3de8b8b6a9b78a9ff720736e8f6 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 19:33:42 +0530 Subject: [PATCH 06/83] Update Doc/library/math.rst Co-Authored-By: Victor Stinner --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index c0555df953814a..813803a715a26d 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -139,7 +139,7 @@ Number-theoretic and representation functions .. function:: lcm(a, b) Return the least common multiple of integers *a* and *b*. If either *a* or - *b* is nonzero,then the value of ``lcm(a, b)`` is the smallest positive + *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive integer that is divisible by both *a* and *b*.``lcm(0, 0)`` returns ``0``. .. versionadded:: 3.9 From b540fc70850dd432031a17830e351e210c672a03 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 19:34:22 +0530 Subject: [PATCH 07/83] Update Lib/test/test_math.py Co-Authored-By: Victor Stinner --- Lib/test/test_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 223061c5eb3fa9..3ef452b37bb1c0 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -974,7 +974,7 @@ def __index__(self): with self.assertRaises(TypeError): math.isqrt(value) - def testlcm(self): + def test_lcm(self): lcm = math.lcm self.assertEqual(lcm(0, 0), 0) self.assertEqual(lcm(1, 0), 1) From 13be573d514f71a4356dae6380f77652bb1a0793 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 19:35:00 +0530 Subject: [PATCH 08/83] Update Doc/library/math.rst Co-Authored-By: Victor Stinner --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 813803a715a26d..101e0f2db54487 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -140,7 +140,7 @@ Number-theoretic and representation functions Return the least common multiple of integers *a* and *b*. If either *a* or *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive - integer that is divisible by both *a* and *b*.``lcm(0, 0)`` returns ``0``. + integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. .. versionadded:: 3.9 From a3a36ab73664abe989d5b07feec7e8c501b26424 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 20:00:49 +0530 Subject: [PATCH 09/83] Update mathmodule.c --- Modules/mathmodule.c | 70 ++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 3b8a854c1e5c04..c566f51d46b9b1 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2026,64 +2026,58 @@ least common multiple of x and y [clinic start generated code]*/ static PyObject* -math_lcm(PyObject*module,PyObject*a,PyObject*b) +math_lcm(PyObject*module, PyObject*a, PyObject*b) { PyObject * g,*m,*f,*ab; - if (PyFloat_Check(a)||PyFloat_Check(b)) { - if (PyErr_WarnEx(PyExc_DeprecationWarning, - "lcm() cannot be used with float", - 1) < 0) - { - return NULL; - } - double dx = PyFloat_AS_DOUBLE((PyFloatObject *)a); - double ax= PyFloat_AS_DOUBLE((PyFloatObject *)b); - if (!((Py_IS_FINITE(dx) && dx == floor(dx))||(Py_IS_FINITE(ax) && ax == floor(ax))) - { - PyErr_SetString(PyExc_ValueError, - "lcm() only accepts integral values"); - return NULL; - } + a=PyNumber_Index(a); - if(a==NULL) + + if (a == NULL) { return NULL; + } + b=PyNumber_Index(b); - if (b== NULL) - { + + if (b== NULL) { Py_DECREF(a); return NULL; - } - g=_PyLong_GCD(a,b); + } + + g=_PyLong_GCD(a, b); Py_DECREF(a); Py_DECREF(b); - if(g==NULL) - { - Py_DECREF(g); - return NULL; + + if (g==NULL) { + Py_DECREF(g); + return NULL; } - m=PyNumber_Multiply(a,b); + + m=PyNumber_Multiply(a, b); Py_DECREF(a); Py_DECREF(b); - if(m==NULL) - { + + if (m==NULL) { Py_DECREF(m); return NULL; - } - f=PyNumber_FloorDivide(m,g); + } + + f=PyNumber_FloorDivide(m, g); Py_DECREF(m); Py_DECREF(g); - if(f==NULL) - { - Py_DECREF(f); + + if (f==NULL) { + Py_DECREF(f); return NULL; - } + } + ab=PyNumber_Absolute(f); Py_DECREF(f); - if(ab==NULL) - { - Py_DECREF(ab); + + if (ab==NULL) { + Py_DECREF(ab); return NULL; - } + } + return g; } From 34efffa0ade6845235227426b2e415dff8a0a7a6 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 21:16:01 +0530 Subject: [PATCH 10/83] Update test_math.py --- Lib/test/test_math.py | 37 ++++++++++++++----------------------- 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 3ef452b37bb1c0..41c04e896fc586 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -988,31 +988,22 @@ def test_lcm(self): self.assertEqual(lcm(84, -120), 12) self.assertEqual(lcm(1216342683557601535506311712, 436522681849110124616458784), 32) - c = 652560 x = 434610456570399902378880679233098819019853229470286994367836600566 y = 1064502245825115327754847244914921553977 - a = x * c - b = y * c - self.assertEqual(lcm(a, b), c) - self.assertEqual(lcm(b, a), c) - self.assertEqual(lcm(-a, b), c) - self.assertEqual(lcm(b, -a), c) - self.assertEqual(lcm(a, -b), c) - self.assertEqual(lcm(-b, a), c) - self.assertEqual(lcm(-a, -b), c) - self.assertEqual(lcm(-b, -a), c) - c = 576559230871654959816130551884856912003141446781646602790216406874 - a = x * c - b = y * c - self.assertEqual(lcm(a, b), c) - self.assertEqual(lcm(b, a), c) - self.assertEqual(lcm(-a, b), c) - self.assertEqual(lcm(b, -a), c) - self.assertEqual(lcm(a, -b), c) - self.assertEqual(lcm(-b, a), c) - self.assertEqual(lcm(-a, -b), c) - self.assertEqual(lcm(-b, -a), c) - + + for c in (652560, + 576559230871654959816130551884856912003141446781646602790216406874, + ): + a = x * c + b = y * c + self.assertEqual(lcm(a, b), c) + self.assertEqual(lcm(b, a), c) + self.assertEqual(lcm(-a, b), c) + self.assertEqual(lcm(b, -a), c) + self.assertEqual(lcm(a, -b), c) + self.assertEqual(lcm(-b, a), c) + self.assertEqual(lcm(-a, -b), c) + self.assertEqual(lcm(-b, -a), c) self.assertRaises(TypeError, lcm, 120.0, 84) self.assertRaises(TypeError, lcm, 120, 84.0) self.assertEqual(lcm(MyIndexable(120), MyIndexable(84)), 12) From e42b00c36177af994a877b0588678544ea6932d6 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 22:38:40 +0530 Subject: [PATCH 11/83] Update mathmodule.c --- Modules/mathmodule.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index c566f51d46b9b1..60a697ad1950de 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2026,54 +2026,54 @@ least common multiple of x and y [clinic start generated code]*/ static PyObject* -math_lcm(PyObject*module, PyObject*a, PyObject*b) +math_lcm(PyObject *module, PyObject *a, PyObject *b) { - PyObject * g,*m,*f,*ab; + PyObject*g,*m,*f,*ab; - a=PyNumber_Index(a); + a = PyNumber_Index(a); if (a == NULL) { return NULL; } - b=PyNumber_Index(b); + b = PyNumber_Index(b); - if (b== NULL) { + if (b == NULL) { Py_DECREF(a); return NULL; } - g=_PyLong_GCD(a, b); + g = _PyLong_GCD(a, b); Py_DECREF(a); Py_DECREF(b); - if (g==NULL) { + if (g == NULL) { Py_DECREF(g); return NULL; } - m=PyNumber_Multiply(a, b); + m = PyNumber_Multiply(a, b); Py_DECREF(a); Py_DECREF(b); - if (m==NULL) { + if (m == NULL) { Py_DECREF(m); return NULL; } - f=PyNumber_FloorDivide(m, g); + f = PyNumber_FloorDivide(m, g); Py_DECREF(m); Py_DECREF(g); - if (f==NULL) { + if (f == NULL) { Py_DECREF(f); return NULL; } - ab=PyNumber_Absolute(f); + ab = PyNumber_Absolute(f); Py_DECREF(f); - if (ab==NULL) { + if (ab == NULL) { Py_DECREF(ab); return NULL; } From 091c4a6ab8521695ec563ae34a676d062e74d6c5 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 22:40:48 +0530 Subject: [PATCH 12/83] Update mathmodule.c --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 60a697ad1950de..6bfa4e244fedf2 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2028,7 +2028,7 @@ least common multiple of x and y static PyObject* math_lcm(PyObject *module, PyObject *a, PyObject *b) { - PyObject*g,*m,*f,*ab; + PyObject *g, *m, *f, *ab; a = PyNumber_Index(a); From 99b1364711c248a7395d820f6908b62c4550cc26 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 23:07:44 +0530 Subject: [PATCH 13/83] Update mathmodule.c --- Modules/mathmodule.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 6bfa4e244fedf2..2e05515589339f 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2081,6 +2081,13 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) return g; } +PyDoc_STRVAR(math_lcm_doc, +"lcm($module, x, y, /)\n" +"--\n" +"\n" + +"least common multiple of x and y"); + /*[clinic input] math.trunc From a5d89b5ea1b8f3f053033120ac0926ec823684ee Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 23:12:20 +0530 Subject: [PATCH 14/83] Update math.rst --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 101e0f2db54487..5d5d2447bdc050 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -142,7 +142,7 @@ Number-theoretic and representation functions *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. -.. versionadded:: 3.9 + .. versionadded:: 3.9 .. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0) From 7823c12f12ffd07f59bca0fa0bcb392af6103e33 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 23:26:00 +0530 Subject: [PATCH 15/83] Update math.rst --- Doc/library/math.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 5d5d2447bdc050..e4a802fbd29d69 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -139,8 +139,10 @@ Number-theoretic and representation functions .. function:: lcm(a, b) Return the least common multiple of integers *a* and *b*. If either *a* or - *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive - integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. + *b* is nonzero,then the value of ``lcm(a, b)`` is the smallest positive + integer that is divisible by both *a* and *b*.``lcm(0, 0)`` returns ``0``. + ``lcm(a, b)`` returns *g* by calling ``gcd(a, b)``. + Thus ``lcm(a, b)=abs(a // gcd(a, b) * b)``. .. versionadded:: 3.9 From ce474a82648bdeb0b46b26c3b95b1ecf34d48a7c Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 23:29:45 +0530 Subject: [PATCH 16/83] Update Doc/library/math.rst Co-Authored-By: Victor Stinner --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index e4a802fbd29d69..dbfb69dc9e6c10 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -140,7 +140,7 @@ Number-theoretic and representation functions Return the least common multiple of integers *a* and *b*. If either *a* or *b* is nonzero,then the value of ``lcm(a, b)`` is the smallest positive - integer that is divisible by both *a* and *b*.``lcm(0, 0)`` returns ``0``. + integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. ``lcm(a, b)`` returns *g* by calling ``gcd(a, b)``. Thus ``lcm(a, b)=abs(a // gcd(a, b) * b)``. From aa9abb73bb3e5224a1a7987e9fb21dfd28a2e164 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 23:31:29 +0530 Subject: [PATCH 17/83] Update Doc/library/math.rst Co-Authored-By: Victor Stinner --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index dbfb69dc9e6c10..d6b5353aac7fa1 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -139,7 +139,7 @@ Number-theoretic and representation functions .. function:: lcm(a, b) Return the least common multiple of integers *a* and *b*. If either *a* or - *b* is nonzero,then the value of ``lcm(a, b)`` is the smallest positive + *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. ``lcm(a, b)`` returns *g* by calling ``gcd(a, b)``. Thus ``lcm(a, b)=abs(a // gcd(a, b) * b)``. From 79bf932d9a437783460a6788c416d4a17750e3db Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 3 Feb 2020 23:33:18 +0530 Subject: [PATCH 18/83] Update math.rst --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index d6b5353aac7fa1..3a4839d617724c 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -142,7 +142,7 @@ Number-theoretic and representation functions *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. ``lcm(a, b)`` returns *g* by calling ``gcd(a, b)``. - Thus ``lcm(a, b)=abs(a // gcd(a, b) * b)``. + Thus ``lcm(a, b) = abs(a // gcd(a, b) * b)``. .. versionadded:: 3.9 From 972f8e6d20c7c62d7d5e82c828f11d0fd2b9edf4 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 19:05:05 +0530 Subject: [PATCH 19/83] Update math.rst --- Doc/library/math.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 3a4839d617724c..50e9dd9a319e5e 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -138,9 +138,9 @@ Number-theoretic and representation functions .. function:: lcm(a, b) - Return the least common multiple of integers *a* and *b*. If either *a* or + Return the least common multiple of integers *a* and *b*. If either *a* or *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive - integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. + integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. ``lcm(a, b)`` returns *g* by calling ``gcd(a, b)``. Thus ``lcm(a, b) = abs(a // gcd(a, b) * b)``. From 75988952ee8ed082ca64bde7de948ae79b8f9ed6 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Tue, 4 Feb 2020 13:47:18 +0000 Subject: [PATCH 20/83] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst diff --git a/Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst b/Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst new file mode 100644 index 00000000000000..cf888d71417f0a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst @@ -0,0 +1 @@ +Add `math.lcm()` function: Least Common Multiple. \ No newline at end of file From 42669da079a7598779253d43d0c22b3ec0ffacfe Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 19:26:12 +0530 Subject: [PATCH 21/83] Update math.rst --- Doc/library/math.rst | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 50e9dd9a319e5e..17ae31e96b3a15 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -141,9 +141,7 @@ Number-theoretic and representation functions Return the least common multiple of integers *a* and *b*. If either *a* or *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. - ``lcm(a, b)`` returns *g* by calling ``gcd(a, b)``. - Thus ``lcm(a, b) = abs(a // gcd(a, b) * b)``. - + .. versionadded:: 3.9 From 4ee5b20da323b104dba8f3d39d062511a3393f6f Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 19:36:33 +0530 Subject: [PATCH 22/83] Update mathmodule.c --- Modules/mathmodule.c | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 2e05515589339f..58e0f4a31f96e2 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2031,48 +2031,37 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) 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; } - g = _PyLong_GCD(a, b); Py_DECREF(a); Py_DECREF(b); - if (g == NULL) { Py_DECREF(g); return NULL; } - m = PyNumber_Multiply(a, b); Py_DECREF(a); Py_DECREF(b); - if (m == NULL) { Py_DECREF(m); return NULL; } - f = PyNumber_FloorDivide(m, g); Py_DECREF(m); Py_DECREF(g); - if (f == NULL) { Py_DECREF(f); return NULL; } - ab = PyNumber_Absolute(f); Py_DECREF(f); - if (ab == NULL) { Py_DECREF(ab); return NULL; From 97a54dbec251e96fe3895ffac2855e92d147cfb1 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 20:01:30 +0530 Subject: [PATCH 23/83] Update mathmodule.c --- Modules/mathmodule.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 58e0f4a31f96e2..ef1ca363ba3599 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2040,22 +2040,20 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) return NULL; } g = _PyLong_GCD(a, b); - Py_DECREF(a); - Py_DECREF(b); if (g == NULL) { Py_DECREF(g); return NULL; } - m = PyNumber_Multiply(a, b); + m = PyNumber_Multiply(g, a); + Py_DECREF(g); Py_DECREF(a); - Py_DECREF(b); if (m == NULL) { Py_DECREF(m); return NULL; } - f = PyNumber_FloorDivide(m, g); + f = PyNumber_FloorDivide(a, m); + Py_DECREF(a); Py_DECREF(m); - Py_DECREF(g); if (f == NULL) { Py_DECREF(f); return NULL; @@ -2067,7 +2065,7 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) return NULL; } - return g; + return ab; } PyDoc_STRVAR(math_lcm_doc, From 4072d9238a59402170e00cad9ea44f0c2db1db5d Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 20:03:03 +0530 Subject: [PATCH 24/83] Update math.rst --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 17ae31e96b3a15..e8f087b8af6c5a 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -138,7 +138,7 @@ Number-theoretic and representation functions .. function:: lcm(a, b) - Return the least common multiple of integers *a* and *b*. If either *a* or + Return the least common multiple of integers *a* and *b*. If both *a* or *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. From 38fa5ed663e59f83749e005eca460ae898afc8c8 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 20:05:46 +0530 Subject: [PATCH 25/83] Update math.rst --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index e8f087b8af6c5a..88384ff02da2de 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -140,7 +140,7 @@ Number-theoretic and representation functions Return the least common multiple of integers *a* and *b*. If both *a* or *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive - integer that is divisible by both *a* and *b*. ``lcm(0, 0)`` returns ``0``. + integer that is multiple of both *a* and *b*. ``lcm(0, 0)`` returns ``0``. .. versionadded:: 3.9 From e616c7405043f447a3bd2ef6e18377edcf895696 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 21:47:29 +0530 Subject: [PATCH 26/83] Update test_math.py --- Lib/test/test_math.py | 60 +++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 41c04e896fc586..13fc5963248f93 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -977,36 +977,46 @@ def __index__(self): def test_lcm(self): lcm = math.lcm self.assertEqual(lcm(0, 0), 0) - self.assertEqual(lcm(1, 0), 1) - self.assertEqual(lcm(-1, 0), 1) - self.assertEqual(lcm(0, 1), 1) - self.assertEqual(lcm(0, -1), 1) - self.assertEqual(lcm(7, 1), 1) - self.assertEqual(lcm(7, -1), 1) - self.assertEqual(lcm(-23, 15), 1) - self.assertEqual(lcm(120, 84), 12) - self.assertEqual(lcm(84, -120), 12) + self.assertEqual(lcm(1, 0), 0) + self.assertEqual(lcm(-1, 0), 0) + self.assertEqual(lcm(0, 1), 0) + self.assertEqual(lcm(0, -1), 0) + self.assertEqual(lcm(7, 1), 7) + self.assertEqual(lcm(7, -1), 7) + self.assertEqual(lcm(-23, 15), 345) + self.assertEqual(lcm(120, 84), 840) + self.assertEqual(lcm(84, -120), 840) self.assertEqual(lcm(1216342683557601535506311712, - 436522681849110124616458784), 32) + 436522681849110124616458784), + 16592536571065866494401400422922201534178938447014944) + c = 652560 x = 434610456570399902378880679233098819019853229470286994367836600566 y = 1064502245825115327754847244914921553977 - - for c in (652560, - 576559230871654959816130551884856912003141446781646602790216406874, - ): - a = x * c - b = y * c - self.assertEqual(lcm(a, b), c) - self.assertEqual(lcm(b, a), c) - self.assertEqual(lcm(-a, b), c) - self.assertEqual(lcm(b, -a), c) - self.assertEqual(lcm(a, -b), c) - self.assertEqual(lcm(-b, a), c) - self.assertEqual(lcm(-a, -b), c) - self.assertEqual(lcm(-b, -a), c) + a = x * c + b = y * c + d=301902842746995509648393721908560577474971578942451106771953796961736341663923782888922697319520284637980813920 652560 + self.assertEqual(lcm(a, b), d) + self.assertEqual(lcm(b, a), d) + self.assertEqual(lcm(-a, b), d) + self.assertEqual(lcm(b, -a), d) + self.assertEqual(lcm(a, -b), d) + self.assertEqual(lcm(-b, a), d) + self.assertEqual(lcm(-a, -b), d) + self.assertEqual(lcm(-b, -a), d) + e=266741557576581350556796933681042612304362158146595254541737503518981783311378696068765001186892238054415825401472091853286591831877639677191947722648985671425215285050268 + c = 576559230871654959816130551884856912003141446781646602790216406874 + a = x * c + b = y * c + self.assertEqual(lcm(a, b), e) + self.assertEqual(lcm(b, a), e) + self.assertEqual(lcm(-a, b), e) + self.assertEqual(lcm(b, -a), e) + self.assertEqual(lcm(a, -b), e) + self.assertEqual(lcm(-b, a), e) + self.assertEqual(lcm(-a, -b), e) + self.assertEqual(lcm(-b, -a), e) self.assertRaises(TypeError, lcm, 120.0, 84) self.assertRaises(TypeError, lcm, 120, 84.0) - self.assertEqual(lcm(MyIndexable(120), MyIndexable(84)), 12) def testLdexp(self): self.assertRaises(TypeError, math.ldexp) From fbb064a3c03520bbd67fbadd658fa9c72e939ecd Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 22:08:40 +0530 Subject: [PATCH 27/83] Update mathmodule.c --- Modules/mathmodule.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index ef1ca363ba3599..b5806645d4bd8d 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2047,6 +2047,7 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) m = PyNumber_Multiply(g, a); Py_DECREF(g); Py_DECREF(a); + Py_DECREF(g); if (m == NULL) { Py_DECREF(m); return NULL; @@ -2054,12 +2055,14 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) f = PyNumber_FloorDivide(a, m); Py_DECREF(a); Py_DECREF(m); + Py_DECREF(m); if (f == NULL) { Py_DECREF(f); return NULL; } ab = PyNumber_Absolute(f); Py_DECREF(f); + Py_DECREF(f); if (ab == NULL) { Py_DECREF(ab); return NULL; From a72f5ddda8ad868169e9a8d61ced2ae28eb57c8f Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 22:50:32 +0530 Subject: [PATCH 28/83] Update mathmodule.c --- Modules/mathmodule.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index b5806645d4bd8d..a96da7539c1e1d 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2041,30 +2041,28 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) } g = _PyLong_GCD(a, b); if (g == NULL) { - Py_DECREF(g); + Py_DECREF(a); + Py_DECREF(b); return NULL; } m = PyNumber_Multiply(g, a); Py_DECREF(g); - Py_DECREF(a); - Py_DECREF(g); if (m == NULL) { - Py_DECREF(m); + Py_DECREF(g); + Py_DECREF(a); return NULL; } f = PyNumber_FloorDivide(a, m); - Py_DECREF(a); - Py_DECREF(m); Py_DECREF(m); if (f == NULL) { - Py_DECREF(f); + Py_DECREF(a); + Py_DECREF(m); return NULL; } ab = PyNumber_Absolute(f); - Py_DECREF(f); - Py_DECREF(f); + Py_DECREF(m); if (ab == NULL) { - Py_DECREF(ab); + Py_DECREF(f); return NULL; } From 049d867a72497e828ac46ce33320f7d4e948b1d1 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 22:51:50 +0530 Subject: [PATCH 29/83] Update Lib/test/test_math.py Co-Authored-By: Serhiy Storchaka --- Lib/test/test_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 13fc5963248f93..c7cfc333e65b74 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -994,7 +994,7 @@ def test_lcm(self): y = 1064502245825115327754847244914921553977 a = x * c b = y * c - d=301902842746995509648393721908560577474971578942451106771953796961736341663923782888922697319520284637980813920 652560 + d = x * y * c self.assertEqual(lcm(a, b), d) self.assertEqual(lcm(b, a), d) self.assertEqual(lcm(-a, b), d) From 6ed80744fbba4240690002326b76f0efc26b0034 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 22:57:48 +0530 Subject: [PATCH 30/83] Update test_math.py --- Lib/test/test_math.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index c7cfc333e65b74..e6922f43c783c3 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -1002,11 +1002,11 @@ def test_lcm(self): self.assertEqual(lcm(a, -b), d) self.assertEqual(lcm(-b, a), d) self.assertEqual(lcm(-a, -b), d) - self.assertEqual(lcm(-b, -a), d) - e=266741557576581350556796933681042612304362158146595254541737503518981783311378696068765001186892238054415825401472091853286591831877639677191947722648985671425215285050268 - c = 576559230871654959816130551884856912003141446781646602790216406874 + self.assertEqual(lcm(-b, -a), d) + c = 576559230871654959816130551884856912003141446781646602790216406874 a = x * c b = y * c + e = x * y * c self.assertEqual(lcm(a, b), e) self.assertEqual(lcm(b, a), e) self.assertEqual(lcm(-a, b), e) From e18e43b86e59ced9b489586217451fde13dac729 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Tue, 4 Feb 2020 23:22:59 +0530 Subject: [PATCH 31/83] Update mathmodule.c --- Modules/mathmodule.c | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index a96da7539c1e1d..a88d55062b4c10 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2042,39 +2042,33 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) g = _PyLong_GCD(a, b); if (g == NULL) { Py_DECREF(a); - Py_DECREF(b); + Py_DECREF(b); return NULL; } m = PyNumber_Multiply(g, a); Py_DECREF(g); if (m == NULL) { Py_DECREF(g); - Py_DECREF(a); + Py_DECREF(a); return NULL; - } + } f = PyNumber_FloorDivide(a, m); Py_DECREF(m); if (f == NULL) { Py_DECREF(a); Py_DECREF(m); return NULL; - } + } ab = PyNumber_Absolute(f); Py_DECREF(m); if (ab == NULL) { Py_DECREF(f); return NULL; } - return ab; } -PyDoc_STRVAR(math_lcm_doc, -"lcm($module, x, y, /)\n" -"--\n" -"\n" - -"least common multiple of x and y"); + /*[clinic input] From 4e3c1cdbfa914eae2fe51a01091060df9fcd5e6c Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Wed, 5 Feb 2020 00:41:06 +0530 Subject: [PATCH 32/83] Update test_math.py --- Lib/test/test_math.py | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index e6922f43c783c3..2c6415dbf57c65 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -989,32 +989,22 @@ def test_lcm(self): self.assertEqual(lcm(1216342683557601535506311712, 436522681849110124616458784), 16592536571065866494401400422922201534178938447014944) - c = 652560 x = 434610456570399902378880679233098819019853229470286994367836600566 y = 1064502245825115327754847244914921553977 - a = x * c - b = y * c - d = x * y * c - self.assertEqual(lcm(a, b), d) - self.assertEqual(lcm(b, a), d) - self.assertEqual(lcm(-a, b), d) - self.assertEqual(lcm(b, -a), d) - self.assertEqual(lcm(a, -b), d) - self.assertEqual(lcm(-b, a), d) - self.assertEqual(lcm(-a, -b), d) - self.assertEqual(lcm(-b, -a), d) - c = 576559230871654959816130551884856912003141446781646602790216406874 - a = x * c - b = y * c - e = x * y * c - self.assertEqual(lcm(a, b), e) - self.assertEqual(lcm(b, a), e) - self.assertEqual(lcm(-a, b), e) - self.assertEqual(lcm(b, -a), e) - self.assertEqual(lcm(a, -b), e) - self.assertEqual(lcm(-b, a), e) - self.assertEqual(lcm(-a, -b), e) - self.assertEqual(lcm(-b, -a), e) + + for c in (652560, + 576559230871654959816130551884856912003141446781646602790216406874) + a = x * c + b = y * c + d = x * y * c + self.assertEqual(lcm(a, b), d) + self.assertEqual(lcm(b, a), d) + self.assertEqual(lcm(-a, b), d) + self.assertEqual(lcm(b, -a), d) + self.assertEqual(lcm(a, -b), d) + self.assertEqual(lcm(-b, a), d) + self.assertEqual(lcm(-a, -b), d) + self.assertEqual(lcm(-b, -a), d) self.assertRaises(TypeError, lcm, 120.0, 84) self.assertRaises(TypeError, lcm, 120, 84.0) From d8b03f98b33dbdacde145c29f8eccb736fdc4c4e Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Wed, 5 Feb 2020 00:44:11 +0530 Subject: [PATCH 33/83] Update mathmodule.c --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index a88d55062b4c10..8cd9d94964c38a 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -3390,7 +3390,6 @@ static PyMethodDef math_methods[] = { {"asin", math_asin, METH_O, math_asin_doc}, {"asinh", math_asinh, METH_O, math_asinh_doc}, {"atan", math_atan, METH_O, math_atan_doc}, - {"lcm", math_lcm, METH_O, math_lcm_doc}, {"atan2", (PyCFunction)(void(*)(void))math_atan2, METH_FASTCALL, math_atan2_doc}, {"atanh", math_atanh, METH_O, math_atanh_doc}, MATH_CEIL_METHODDEF @@ -3417,6 +3416,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 From 389c49741dc316aa8b8e3a5dff2355745d35d5d4 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Wed, 5 Feb 2020 00:45:53 +0530 Subject: [PATCH 34/83] Update mathmodule.c.h --- Modules/clinic/mathmodule.c.h | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/Modules/clinic/mathmodule.c.h b/Modules/clinic/mathmodule.c.h index f95d291d41f804..df45a1a0c5e855 100644 --- a/Modules/clinic/mathmodule.c.h +++ b/Modules/clinic/mathmodule.c.h @@ -85,6 +85,36 @@ PyDoc_STRVAR(math_factorial__doc__, #define MATH_FACTORIAL_METHODDEF \ {"factorial", (PyCFunction)math_factorial, METH_O, math_factorial__doc__}, +PyDoc_STRVAR(math_lcm__doc__, +"lcm($module, x, y, /)\n" +"--\n" +"\n" +"least common multiple of x and y"); + +#define MATH_LCM_METHODDEF \ + {"lcm", (PyCFunction)(void(*)(void))math_lcm, METH_FASTCALL, math_lcm__doc__}, + +static PyObject * +math_lcm_impl(PyObject *module, PyObject *a, PyObject *b); + +static PyObject * +math_lcm(PyObject *module, PyObject *const *args, Py_ssize_t nargs) +{ + PyObject *return_value = NULL; + PyObject *a; + PyObject *b; + + if (!_PyArg_CheckPositional("lcm", nargs, 2, 2)) { + goto exit; + } + a = args[0]; + b = args[1]; + return_value = math_lcm_impl(module, a, b); + +exit: + return return_value; +} + PyDoc_STRVAR(math_trunc__doc__, "trunc($module, x, /)\n" "--\n" @@ -895,4 +925,4 @@ math_ulp(PyObject *module, PyObject *arg) exit: return return_value; } -/*[clinic end generated code: output=9b51d215dbcac060 input=a9049054013a1b77]*/ +/*[clinic end generated code: output=f8daa185c043a7b7 input=a9049054013a1b77]*/ From 97da5b8aadc56ed84eb4ec825097702270b447c4 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Wed, 5 Feb 2020 00:53:01 +0530 Subject: [PATCH 35/83] Update mathmodule.c --- Modules/mathmodule.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 8cd9d94964c38a..de2f3a1a1daf1b 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2019,12 +2019,16 @@ math_factorial(PyObject *module, PyObject *arg) /*[clinic input] math.lcm - x as a: object - y as b: object + 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=bd41b785dc2a4ff1]*/ + static PyObject* math_lcm(PyObject *module, PyObject *a, PyObject *b) { @@ -2041,14 +2045,12 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) } g = _PyLong_GCD(a, b); if (g == NULL) { - Py_DECREF(a); Py_DECREF(b); return NULL; } m = PyNumber_Multiply(g, a); Py_DECREF(g); if (m == NULL) { - Py_DECREF(g); Py_DECREF(a); return NULL; } @@ -2056,7 +2058,6 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) Py_DECREF(m); if (f == NULL) { Py_DECREF(a); - Py_DECREF(m); return NULL; } ab = PyNumber_Absolute(f); From bc6490bdc676b3f2f130f6ddc9fbc41a943ca90f Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Wed, 5 Feb 2020 17:43:15 +0530 Subject: [PATCH 36/83] Update mathmodule.c --- Modules/mathmodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index de2f3a1a1daf1b..2c347a22e79785 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2045,6 +2045,7 @@ math_lcm(PyObject *module, PyObject *a, PyObject *b) } g = _PyLong_GCD(a, b); if (g == NULL) { + Py_DECREF(a); Py_DECREF(b); return NULL; } From 227dee6188a4c7fbe6e917262f68c7fec5837779 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Thu, 6 Feb 2020 17:54:00 +0530 Subject: [PATCH 37/83] Update mathmodule.c --- Modules/mathmodule.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 2c347a22e79785..9fce4f285b00c2 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2028,9 +2028,6 @@ least common multiple of x and y static PyObject * math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=6f83fb6d671074ba input=bd41b785dc2a4ff1]*/ - -static PyObject* -math_lcm(PyObject *module, PyObject *a, PyObject *b) { PyObject *g, *m, *f, *ab; From 177d42c5d0a7d83d5c465452ea4276ecfb13bff5 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 16:17:21 +0530 Subject: [PATCH 38/83] Update Lib/test/test_math.py Co-Authored-By: Dong-hee Na --- Lib/test/test_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 2c6415dbf57c65..24a46f0084d405 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -974,7 +974,7 @@ def __index__(self): with self.assertRaises(TypeError): math.isqrt(value) - def test_lcm(self): + def test_lcm(self): lcm = math.lcm self.assertEqual(lcm(0, 0), 0) self.assertEqual(lcm(1, 0), 0) From e7e5a84bcc5c33fe67d3814ca809112e5da0d9ff Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 16:17:43 +0530 Subject: [PATCH 39/83] Update Lib/test/test_math.py Co-Authored-By: Dong-hee Na --- Lib/test/test_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 24a46f0084d405..3a46566f9ee404 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -993,7 +993,7 @@ def test_lcm(self): y = 1064502245825115327754847244914921553977 for c in (652560, - 576559230871654959816130551884856912003141446781646602790216406874) + 576559230871654959816130551884856912003141446781646602790216406874): a = x * c b = y * c d = x * y * c From 26ca487aadf6ea667050ad52506fd83f2bd710d3 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 16:22:35 +0530 Subject: [PATCH 40/83] Update mathmodule.c --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 9fce4f285b00c2..2ea12ce8646597 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2046,7 +2046,7 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) Py_DECREF(b); return NULL; } - m = PyNumber_Multiply(g, a); + m = PyNumber_Multiply(g, b); Py_DECREF(g); if (m == NULL) { Py_DECREF(a); From 30f0226848d6b425c90fe36dc24509cbbcef5f4e Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 16:31:10 +0530 Subject: [PATCH 41/83] Update mathmodule.c --- Modules/mathmodule.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 2ea12ce8646597..ea72bfc6cf263a 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2059,7 +2059,8 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) return NULL; } ab = PyNumber_Absolute(f); - Py_DECREF(m); + Py_DECREF(a); + Py_DECREF(b); if (ab == NULL) { Py_DECREF(f); return NULL; From 08b1fd2586474afe11acf0ca734755aec192f781 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 16:35:28 +0530 Subject: [PATCH 42/83] Update mathmodule.c --- Modules/mathmodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index ea72bfc6cf263a..da27431212bfa8 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2056,6 +2056,7 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) Py_DECREF(m); if (f == NULL) { Py_DECREF(a); + Py_DECREF(b); return NULL; } ab = PyNumber_Absolute(f); From d504b862cfd56f291d43b2f8f42dcbdc9b8f6641 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 16:38:17 +0530 Subject: [PATCH 43/83] Update mathmodule.c --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index da27431212bfa8..106ef357e00884 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2054,9 +2054,9 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) } f = PyNumber_FloorDivide(a, m); Py_DECREF(m); + Py_DECREF(b); if (f == NULL) { Py_DECREF(a); - Py_DECREF(b); return NULL; } ab = PyNumber_Absolute(f); From 53d3af10977c1f10647d7a7a62f8c0b3adb5e4c1 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 16:40:34 +0530 Subject: [PATCH 44/83] Update mathmodule.c --- Modules/mathmodule.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 106ef357e00884..5a481aab18fc14 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2048,6 +2048,7 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) } m = PyNumber_Multiply(g, b); Py_DECREF(g); + Py_DECREF(b); if (m == NULL) { Py_DECREF(a); return NULL; From 0700ed6409dd522e4bdad6ae2a1421678b27785a Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 18:07:27 +0530 Subject: [PATCH 45/83] Update mathmodule.c --- Modules/mathmodule.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 5a481aab18fc14..9bc39a304c7230 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2030,43 +2030,45 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) /*[clinic end generated code: output=6f83fb6d671074ba input=bd41b785dc2a4ff1]*/ { PyObject *g, *m, *f, *ab; - + a = PyNumber_Index(a); - if (a == NULL) { + if (a == NULL) { return NULL; } b = PyNumber_Index(b); if (b == NULL) { - Py_DECREF(a); return NULL; } g = _PyLong_GCD(a, b); + Py_DECREF(a); + Py_DECREF(b); + Py_DECREF(a); + Py_DECREF(b); if (g == NULL) { - Py_DECREF(a); - Py_DECREF(b); return NULL; } - m = PyNumber_Multiply(g, b); + f = PyNumber_FloorDivide(a, g); + Py_DECREF(a); Py_DECREF(g); + Py_DECREF(g); + if (f == NULL) { + return NULL; + } + m = PyNumber_Multiply(f, b); + Py_DECREF(f); + Py_DECREF(f); Py_DECREF(b); if (m == NULL) { - Py_DECREF(a); return NULL; } - f = PyNumber_FloorDivide(a, m); + ab = PyNumber_Absolute(m); Py_DECREF(m); - Py_DECREF(b); - if (f == NULL) { - Py_DECREF(a); + Py_DECREF(m); + if (ab == NULL) { return NULL; } - ab = PyNumber_Absolute(f); Py_DECREF(a); Py_DECREF(b); - if (ab == NULL) { - Py_DECREF(f); - return NULL; - } return ab; } From e45850791ca9a91faea7c688f9f3e34a9ea51088 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 20:37:08 +0530 Subject: [PATCH 46/83] Update mathmodule.c --- Modules/mathmodule.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 9bc39a304c7230..cc4e75c5925d40 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2039,6 +2039,9 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (b == NULL) { return NULL; } + if(a == 0 || b==0) { + return 0; + } g = _PyLong_GCD(a, b); Py_DECREF(a); Py_DECREF(b); From 67aad6160af79837431cea908e66a6af9e6cdbf0 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 20:48:19 +0530 Subject: [PATCH 47/83] Update math.rst --- Doc/library/math.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 88384ff02da2de..59edf1f8dfa9f7 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -139,9 +139,9 @@ Number-theoretic and representation functions .. function:: lcm(a, b) Return the least common multiple of integers *a* and *b*. If both *a* or - *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive + *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive integer that is multiple of both *a* and *b*. ``lcm(0, 0)`` returns ``0``. - + .. versionadded:: 3.9 From 517912b462080a122ea8b4f0c19082cfdfdba9fb Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 20:50:47 +0530 Subject: [PATCH 48/83] Update 3.9.rst --- Doc/whatsnew/3.9.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index 7700d4cc36e0d8..95d173fa6ea674 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -217,7 +217,7 @@ of a float. (Contributed by Victor Stinner in :issue:`39310`.) Add :func:`math.lcm`: return the least common multiple of *a* and *b*. -(Contributed by Ananthakrishnan in :issue:`39479`.) +(Contributed by Ananthakrishnan in :issue:`39479`.) nntplib ------- From 375c2581f6183a2ce7f1f5a6442cae6d688d5f11 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 20:56:31 +0530 Subject: [PATCH 49/83] Update math.rst --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 59edf1f8dfa9f7..19709fe90e8ff0 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -141,7 +141,7 @@ Number-theoretic and representation functions Return the least common multiple of integers *a* and *b*. If both *a* or *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive integer that is multiple of both *a* and *b*. ``lcm(0, 0)`` returns ``0``. - + .. versionadded:: 3.9 From 76fed5d5f8d056334c572247828a22b4a56184da Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 21:06:39 +0530 Subject: [PATCH 50/83] Update mathmodule.c --- Modules/mathmodule.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index cc4e75c5925d40..5205ab4fbd019b 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2042,6 +2042,7 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if(a == 0 || b==0) { return 0; } + else { g = _PyLong_GCD(a, b); Py_DECREF(a); Py_DECREF(b); @@ -2073,6 +2074,7 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) Py_DECREF(a); Py_DECREF(b); return ab; + } } From d128e2d96b327239394eb3272603bcfa698e294a Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 21:15:52 +0530 Subject: [PATCH 51/83] Update test_math.py --- Lib/test/test_math.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 3a46566f9ee404..653f08b157cd37 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -991,7 +991,6 @@ def test_lcm(self): 16592536571065866494401400422922201534178938447014944) x = 434610456570399902378880679233098819019853229470286994367836600566 y = 1064502245825115327754847244914921553977 - for c in (652560, 576559230871654959816130551884856912003141446781646602790216406874): a = x * c @@ -1004,10 +1003,10 @@ def test_lcm(self): self.assertEqual(lcm(a, -b), d) self.assertEqual(lcm(-b, a), d) self.assertEqual(lcm(-a, -b), d) - self.assertEqual(lcm(-b, -a), d) + self.assertEqual(lcm(-b, -a), d) self.assertRaises(TypeError, lcm, 120.0, 84) self.assertRaises(TypeError, lcm, 120, 84.0) - + def testLdexp(self): self.assertRaises(TypeError, math.ldexp) self.ftest('ldexp(0,1)', math.ldexp(0,1), 0) From e009376c947c2cd1ba66bbe0e2de0ee93e67d02c Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 21:30:35 +0530 Subject: [PATCH 52/83] Update test_math.py --- Lib/test/test_math.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 653f08b157cd37..47e3c75d53e466 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -976,7 +976,6 @@ def __index__(self): def test_lcm(self): lcm = math.lcm - self.assertEqual(lcm(0, 0), 0) self.assertEqual(lcm(1, 0), 0) self.assertEqual(lcm(-1, 0), 0) self.assertEqual(lcm(0, 1), 0) From 4a4f24171fe146fcd637327b9fcc6817d9199194 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 21:32:31 +0530 Subject: [PATCH 53/83] Update mathmodule.c --- Modules/mathmodule.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 5205ab4fbd019b..9bc39a304c7230 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2039,10 +2039,6 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (b == NULL) { return NULL; } - if(a == 0 || b==0) { - return 0; - } - else { g = _PyLong_GCD(a, b); Py_DECREF(a); Py_DECREF(b); @@ -2074,7 +2070,6 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) Py_DECREF(a); Py_DECREF(b); return ab; - } } From a596a839d9ae8bec1208e38b754efa643288957a Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 22:18:12 +0530 Subject: [PATCH 54/83] Update mathmodule.c --- Modules/mathmodule.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 9bc39a304c7230..21f97d776ef855 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2039,6 +2039,14 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (b == NULL) { return NULL; } + if (PyString_Check(a) || PyString_Check(b)) { + PyErr_Format(PyExc_TypeError,"'str' object cannot be an argument to lcm"); + return; + } + if (PyFloat_Check(a) || PyFloat_Check(b)) { + PyErr_Format(PyExc_TypeError,"'float' object cannot be an argument to lcm"); + return; + } g = _PyLong_GCD(a, b); Py_DECREF(a); Py_DECREF(b); @@ -2046,11 +2054,16 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) Py_DECREF(b); if (g == NULL) { return NULL; - } + } + if (g==0) { + f=0; + } + else { f = PyNumber_FloorDivide(a, g); Py_DECREF(a); Py_DECREF(g); Py_DECREF(g); + } if (f == NULL) { return NULL; } From 92cd405278a7c2946d0241bb846823822eb81d14 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 22:21:51 +0530 Subject: [PATCH 55/83] Update test_math.py --- Lib/test/test_math.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 47e3c75d53e466..c8f476c58232a4 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -976,6 +976,7 @@ def __index__(self): def test_lcm(self): lcm = math.lcm + self.assertEqual(lcm(0, 0), 0) self.assertEqual(lcm(1, 0), 0) self.assertEqual(lcm(-1, 0), 0) self.assertEqual(lcm(0, 1), 0) @@ -990,6 +991,7 @@ def test_lcm(self): 16592536571065866494401400422922201534178938447014944) x = 434610456570399902378880679233098819019853229470286994367836600566 y = 1064502245825115327754847244914921553977 + for c in (652560, 576559230871654959816130551884856912003141446781646602790216406874): a = x * c @@ -1005,6 +1007,9 @@ def test_lcm(self): self.assertEqual(lcm(-b, -a), d) self.assertRaises(TypeError, lcm, 120.0, 84) self.assertRaises(TypeError, lcm, 120, 84.0) + self.assertRaises(TypeError, lcm, "a", 7) + self.assertRaises(TypeError, lcm, 7, "a") + self.assertRaises(TypeError, lcm, "a", "a") def testLdexp(self): self.assertRaises(TypeError, math.ldexp) From c069214b0097a644d5b7ea6153a75996928c9460 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 22:28:13 +0530 Subject: [PATCH 56/83] Update mathmodule.c --- Modules/mathmodule.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 21f97d776ef855..0c52918e99aa6f 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2039,13 +2039,13 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (b == NULL) { return NULL; } - if (PyString_Check(a) || PyString_Check(b)) { + if (PyStr_Check(a) || PyStr_Check(b)) { PyErr_Format(PyExc_TypeError,"'str' object cannot be an argument to lcm"); - return; + return 0; } if (PyFloat_Check(a) || PyFloat_Check(b)) { PyErr_Format(PyExc_TypeError,"'float' object cannot be an argument to lcm"); - return; + return 0; } g = _PyLong_GCD(a, b); Py_DECREF(a); From 8543b035ada645cfb39837bbdff044e040833cf8 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 22:46:47 +0530 Subject: [PATCH 57/83] Update mathmodule.c --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 0c52918e99aa6f..734be809374f5d 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2039,7 +2039,7 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (b == NULL) { return NULL; } - if (PyStr_Check(a) || PyStr_Check(b)) { + if (PyObject_TypeCheck(a, PyBytes_Type) || PyObject_TypeCheck(b, PyBytes_Type)) { PyErr_Format(PyExc_TypeError,"'str' object cannot be an argument to lcm"); return 0; } From 4566c08a1a14957b52d321b39cffae761babacec Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 22:53:55 +0530 Subject: [PATCH 58/83] Update mathmodule.c --- Modules/mathmodule.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 734be809374f5d..086c3f5943a1d7 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2039,10 +2039,6 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (b == NULL) { return NULL; } - if (PyObject_TypeCheck(a, PyBytes_Type) || PyObject_TypeCheck(b, PyBytes_Type)) { - PyErr_Format(PyExc_TypeError,"'str' object cannot be an argument to lcm"); - return 0; - } if (PyFloat_Check(a) || PyFloat_Check(b)) { PyErr_Format(PyExc_TypeError,"'float' object cannot be an argument to lcm"); return 0; From 66e6d6536c6f0de19b3b673bd742f915e0669c1e Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 23:05:58 +0530 Subject: [PATCH 59/83] Update mathmodule.c --- Modules/mathmodule.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 086c3f5943a1d7..65f101959fb299 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2051,8 +2051,8 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (g == NULL) { return NULL; } - if (g==0) { - f=0; + if (g == 0) { + f = 0; } else { f = PyNumber_FloorDivide(a, g); From c063ea31d0834e1c1e4cff774a22e1e114af9806 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 23:07:00 +0530 Subject: [PATCH 60/83] Update test_math.py --- Lib/test/test_math.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index c8f476c58232a4..46dd58be28091a 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -1007,9 +1007,6 @@ def test_lcm(self): self.assertEqual(lcm(-b, -a), d) self.assertRaises(TypeError, lcm, 120.0, 84) self.assertRaises(TypeError, lcm, 120, 84.0) - self.assertRaises(TypeError, lcm, "a", 7) - self.assertRaises(TypeError, lcm, 7, "a") - self.assertRaises(TypeError, lcm, "a", "a") def testLdexp(self): self.assertRaises(TypeError, math.ldexp) From f45794846f8ea9f6a4e4962a8bb540c94ec8c6a4 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 23:16:07 +0530 Subject: [PATCH 61/83] Update test_math.py --- Lib/test/test_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 46dd58be28091a..849fcd08304785 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -976,7 +976,6 @@ def __index__(self): def test_lcm(self): lcm = math.lcm - self.assertEqual(lcm(0, 0), 0) self.assertEqual(lcm(1, 0), 0) self.assertEqual(lcm(-1, 0), 0) self.assertEqual(lcm(0, 1), 0) @@ -1007,6 +1006,7 @@ def test_lcm(self): self.assertEqual(lcm(-b, -a), d) self.assertRaises(TypeError, lcm, 120.0, 84) self.assertRaises(TypeError, lcm, 120, 84.0) + self.assertRaises(ZeroDivisionError, lcm, 0, 0) def testLdexp(self): self.assertRaises(TypeError, math.ldexp) From ea7273509de84937a10d6f7e76c3e87798cf5288 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 23:21:25 +0530 Subject: [PATCH 62/83] Update mathmodule.c --- Modules/mathmodule.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 65f101959fb299..256e41d8967180 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2039,6 +2039,10 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (b == NULL) { return NULL; } + if (a == 0 && b == 0) { + PyErr_Format(PyExc_ZeroDivisionError,"lcm(0,0) is not possible"); + return 0; + } if (PyFloat_Check(a) || PyFloat_Check(b)) { PyErr_Format(PyExc_TypeError,"'float' object cannot be an argument to lcm"); return 0; @@ -2051,15 +2055,10 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (g == NULL) { return NULL; } - if (g == 0) { - f = 0; - } - else { f = PyNumber_FloorDivide(a, g); Py_DECREF(a); Py_DECREF(g); Py_DECREF(g); - } if (f == NULL) { return NULL; } From 46f10f2e506c3ebb64192eacc8a900dbb4ffad73 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 23:34:36 +0530 Subject: [PATCH 63/83] Update mathmodule.c --- Modules/mathmodule.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 256e41d8967180..cdc600a2774d77 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2071,7 +2071,6 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) } ab = PyNumber_Absolute(m); Py_DECREF(m); - Py_DECREF(m); if (ab == NULL) { return NULL; } From 5b4428b5b67c5410e600a2274085fe64268f84d3 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Fri, 7 Feb 2020 23:47:48 +0530 Subject: [PATCH 64/83] Update mathmodule.c --- Modules/mathmodule.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index cdc600a2774d77..30de9f250f113e 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2064,7 +2064,6 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) } m = PyNumber_Multiply(f, b); Py_DECREF(f); - Py_DECREF(f); Py_DECREF(b); if (m == NULL) { return NULL; From 6caf90085972a5f003d99bfa87dfa7b1425503c5 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 8 Feb 2020 21:11:20 +0530 Subject: [PATCH 65/83] Update mathmodule.c --- Modules/mathmodule.c | 72 ++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 30de9f250f113e..6259bf7e2efd71 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2033,49 +2033,49 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) a = PyNumber_Index(a); if (a == NULL) { - return NULL; + return NULL; } b = PyNumber_Index(b); if (b == NULL) { return NULL; } - if (a == 0 && b == 0) { - PyErr_Format(PyExc_ZeroDivisionError,"lcm(0,0) is not possible"); - return 0; - } - if (PyFloat_Check(a) || PyFloat_Check(b)) { - PyErr_Format(PyExc_TypeError,"'float' object cannot be an argument to lcm"); - return 0; - } - g = _PyLong_GCD(a, b); - Py_DECREF(a); - Py_DECREF(b); - Py_DECREF(a); - Py_DECREF(b); - if (g == NULL) { - return NULL; - } - f = PyNumber_FloorDivide(a, g); - Py_DECREF(a); - Py_DECREF(g); - Py_DECREF(g); - if (f == NULL) { - return NULL; + if (a == 0) { + return 0; } - m = PyNumber_Multiply(f, b); - Py_DECREF(f); - Py_DECREF(b); - if (m == NULL) { - return NULL; - } - ab = PyNumber_Absolute(m); - Py_DECREF(m); - if (ab == NULL) { - return NULL; + else { + if (PyFloat_Check(a) || PyFloat_Check(b)) { + PyErr_Format(PyExc_TypeError,"'float' object cannot be an argument to lcm"); + return 0; + } + g = _PyLong_GCD(a, b); + Py_DECREF(a); + Py_DECREF(b); + Py_DECREF(b); + if (g == NULL) { + return NULL; + } + f = PyNumber_FloorDivide(a, g); + Py_DECREF(a); + Py_DECREF(g); + Py_DECREF(g); + if (f == NULL) { + 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); + if (ab == NULL) { + return NULL; + } + Py_DECREF(a); + Py_DECREF(b); + return ab; } - Py_DECREF(a); - Py_DECREF(b); - return ab; } From 911570be63a83dcf9eca231d9e37fbfd79e03e90 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 8 Feb 2020 21:12:13 +0530 Subject: [PATCH 66/83] Update test_math.py --- Lib/test/test_math.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 849fcd08304785..46dd58be28091a 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -976,6 +976,7 @@ def __index__(self): def test_lcm(self): lcm = math.lcm + self.assertEqual(lcm(0, 0), 0) self.assertEqual(lcm(1, 0), 0) self.assertEqual(lcm(-1, 0), 0) self.assertEqual(lcm(0, 1), 0) @@ -1006,7 +1007,6 @@ def test_lcm(self): self.assertEqual(lcm(-b, -a), d) self.assertRaises(TypeError, lcm, 120.0, 84) self.assertRaises(TypeError, lcm, 120, 84.0) - self.assertRaises(ZeroDivisionError, lcm, 0, 0) def testLdexp(self): self.assertRaises(TypeError, math.ldexp) From 24ba4c8d2d53364a9b633b669e4d07685a5496a0 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 8 Feb 2020 21:22:19 +0530 Subject: [PATCH 67/83] Update mathmodule.c --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 6259bf7e2efd71..6c31e6e947f33e 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2039,7 +2039,7 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) if (b == NULL) { return NULL; } - if (a == 0) { + if (a == 0 && b == 0) { return 0; } else { From b097bc6d20fb138e7d29264339bbd60c37f9a66b Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 8 Feb 2020 21:30:24 +0530 Subject: [PATCH 68/83] Update test_math.py --- Lib/test/test_math.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 46dd58be28091a..e532fa346871cd 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -976,7 +976,6 @@ def __index__(self): def test_lcm(self): lcm = math.lcm - self.assertEqual(lcm(0, 0), 0) self.assertEqual(lcm(1, 0), 0) self.assertEqual(lcm(-1, 0), 0) self.assertEqual(lcm(0, 1), 0) From ddf5a39c442ff12a522d53cd1a9432f77299fdec Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 18:11:49 +0530 Subject: [PATCH 69/83] Update mathmodule.c --- Modules/mathmodule.c | 58 +++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 33 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 6c31e6e947f33e..7fce1675526fa5 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2032,55 +2032,47 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) PyObject *g, *m, *f, *ab; a = PyNumber_Index(a); - if (a == NULL) { - return NULL; + if (a == NULL) { + return NULL; } b = PyNumber_Index(b); if (b == NULL) { + Py_DECREF(a); return NULL; } - if (a == 0 && b == 0) { - return 0; - } - else { - if (PyFloat_Check(a) || PyFloat_Check(b)) { - PyErr_Format(PyExc_TypeError,"'float' object cannot be an argument to lcm"); - return 0; - } - g = _PyLong_GCD(a, b); + g = _PyLong_GCD(a, b); + if (g == NULL) { Py_DECREF(a); Py_DECREF(b); + return NULL; + } + f = PyNumber_FloorDivide(a, g); + Py_DECREF(g); + if (f == NULL) { + Py_DECREF(a); Py_DECREF(b); - if (g == NULL) { - return NULL; - } - f = PyNumber_FloorDivide(a, g); + return NULL; + } + m = PyNumber_Multiply(f, b); + Py_DECREF(f); + if (m == NULL) { Py_DECREF(a); - Py_DECREF(g); - Py_DECREF(g); - if (f == NULL) { - 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); - if (ab == NULL) { - return NULL; - } + return NULL; + } + ab = PyNumber_Absolute(m); + Py_DECREF(m); + if (ab == NULL) { Py_DECREF(a); Py_DECREF(b); - return ab; + return NULL; } + Py_DECREF(a); + Py_DECREF(b); + return ab; } - - /*[clinic input] math.trunc From 37f52331848c616edcc5ad7b1bf669ef588f6ecc Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 18:26:50 +0530 Subject: [PATCH 70/83] Update mathmodule.c --- Modules/mathmodule.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 7fce1675526fa5..41199464b1847b 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2048,27 +2048,22 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) } f = PyNumber_FloorDivide(a, g); Py_DECREF(g); + Py_DECREF(a); if (f == NULL) { - Py_DECREF(a); Py_DECREF(b); return NULL; } m = PyNumber_Multiply(f, b); Py_DECREF(f); + Py_DECREF(b); if (m == NULL) { - Py_DECREF(a); - Py_DECREF(b); return NULL; } ab = PyNumber_Absolute(m); Py_DECREF(m); if (ab == NULL) { - Py_DECREF(a); - Py_DECREF(b); return NULL; } - Py_DECREF(a); - Py_DECREF(b); return ab; } From 3074e34fcb5f8524046c3e6e67b3a3b267127e82 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 18:34:08 +0530 Subject: [PATCH 71/83] Update test_math.py --- Lib/test/test_math.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index e532fa346871cd..02d17bff1a9de4 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -976,6 +976,7 @@ def __index__(self): def test_lcm(self): lcm = math.lcm + self.assertEqual(lcm(0, 0), 0) self.assertEqual(lcm(1, 0), 0) self.assertEqual(lcm(-1, 0), 0) self.assertEqual(lcm(0, 1), 0) @@ -1004,8 +1005,6 @@ def test_lcm(self): self.assertEqual(lcm(-b, a), d) self.assertEqual(lcm(-a, -b), d) self.assertEqual(lcm(-b, -a), d) - self.assertRaises(TypeError, lcm, 120.0, 84) - self.assertRaises(TypeError, lcm, 120, 84.0) def testLdexp(self): self.assertRaises(TypeError, math.ldexp) From 3bb50dcaf95d10891a75329e93c498ef4ca8ac55 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 18:55:40 +0530 Subject: [PATCH 72/83] Update mathmodule.c --- Modules/mathmodule.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 41199464b1847b..f8cdb6d1cccf29 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2061,9 +2061,6 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) } ab = PyNumber_Absolute(m); Py_DECREF(m); - if (ab == NULL) { - return NULL; - } return ab; } From e40c94cae8d1bbb9dada32c9033e39a9e2356997 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 19:26:50 +0530 Subject: [PATCH 73/83] Update mathmodule.c --- Modules/mathmodule.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index f8cdb6d1cccf29..ee23defab49d53 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2040,28 +2040,33 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) Py_DECREF(a); return NULL; } - g = _PyLong_GCD(a, b); - if (g == NULL) { - Py_DECREF(a); - Py_DECREF(b); - return NULL; + if (_PyLong_Sign(a) == 0 && _PyLong_Sign(b) == 0) { + return 0; } - f = PyNumber_FloorDivide(a, g); - Py_DECREF(g); - Py_DECREF(a); - if (f == NULL) { + else { + 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); - return NULL; + if (m == NULL) { + return NULL; + } + ab = PyNumber_Absolute(m); + Py_DECREF(m); + return ab; } - 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; } From 2e95203affa8002653e7e5a7923b4ed1ec130629 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 21:49:21 +0530 Subject: [PATCH 74/83] Update mathmodule.c --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index ee23defab49d53..a4e8c5aa9ce3c7 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2041,7 +2041,7 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) return NULL; } if (_PyLong_Sign(a) == 0 && _PyLong_Sign(b) == 0) { - return 0; + return PyLong_FromLong(0); } else { g = _PyLong_GCD(a, b); From afdecfa4fd00d56334baa8661dc3105414d40b27 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 22:03:35 +0530 Subject: [PATCH 75/83] Update mathmodule.c --- Modules/mathmodule.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index a4e8c5aa9ce3c7..1741852328211f 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2041,6 +2041,8 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) return NULL; } if (_PyLong_Sign(a) == 0 && _PyLong_Sign(b) == 0) { + Py_DECREF(a); + Py_DECREF(b); return PyLong_FromLong(0); } else { From aa118d9adafbca30eb5df73f65e27a601075ee2e Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 22:17:32 +0530 Subject: [PATCH 76/83] changed '&&' to '||' --- Modules/mathmodule.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index 1741852328211f..a0760119f3bf4b 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2040,7 +2040,7 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) Py_DECREF(a); return NULL; } - if (_PyLong_Sign(a) == 0 && _PyLong_Sign(b) == 0) { + if (_PyLong_Sign(a) == 0 || _PyLong_Sign(b) == 0) { Py_DECREF(a); Py_DECREF(b); return PyLong_FromLong(0); From 0c03453445d4cbb77cf9fd5624431c074cd73acb Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 22:41:50 +0530 Subject: [PATCH 77/83] changed gramatical errors --- Doc/library/math.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index 19709fe90e8ff0..f3935a68cf4905 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -138,9 +138,9 @@ Number-theoretic and representation functions .. function:: lcm(a, b) - Return the least common multiple of integers *a* and *b*. If both *a* or - *b* is nonzero, then the value of ``lcm(a, b)`` is the smallest positive - integer that is multiple of both *a* and *b*. ``lcm(0, 0)`` returns ``0``. + Return the least common multiple of integers *a* and *b*. The value of + ``lcm(a, b)`` is the smallest non negative integer that is a multiple of + both *a* and *b*. If either *a* or *b* is zero then ``lcm(a, b)`` is zero. .. versionadded:: 3.9 From 73fe2afd56f8fb240a085cea217bfdbbbb2b8d24 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 22:51:21 +0530 Subject: [PATCH 78/83] Update math.rst --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index f3935a68cf4905..d8ac35256d1b4b 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -139,7 +139,7 @@ Number-theoretic and representation functions .. function:: lcm(a, b) Return the least common multiple of integers *a* and *b*. The value of - ``lcm(a, b)`` is the smallest non negative integer that is a multiple of + ``lcm(a, b)`` is the smallest nonnegative integer that is a multiple of both *a* and *b*. If either *a* or *b* is zero then ``lcm(a, b)`` is zero. .. versionadded:: 3.9 From 20e696ff4e61c2809b55941737da8a0172cc8b6e Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 23:03:52 +0530 Subject: [PATCH 79/83] removed "else" statement. --- Modules/mathmodule.c | 40 +++++++++++++++++++--------------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c index a0760119f3bf4b..3bd53a00a25922 100644 --- a/Modules/mathmodule.c +++ b/Modules/mathmodule.c @@ -2045,30 +2045,28 @@ math_lcm_impl(PyObject *module, PyObject *a, PyObject *b) Py_DECREF(b); return PyLong_FromLong(0); } - else { - g = _PyLong_GCD(a, b); - if (g == NULL) { - Py_DECREF(a); - Py_DECREF(b); - return NULL; - } - f = PyNumber_FloorDivide(a, g); - Py_DECREF(g); + g = _PyLong_GCD(a, b); + if (g == NULL) { 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; + 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; } From 54d1be4c40ff6032e5535526ffb94d24dada5b0f Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 23:36:55 +0530 Subject: [PATCH 80/83] Update Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst Co-Authored-By: Serhiy Storchaka --- .../next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst b/Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst index cf888d71417f0a..7298ecec4a5201 100644 --- a/Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst +++ b/Misc/NEWS.d/next/Library/2020-02-04-13-47-16.bpo-39479.BN_mM9.rst @@ -1 +1 @@ -Add `math.lcm()` function: Least Common Multiple. \ No newline at end of file +Add func:`math.lcm` function: Least Common Multiple. From f4ec2512f3cfc447f87d9d4b40b522ed53757165 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sat, 15 Feb 2020 23:52:27 +0530 Subject: [PATCH 81/83] Update test_math.py --- Lib/test/test_math.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Lib/test/test_math.py b/Lib/test/test_math.py index 02d17bff1a9de4..58f58a924ca99d 100644 --- a/Lib/test/test_math.py +++ b/Lib/test/test_math.py @@ -1005,6 +1005,9 @@ def test_lcm(self): self.assertEqual(lcm(-b, a), d) self.assertEqual(lcm(-a, -b), d) self.assertEqual(lcm(-b, -a), d) + self.assertEqual(lcm(MyIndexable(120), MyIndexable(84)), 840) + self.assertRaises(TypeError, lcm, 120.0, 84) + self.assertRaises(TypeError, lcm, 120, 84.0) def testLdexp(self): self.assertRaises(TypeError, math.ldexp) From 134c4f4c358b416826e630165a8936e705606edc Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Sun, 16 Feb 2020 23:14:18 +0530 Subject: [PATCH 82/83] Update math.rst --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index d8ac35256d1b4b..af00b2adb10174 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -140,7 +140,7 @@ Number-theoretic and representation functions Return the least common multiple of integers *a* and *b*. The value of ``lcm(a, b)`` is the smallest nonnegative integer that is a multiple of - both *a* and *b*. If either *a* or *b* is zero then ``lcm(a, b)`` is zero. + nonzero *a* and *b*. If either *a* or *b* is zero then ``lcm(a, b)`` is zero. .. versionadded:: 3.9 From 2db3ca75e4203da7d876c7fa5e00fd59b9ad3034 Mon Sep 17 00:00:00 2001 From: ananthan-123 <60189117+ananthan-123@users.noreply.github.com> Date: Mon, 17 Feb 2020 08:05:41 +0530 Subject: [PATCH 83/83] Changed nonzero to both --- Doc/library/math.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/math.rst b/Doc/library/math.rst index af00b2adb10174..d8ac35256d1b4b 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -140,7 +140,7 @@ Number-theoretic and representation functions Return the least common multiple of integers *a* and *b*. The value of ``lcm(a, b)`` is the smallest nonnegative integer that is a multiple of - nonzero *a* and *b*. If either *a* or *b* is zero then ``lcm(a, b)`` is zero. + both *a* and *b*. If either *a* or *b* is zero then ``lcm(a, b)`` is zero. .. versionadded:: 3.9