mirror of
https://github.com/python/cpython.git
synced 2025-07-29 14:15:07 +00:00
Issue #2138: Add math.factorial().
This commit is contained in:
parent
dd96db63f6
commit
ecbdd2e9b0
4 changed files with 71 additions and 0 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue