Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
963eb0f4
Commit
963eb0f4
authored
Jun 04, 2019
by
Raymond Hettinger
Committed by
GitHub
Jun 04, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-35431: Drop the k <= n requirement (GH-13798)
parent
0fd2c300
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
49 additions
and
38 deletions
+49
-38
Doc/library/math.rst
Doc/library/math.rst
+12
-8
Lib/test/test_math.py
Lib/test/test_math.py
+6
-6
Modules/clinic/mathmodule.c.h
Modules/clinic/mathmodule.c.h
+13
-9
Modules/mathmodule.c
Modules/mathmodule.c
+18
-15
No files found.
Doc/library/math.rst
View file @
963eb0f4
...
...
@@ -41,12 +41,15 @@ Number-theoretic and representation functions
Return the number of ways to choose *k* items from *n* items without repetition
and without order.
Also called the binomial coefficient. It is mathematically equal to the expression
``n! / (k! (n - k)!)``. It is equivalent to the coefficient of the *k*-th term in the
polynomial expansion of the expression ``(1 + x) ** n``.
Evaluates to ``n! / (k! * (n - k)!)`` when ``k <= n`` and evaluates
to zero when ``k > n``.
Raises :exc:`TypeError` if the arguments not integers.
Raises :exc:`ValueError` if the arguments are negative or if *k* > *n*.
Also called the binomial coefficient because it is equivalent
to the coefficient of k-th term in polynomial expansion of the
expression ``(1 + x) ** n``.
Raises :exc:`TypeError` if either of the arguments not integers.
Raises :exc:`ValueError` if either of the arguments are negative.
.. versionadded:: 3.8
...
...
@@ -212,10 +215,11 @@ Number-theoretic and representation functions
Return the number of ways to choose *k* items from *n* items
without repetition and with order.
It is mathematically equal to the expression ``n! / (n - k)!``.
Evaluates to ``n! / (n - k)!`` when ``k <= n`` and evaluates
to zero when ``k > n``.
Raises :exc:`TypeError` if the arguments not integers.
Raises :exc:`ValueError` if
the arguments are negative or if *k* > *n*
.
Raises :exc:`TypeError` if
either of
the arguments not integers.
Raises :exc:`ValueError` if
either of the arguments are negative
.
.. versionadded:: 3.8
...
...
Lib/test/test_math.py
View file @
963eb0f4
...
...
@@ -1904,9 +1904,9 @@ class IsCloseTests(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
perm
,
1
,
-
1
)
self
.
assertRaises
(
ValueError
,
perm
,
1
,
-
2
**
1000
)
# R
aises value error
if k is greater than n
self
.
assert
Raises
(
ValueError
,
perm
,
1
,
2
)
self
.
assert
Raises
(
ValueError
,
perm
,
1
,
2
**
100
0
)
# R
eturns zero
if k is greater than n
self
.
assert
Equal
(
perm
(
1
,
2
),
0
)
self
.
assert
Equal
(
perm
(
1
,
2
**
1000
),
0
)
n
=
2
**
1000
self
.
assertEqual
(
perm
(
n
,
0
),
1
)
...
...
@@ -1970,9 +1970,9 @@ class IsCloseTests(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
comb
,
1
,
-
1
)
self
.
assertRaises
(
ValueError
,
comb
,
1
,
-
2
**
1000
)
# R
aises value error
if k is greater than n
self
.
assert
Raises
(
ValueError
,
comb
,
1
,
2
)
self
.
assert
Raises
(
ValueError
,
comb
,
1
,
2
**
100
0
)
# R
eturns zero
if k is greater than n
self
.
assert
Equal
(
comb
(
1
,
2
),
0
)
self
.
assert
Equal
(
comb
(
1
,
2
**
1000
),
0
)
n
=
2
**
1000
self
.
assertEqual
(
comb
(
n
,
0
),
1
)
...
...
Modules/clinic/mathmodule.c.h
View file @
963eb0f4
...
...
@@ -644,10 +644,11 @@ PyDoc_STRVAR(math_perm__doc__,
"
\n
"
"Number of ways to choose k items from n items without repetition and with order.
\n
"
"
\n
"
"It is mathematically equal to the expression n! / (n - k)!.
\n
"
"Evaluates to n! / (n - k)! when k <= n and evaluates
\n
"
"to zero when k > n.
\n
"
"
\n
"
"Raises TypeError if the arguments are not integers.
\n
"
"Raises ValueError if
the arguments are negative or if k > n
."
);
"Raises TypeError if
either of
the arguments are not integers.
\n
"
"Raises ValueError if
either of the arguments are negative
."
);
#define MATH_PERM_METHODDEF \
{"perm", (PyCFunction)(void(*)(void))math_perm, METH_FASTCALL, math_perm__doc__},
...
...
@@ -679,12 +680,15 @@ PyDoc_STRVAR(math_comb__doc__,
"
\n
"
"Number of ways to choose k items from n items without repetition and without order.
\n
"
"
\n
"
"Also called the binomial coefficient. It is mathematically equal to the expression
\n
"
"n! / (k! * (n - k)!). It is equivalent to the coefficient of k-th term in
\n
"
"polynomial expansion of the expression (1 + x)**n.
\n
"
"Evaluates to n! / (k! * (n - k)!) when k <= n and evaluates
\n
"
"to zero when k > n.
\n
"
"
\n
"
"Raises TypeError if the arguments are not integers.
\n
"
"Raises ValueError if the arguments are negative or if k > n."
);
"Also called the binomial coefficient because it is equivalent
\n
"
"to the coefficient of k-th term in polynomial expansion of the
\n
"
"expression (1 + x)**n.
\n
"
"
\n
"
"Raises TypeError if either of the arguments are not integers.
\n
"
"Raises ValueError if either of the arguments are negative."
);
#define MATH_COMB_METHODDEF \
{"comb", (PyCFunction)(void(*)(void))math_comb, METH_FASTCALL, math_comb__doc__},
...
...
@@ -709,4 +713,4 @@ math_comb(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
exit:
return
return_value
;
}
/*[clinic end generated code: output=
a82b0e705b6d0ec0
input=a9049054013a1b77]*/
/*[clinic end generated code: output=
5004266613284dcc
input=a9049054013a1b77]*/
Modules/mathmodule.c
View file @
963eb0f4
...
...
@@ -3007,15 +3007,16 @@ math.perm
Number of ways to choose k items from n items without repetition and with order.
It is mathematically equal to the expression n! / (n - k)!.
Evaluates to n! / (n - k)! when k <= n and evaluates
to zero when k > n.
Raises TypeError if the arguments are not integers.
Raises ValueError if
the arguments are negative or if k > n
.
Raises TypeError if
either of
the arguments are not integers.
Raises ValueError if
either of the arguments are negative
.
[clinic start generated code]*/
static
PyObject
*
math_perm_impl
(
PyObject
*
module
,
PyObject
*
n
,
PyObject
*
k
)
/*[clinic end generated code: output=e021a25469653e23 input=
f71ee4f6ff26be24
]*/
/*[clinic end generated code: output=e021a25469653e23 input=
b2e7729d9a1949cf
]*/
{
PyObject
*
result
=
NULL
,
*
factor
=
NULL
;
int
overflow
,
cmp
;
...
...
@@ -3052,8 +3053,8 @@ math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
cmp
=
PyObject_RichCompareBool
(
n
,
k
,
Py_LT
);
if
(
cmp
!=
0
)
{
if
(
cmp
>
0
)
{
PyErr_SetString
(
PyExc_ValueError
,
"k must be an integer less than or equal to n"
)
;
result
=
PyLong_FromLong
(
0
);
goto
done
;
}
goto
error
;
}
...
...
@@ -3121,18 +3122,21 @@ math.comb
Number of ways to choose k items from n items without repetition and without order.
Also called the binomial coefficient. It is mathematically equal to the expression
n! / (k! * (n - k)!). It is equivalent to the coefficient of k-th term in
polynomial expansion of the expression (1 + x)**n.
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 the
expression (1 + x)**n.
Raises TypeError if the arguments are not integers.
Raises ValueError if
the arguments are negative or if k > n
.
Raises TypeError if
either of
the arguments are not integers.
Raises ValueError if
either of the arguments are negative
.
[clinic start generated code]*/
static
PyObject
*
math_comb_impl
(
PyObject
*
module
,
PyObject
*
n
,
PyObject
*
k
)
/*[clinic end generated code: output=bd2cec8d854f3493 input=
2f336ac9ec8242f
9]*/
/*[clinic end generated code: output=bd2cec8d854f3493 input=
9a05315af251870
9]*/
{
PyObject
*
result
=
NULL
,
*
factor
=
NULL
,
*
temp
;
int
overflow
,
cmp
;
...
...
@@ -3173,9 +3177,8 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
}
if
(
Py_SIZE
(
temp
)
<
0
)
{
Py_DECREF
(
temp
);
PyErr_SetString
(
PyExc_ValueError
,
"k must be an integer less than or equal to n"
);
goto
error
;
result
=
PyLong_FromLong
(
0
);
goto
done
;
}
cmp
=
PyObject_RichCompareBool
(
temp
,
k
,
Py_LT
);
if
(
cmp
>
0
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment