From 559e7f165ad03731e6bc2211c0e6d8d9c02fb549 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 23 Feb 2020 13:21:29 +0200 Subject: bpo-39648: Expand math.gcd() and math.lcm() to handle multiple arguments. (GH-18604) * bpo-39648: Expand math.gcd() and math.lcm() to handle multiple arguments. * Simplify fast path. * Difine lcm() without arguments returning 1. * Apply suggestions from code review Co-Authored-By: Mark Dickinson Co-authored-by: Mark Dickinson --- Doc/library/math.rst | 33 ++++++++++++++++++++------------- Doc/whatsnew/3.9.rst | 9 +++++++-- 2 files changed, 27 insertions(+), 15 deletions(-) (limited to 'Doc') diff --git a/Doc/library/math.rst b/Doc/library/math.rst index d8ac35256d..6ec1feee35 100644 --- a/Doc/library/math.rst +++ b/Doc/library/math.rst @@ -126,23 +126,19 @@ Number-theoretic and representation functions `_\. -.. 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) @@ -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 diff --git a/Doc/whatsnew/3.9.rst b/Doc/whatsnew/3.9.rst index 161675d32f..a0ae4eb9b8 100644 --- a/Doc/whatsnew/3.9.rst +++ b/Doc/whatsnew/3.9.rst @@ -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*. -- cgit v1.2.1