Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions Doc/library/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,19 @@ Number-theoretic and representation functions
<https://code.activestate.com/recipes/393090/>`_\.


.. function:: gcd(a, b)
.. function:: gcd(*integers)

Return the greatest common divisor of the integers *a* and *b*. If either
*a* or *b* is nonzero, then the value of ``gcd(a, b)`` is the largest
positive integer that divides both *a* and *b*. ``gcd(0, 0)`` returns
``0``.
Return the greatest common divisor of the specified integer arguments.
If any of the arguments is nonzero, then the returned value is the largest
positive integer that is a divisor af all arguments. If all arguments
are zero, then the returned value is ``0``. ``gcd()`` without arguments
returns ``0``.

.. versionadded:: 3.5


.. function:: lcm(a, b)

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.

.. versionadded:: 3.9
.. versionchanged:: 3.9
Added support for an arbitrary number of arguments. Formerly, only two
arguments were supported.


.. function:: isclose(a, b, *, rel_tol=1e-09, abs_tol=0.0)
Expand Down Expand Up @@ -210,6 +206,17 @@ Number-theoretic and representation functions
.. versionadded:: 3.8


.. function:: lcm(*integers)

Return the least common multiple of the specified integer arguments.
If all arguments are nonzero, then the returned value is the smallest
positive integer that is a multiple of all arguments. If any of the arguments
is zero, then the returned value is ``0``. ``lcm()`` without arguments
returns ``1``.

.. versionadded:: 3.9


.. function:: ldexp(x, i)

Return ``x * (2**i)``. This is essentially the inverse of function
Expand Down
9 changes: 7 additions & 2 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,13 @@ import attempts.
math
----

Add :func:`math.lcm`: return the least common multiple of *a* and *b*.
(Contributed by Ananthakrishnan in :issue:`39479`.)
Expanded the :func:`math.gcd` function to handle multiple arguments.
Formerly, it only supported two arguments.
(Contributed by Serhiy Storchaka in :issue:`39648`.)

Add :func:`math.lcm`: return the least common multiple of specified arguments.
(Contributed by Mark Dickinson, Ananthakrishnan and Serhiy Storchaka in
:issue:`39479` and :issue:`39648`.)

Add :func:`math.nextafter`: return the next floating-point value after *x*
towards *y*.
Expand Down
58 changes: 33 additions & 25 deletions Lib/test/test_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,33 +705,32 @@ def testGcd(self):
self.assertEqual(gcd(84, -120), 12)
self.assertEqual(gcd(1216342683557601535506311712,
436522681849110124616458784), 32)
c = 652560

x = 434610456570399902378880679233098819019853229470286994367836600566
y = 1064502245825115327754847244914921553977
a = x * c
b = y * c
self.assertEqual(gcd(a, b), c)
self.assertEqual(gcd(b, a), c)
self.assertEqual(gcd(-a, b), c)
self.assertEqual(gcd(b, -a), c)
self.assertEqual(gcd(a, -b), c)
self.assertEqual(gcd(-b, a), c)
self.assertEqual(gcd(-a, -b), c)
self.assertEqual(gcd(-b, -a), c)
c = 576559230871654959816130551884856912003141446781646602790216406874
a = x * c
b = y * c
self.assertEqual(gcd(a, b), c)
self.assertEqual(gcd(b, a), c)
self.assertEqual(gcd(-a, b), c)
self.assertEqual(gcd(b, -a), c)
self.assertEqual(gcd(a, -b), c)
self.assertEqual(gcd(-b, a), c)
self.assertEqual(gcd(-a, -b), c)
self.assertEqual(gcd(-b, -a), c)

for c in (652560,
576559230871654959816130551884856912003141446781646602790216406874):
a = x * c
b = y * c
self.assertEqual(gcd(a, b), c)
self.assertEqual(gcd(b, a), c)
self.assertEqual(gcd(-a, b), c)
self.assertEqual(gcd(b, -a), c)
self.assertEqual(gcd(a, -b), c)
self.assertEqual(gcd(-b, a), c)
self.assertEqual(gcd(-a, -b), c)
self.assertEqual(gcd(-b, -a), c)

self.assertEqual(gcd(), 0)
self.assertEqual(gcd(120), 120)
self.assertEqual(gcd(-120), 120)
self.assertEqual(gcd(120, 84, 102), 6)
self.assertEqual(gcd(120, 1, 84), 1)

self.assertRaises(TypeError, gcd, 120.0)
self.assertRaises(TypeError, gcd, 120.0, 84)
self.assertRaises(TypeError, gcd, 120, 84.0)
self.assertRaises(TypeError, gcd, 120, 1, 84.0)
self.assertEqual(gcd(MyIndexable(120), MyIndexable(84)), 12)

def testHypot(self):
Expand Down Expand Up @@ -989,9 +988,9 @@ def test_lcm(self):
self.assertEqual(lcm(1216342683557601535506311712,
436522681849110124616458784),
16592536571065866494401400422922201534178938447014944)

x = 43461045657039990237
y = 10645022458251153277

for c in (652560,
57655923087165495981):
a = x * c
Expand All @@ -1005,9 +1004,18 @@ 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.assertEqual(lcm(), 1)
self.assertEqual(lcm(120), 120)
self.assertEqual(lcm(-120), 120)
self.assertEqual(lcm(120, 84, 102), 14280)
self.assertEqual(lcm(120, 0, 84), 0)

self.assertRaises(TypeError, lcm, 120.0)
self.assertRaises(TypeError, lcm, 120.0, 84)
self.assertRaises(TypeError, lcm, 120, 84.0)
self.assertRaises(TypeError, lcm, 120, 0, 84.0)
self.assertEqual(lcm(MyIndexable(120), MyIndexable(84)), 840)

def testLdexp(self):
self.assertRaises(TypeError, math.ldexp)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Expanded :func:`math.gcd` and :func:`math.lcm` to handle multiple arguments.
62 changes: 1 addition & 61 deletions Modules/clinic/mathmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading