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
231aad38
Commit
231aad38
authored
Jun 17, 2019
by
Serhiy Storchaka
Committed by
GitHub
Jun 17, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-37315: Deprecate accepting floats in math.factorial(). (GH-14147)
parent
1ce2656f
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
28 additions
and
7 deletions
+28
-7
Doc/library/math.rst
Doc/library/math.rst
+3
-0
Doc/whatsnew/3.9.rst
Doc/whatsnew/3.9.rst
+5
-0
Lib/test/test_math.py
Lib/test/test_math.py
+12
-7
Misc/NEWS.d/next/Library/2019-06-17-11-59-52.bpo-37315.o1xFC0.rst
...S.d/next/Library/2019-06-17-11-59-52.bpo-37315.o1xFC0.rst
+2
-0
Modules/mathmodule.c
Modules/mathmodule.c
+6
-0
No files found.
Doc/library/math.rst
View file @
231aad38
...
...
@@ -71,6 +71,9 @@ Number-theoretic and representation functions
Return *x* factorial as an integer. Raises :exc:`ValueError` if *x* is not integral or
is negative.
.. deprecated:: 3.9
Accepting floats with integral values (like ``5.0``) is deprecated.
.. function:: floor(x)
...
...
Doc/whatsnew/3.9.rst
View file @
231aad38
...
...
@@ -109,6 +109,11 @@ Build and C API Changes
Deprecated
==========
* Currently :func:`math.factorial` accepts :class:`float` instances with
non-negative integer values (like ``5.0``). It raises a :exc:`ValueError`
for non-integral and negative floats. It is deprecated now. In future
Python versions it will raise a :exc:`TypeError` for all floats.
(Contributed by Serhiy Storchaka in :issue:`37315`.)
Removed
...
...
Lib/test/test_math.py
View file @
231aad38
...
...
@@ -501,21 +501,25 @@ class MathTests(unittest.TestCase):
def
testFactorial
(
self
):
self
.
assertEqual
(
math
.
factorial
(
0
),
1
)
self
.
assertEqual
(
math
.
factorial
(
0.0
),
1
)
total
=
1
for
i
in
range
(
1
,
1000
):
total
*=
i
self
.
assertEqual
(
math
.
factorial
(
i
),
total
)
self
.
assertEqual
(
math
.
factorial
(
float
(
i
)),
total
)
self
.
assertEqual
(
math
.
factorial
(
i
),
py_factorial
(
i
))
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
-
1
)
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
-
1.0
)
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
-
10
**
100
)
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
-
1e100
)
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
math
.
pi
)
def
testFactorialNonIntegers
(
self
):
self
.
assertRaises
(
TypeError
,
math
.
factorial
,
decimal
.
Decimal
(
5.2
))
with
self
.
assertWarns
(
DeprecationWarning
):
self
.
assertEqual
(
math
.
factorial
(
5.0
),
120
)
with
self
.
assertWarns
(
DeprecationWarning
):
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
5.2
)
with
self
.
assertWarns
(
DeprecationWarning
):
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
-
1.0
)
with
self
.
assertWarns
(
DeprecationWarning
):
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
-
1e100
)
self
.
assertRaises
(
TypeError
,
math
.
factorial
,
decimal
.
Decimal
(
'5'
))
self
.
assertRaises
(
TypeError
,
math
.
factorial
,
decimal
.
Decimal
(
'5.2'
))
self
.
assertRaises
(
TypeError
,
math
.
factorial
,
"5"
)
# Other implementations may place different upper bounds.
...
...
@@ -524,7 +528,8 @@ class MathTests(unittest.TestCase):
# Currently raises ValueError for inputs that are too large
# to fit into a C long.
self
.
assertRaises
(
OverflowError
,
math
.
factorial
,
10
**
100
)
self
.
assertRaises
(
OverflowError
,
math
.
factorial
,
1e100
)
with
self
.
assertWarns
(
DeprecationWarning
):
self
.
assertRaises
(
OverflowError
,
math
.
factorial
,
1e100
)
def
testFloor
(
self
):
self
.
assertRaises
(
TypeError
,
math
.
floor
)
...
...
Misc/NEWS.d/next/Library/2019-06-17-11-59-52.bpo-37315.o1xFC0.rst
0 → 100644
View file @
231aad38
Deprecated accepting floats with integral value (like ``5.0``) in
:func:`math.factorial`.
Modules/mathmodule.c
View file @
231aad38
...
...
@@ -1981,6 +1981,12 @@ math_factorial(PyObject *module, PyObject *arg)
PyObject
*
result
,
*
odd_part
,
*
pyint_form
;
if
(
PyFloat_Check
(
arg
))
{
if
(
PyErr_WarnEx
(
PyExc_DeprecationWarning
,
"Using factorial() with floats is deprecated"
,
1
)
<
0
)
{
return
NULL
;
}
PyObject
*
lx
;
double
dx
=
PyFloat_AS_DOUBLE
((
PyFloatObject
*
)
arg
);
if
(
!
(
Py_IS_FINITE
(
dx
)
&&
dx
==
floor
(
dx
)))
{
...
...
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