Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
ecbdd2e9
Kaydet (Commit)
ecbdd2e9
authored
Haz 09, 2008
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #2138: Add math.factorial().
üst
dd96db63
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
71 additions
and
0 deletions
+71
-0
math.rst
Doc/library/math.rst
+4
-0
test_math.py
Lib/test/test_math.py
+15
-0
NEWS
Misc/NEWS
+2
-0
mathmodule.c
Modules/mathmodule.c
+50
-0
No files found.
Doc/library/math.rst
Dosyayı görüntüle @
ecbdd2e9
...
...
@@ -42,6 +42,10 @@ Number-theoretic and representation functions:
Return the absolute value of *x*.
.. function:: factorial(x)
Return *x* factorial. Raises :exc:`ValueError` if *x* is not intergral or
is negative.
.. function:: floor(x)
...
...
Lib/test/test_math.py
Dosyayı görüntüle @
ecbdd2e9
...
...
@@ -6,6 +6,7 @@ import unittest
import
math
import
os
import
sys
import
random
eps
=
1E-05
NAN
=
float
(
'nan'
)
...
...
@@ -277,6 +278,20 @@ class MathTests(unittest.TestCase):
self
.
ftest
(
'fabs(0)'
,
math
.
fabs
(
0
),
0
)
self
.
ftest
(
'fabs(1)'
,
math
.
fabs
(
1
),
1
)
def
testFactorial
(
self
):
def
fact
(
n
):
result
=
1
for
i
in
range
(
1
,
int
(
n
)
+
1
):
result
*=
i
return
result
values
=
range
(
10
)
+
[
50
,
100
,
500
]
random
.
shuffle
(
values
)
for
x
in
range
(
10
):
for
cast
in
(
int
,
long
,
float
):
self
.
assertEqual
(
math
.
factorial
(
cast
(
x
)),
fact
(
x
),
(
x
,
fact
(
x
),
math
.
factorial
(
x
)))
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
-
1
)
self
.
assertRaises
(
ValueError
,
math
.
factorial
,
math
.
pi
)
def
testFloor
(
self
):
self
.
assertRaises
(
TypeError
,
math
.
floor
)
# These types will be int in py3k.
...
...
Misc/NEWS
Dosyayı görüntüle @
ecbdd2e9
...
...
@@ -40,6 +40,8 @@ Core and Builtins
Extension Modules
-----------------
- Issue #2138: Add factorial() the math module.
- The heapq module does comparisons using LT instead of LE. This
makes its implementation match that used by list.sort().
...
...
Modules/mathmodule.c
Dosyayı görüntüle @
ecbdd2e9
...
...
@@ -512,6 +512,55 @@ PyDoc_STRVAR(math_sum_doc,
Return an accurate floating point sum of values in the iterable.
\n
\
Assumes IEEE-754 floating point arithmetic."
);
static
PyObject
*
math_factorial
(
PyObject
*
self
,
PyObject
*
arg
)
{
long
i
,
x
;
PyObject
*
result
,
*
iobj
,
*
newresult
;
if
(
PyFloat_Check
(
arg
))
{
double
dx
=
PyFloat_AS_DOUBLE
((
PyFloatObject
*
)
arg
);
if
(
dx
!=
floor
(
dx
))
{
PyErr_SetString
(
PyExc_ValueError
,
"factorial() only accepts integral values"
);
return
NULL
;
}
}
x
=
PyInt_AsLong
(
arg
);
if
(
x
==
-
1
&&
PyErr_Occurred
())
return
NULL
;
if
(
x
<
0
)
{
PyErr_SetString
(
PyExc_ValueError
,
"factorial() not defined for negative values"
);
return
NULL
;
}
result
=
(
PyObject
*
)
PyInt_FromLong
(
1
);
if
(
result
==
NULL
)
return
NULL
;
for
(
i
=
1
;
i
<=
x
;
i
++
)
{
iobj
=
(
PyObject
*
)
PyInt_FromLong
(
i
);
if
(
iobj
==
NULL
)
goto
error
;
newresult
=
PyNumber_Multiply
(
result
,
iobj
);
Py_DECREF
(
iobj
);
if
(
newresult
==
NULL
)
goto
error
;
Py_DECREF
(
result
);
result
=
newresult
;
}
return
result
;
error:
Py_DECREF
(
result
);
Py_XDECREF
(
iobj
);
return
NULL
;
}
PyDoc_STRVAR
(
math_factorial_doc
,
"Return n!"
);
static
PyObject
*
math_trunc
(
PyObject
*
self
,
PyObject
*
number
)
{
...
...
@@ -949,6 +998,7 @@ static PyMethodDef math_methods[] = {
{
"degrees"
,
math_degrees
,
METH_O
,
math_degrees_doc
},
{
"exp"
,
math_exp
,
METH_O
,
math_exp_doc
},
{
"fabs"
,
math_fabs
,
METH_O
,
math_fabs_doc
},
{
"factorial"
,
math_factorial
,
METH_O
,
math_factorial_doc
},
{
"floor"
,
math_floor
,
METH_O
,
math_floor_doc
},
{
"fmod"
,
math_fmod
,
METH_VARARGS
,
math_fmod_doc
},
{
"frexp"
,
math_frexp
,
METH_O
,
math_frexp_doc
},
...
...
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