File size: 12,018 Bytes
a4edccc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | diff --git a/Doc/library/math.integer.rst b/Doc/library/math.integer.rst
new file mode 100644
index 00000000000..6a9fe74c5e8
--- /dev/null
+++ b/Doc/library/math.integer.rst
@@ -0,0 +1,85 @@
+:mod:`math.integer` --- integer-specific mathematics functions
+==============================================================
+
+.. module:: math.integer
+ :synopsis: Integer-specific mathematics functions.
+
+.. versionadded:: next
+
+--------------
+
+This module provides access to the mathematical functions defined for integer arguments.
+These functions accept integers and objects that implement the
+:meth:`~object.__index__` method which is used to convert the object to an integer
+number.
+
+The following functions are provided by this module. All return values are
+computed exactly and are integers.
+
+
+.. function:: comb(n, k, /)
+
+ Return the number of ways to choose *k* items from *n* items without repetition
+ and without order.
+
+ Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates
+ to zero when ``k > n``.
+
+ Also called the binomial coefficient because it is equivalent
+ to the coefficient of k-th term in polynomial expansion of
+ ``(1 + x)ⁿ``.
+
+ Raises :exc:`ValueError` if either of the arguments are negative.
+
+
+.. function:: factorial(n, /)
+
+ Return factorial of the nonnegative integer *n*.
+
+
+.. function:: gcd(*integers)
+
+ 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 of all arguments. If all arguments
+ are zero, then the returned value is ``0``. ``gcd()`` without arguments
+ returns ``0``.
+
+
+.. function:: isqrt(n, /)
+
+ Return the integer square root of the nonnegative integer *n*. This is the
+ floor of the exact square root of *n*, or equivalently the greatest integer
+ *a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*.
+
+ For some applications, it may be more convenient to have the least integer
+ *a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of
+ the exact square root of *n*. For positive *n*, this can be computed using
+ ``a = 1 + isqrt(n - 1)``.
+
+
+ .. |nbsp| unicode:: 0xA0
+ :trim:
+
+
+.. 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``.
+
+
+.. function:: perm(n, k=None, /)
+
+ Return the number of ways to choose *k* items from *n* items
+ without repetition and with order.
+
+ Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
+ to zero when ``k > n``.
+
+ If *k* is not specified or is ``None``, then *k* defaults to *n*
+ and the function returns ``n!``.
+
+ Raises :exc:`ValueError` if either of the arguments are negative.
diff --git a/Doc/library/math.rst b/Doc/library/math.rst
index 5eb82a10a8c..54c98346b27 100644
--- a/Doc/library/math.rst
+++ b/Doc/library/math.rst
@@ -27,15 +27,6 @@ noted otherwise, all return values are floats.
==================================================== ============================================
-**Number-theoretic functions**
---------------------------------------------------------------------------------------------------
-:func:`comb(n, k) <comb>` Number of ways to choose *k* items from *n* items without repetition and without order
-:func:`factorial(n) <factorial>` *n* factorial
-:func:`gcd(*integers) <gcd>` Greatest common divisor of the integer arguments
-:func:`isqrt(n) <isqrt>` Integer square root of a nonnegative integer *n*
-:func:`lcm(*integers) <lcm>` Least common multiple of the integer arguments
-:func:`perm(n, k) <perm>` Number of ways to choose *k* items from *n* items without repetition and with order
-
**Floating point arithmetic**
--------------------------------------------------------------------------------------------------
:func:`ceil(x) <ceil>` Ceiling of *x*, the smallest integer greater than or equal to *x*
@@ -126,92 +117,6 @@ noted otherwise, all return values are floats.
==================================================== ============================================
-Number-theoretic functions
---------------------------
-
-.. function:: comb(n, k)
-
- Return the number of ways to choose *k* items from *n* items without repetition
- and without order.
-
- Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates
- to zero when ``k > n``.
-
- Also called the binomial coefficient because it is equivalent
- to the coefficient of k-th term in polynomial expansion of
- ``(1 + x)ⁿ``.
-
- Raises :exc:`TypeError` if either of the arguments are not integers.
- Raises :exc:`ValueError` if either of the arguments are negative.
-
- .. versionadded:: 3.8
-
-
-.. function:: factorial(n)
-
- Return factorial of the nonnegative integer *n*.
-
- .. versionchanged:: 3.10
- Floats with integral values (like ``5.0``) are no longer accepted.
-
-
-.. function:: gcd(*integers)
-
- 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 of all arguments. If all arguments
- are zero, then the returned value is ``0``. ``gcd()`` without arguments
- returns ``0``.
-
- .. versionadded:: 3.5
-
- .. versionchanged:: 3.9
- Added support for an arbitrary number of arguments. Formerly, only two
- arguments were supported.
-
-
-.. function:: isqrt(n)
-
- Return the integer square root of the nonnegative integer *n*. This is the
- floor of the exact square root of *n*, or equivalently the greatest integer
- *a* such that *a*\ ² |nbsp| ≤ |nbsp| *n*.
-
- For some applications, it may be more convenient to have the least integer
- *a* such that *n* |nbsp| ≤ |nbsp| *a*\ ², or in other words the ceiling of
- the exact square root of *n*. For positive *n*, this can be computed using
- ``a = 1 + isqrt(n - 1)``.
-
- .. 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:: perm(n, k=None)
-
- Return the number of ways to choose *k* items from *n* items
- without repetition and with order.
-
- Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
- to zero when ``k > n``.
-
- If *k* is not specified or is ``None``, then *k* defaults to *n*
- and the function returns ``n!``.
-
- Raises :exc:`TypeError` if either of the arguments are not integers.
- Raises :exc:`ValueError` if either of the arguments are negative.
-
- .. versionadded:: 3.8
-
-
Floating point arithmetic
-------------------------
@@ -812,6 +717,75 @@ Special functions
.. versionadded:: 3.2
+Number-theoretic functions
+--------------------------
+
+For backward compatibility, the :mod:`math` module provides also aliases of
+the following functions from the :mod:`math.integer` module:
+
+.. list-table::
+
+ * - .. function:: comb(n, k)
+ :no-typesetting:
+
+ :func:`comb(n, k) <math.integer.comb>`
+ - Number of ways to choose *k* items from *n* items without repetition
+ and without order
+
+ * - .. function:: factorial(n)
+ :no-typesetting:
+
+ :func:`factorial(n) <math.integer.factorial>`
+ - *n* factorial
+
+ * - .. function:: gcd(*integers)
+ :no-typesetting:
+
+ :func:`gcd(*integers) <math.integer.gcd>`
+ - Greatest common divisor of the integer arguments
+
+ * - .. function:: isqrt(n)
+ :no-typesetting:
+
+ :func:`isqrt(n) <math.integer.isqrt>`
+ - Integer square root of a nonnegative integer *n*
+
+ * - .. function:: lcm(*integers)
+ :no-typesetting:
+
+ :func:`lcm(*integers) <math.integer.lcm>`
+ - Least common multiple of the integer arguments
+
+ * - .. function:: perm(n, k)
+ :no-typesetting:
+
+ :func:`perm(n, k) <math.integer.perm>`
+ - Number of ways to choose *k* items from *n* items without repetition
+ and with order
+
+.. versionadded:: 3.5
+ The :func:`gcd` function.
+
+.. versionadded:: 3.8
+ The :func:`comb`, :func:`perm` and :func:`isqrt` functions.
+
+.. versionadded:: 3.9
+ The :func:`lcm` function.
+
+.. versionchanged:: 3.9
+ Added support for an arbitrary number of arguments in the :func:`gcd`
+ function.
+ Formerly, only two arguments were supported.
+
+.. versionchanged:: 3.10
+ Floats with integral values (like ``5.0``) are no longer accepted in the
+ :func:`factorial` function.
+
+.. deprecated:: next
+ These aliases are :term:`soft deprecated` in favor of the
+ :mod:`math.integer` functions.
+
+
Constants
---------
@@ -894,5 +868,5 @@ Constants
Module :mod:`cmath`
Complex number versions of many of these functions.
-.. |nbsp| unicode:: 0xA0
- :trim:
+ Module :mod:`math.integer`
+ Integer-specific mathematics functions.
diff --git a/Doc/library/numeric.rst b/Doc/library/numeric.rst
index 7c76a479d73..a2109a29bfb 100644
--- a/Doc/library/numeric.rst
+++ b/Doc/library/numeric.rst
@@ -19,6 +19,7 @@ The following modules are documented in this chapter:
numbers.rst
math.rst
+ math.integer.rst
cmath.rst
decimal.rst
fractions.rst
diff --git a/Doc/whatsnew/3.15.rst b/Doc/whatsnew/3.15.rst
index 903645fb40b..1294415a220 100644
--- a/Doc/whatsnew/3.15.rst
+++ b/Doc/whatsnew/3.15.rst
@@ -311,7 +311,12 @@ Other language changes
New modules
===========
-* None yet.
+math.integer
+------------
+
+This module provides access to the mathematical functions for integer
+arguments (:pep:`791`).
+(Contributed by Serhiy Storchaka in :gh:`81313`.)
Improved modules
diff --git a/Misc/NEWS.d/next/Library/2019-06-02-13-56-16.gh-issue-81313.axawSH.rst b/Misc/NEWS.d/next/Library/2019-06-02-13-56-16.gh-issue-81313.axawSH.rst
new file mode 100644
index 00000000000..2291c938222
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-06-02-13-56-16.gh-issue-81313.axawSH.rst
@@ -0,0 +1 @@
+Add the :mod:`math.integer` module (:pep:`791`).
diff --git a/PCbuild/pythoncore.vcxproj b/PCbuild/pythoncore.vcxproj
index 5e101ee1d0e..a50ffb120bc 100644
--- a/PCbuild/pythoncore.vcxproj
+++ b/PCbuild/pythoncore.vcxproj
@@ -479,6 +479,7 @@ <?xml version="1.0" encoding="utf-8"?>
<ClCompile Include="..\Modules\hmacmodule.c" />
<ClCompile Include="..\Modules\itertoolsmodule.c" />
<ClCompile Include="..\Modules\main.c" />
+ <ClCompile Include="..\Modules\mathintegermodule.c" />
<ClCompile Include="..\Modules\mathmodule.c" />
<ClCompile Include="..\Modules\md5module.c" />
<ClCompile Include="..\Modules\mmapmodule.c" />
diff --git a/PCbuild/pythoncore.vcxproj.filters b/PCbuild/pythoncore.vcxproj.filters
index 3d20ea41cd2..17999690990 100644
--- a/PCbuild/pythoncore.vcxproj.filters
+++ b/PCbuild/pythoncore.vcxproj.filters
@@ -1070,6 +1070,9 @@ <?xml version="1.0" encoding="utf-8"?>
<ClCompile Include="..\Modules\main.c">
<Filter>Modules</Filter>
</ClCompile>
+ <ClCompile Include="..\Modules\mathintegermodule.c">
+ <Filter>Modules</Filter>
+ </ClCompile>
<ClCompile Include="..\Modules\mathmodule.c">
<Filter>Modules</Filter>
</ClCompile>
|