Math.calc: allow factorial for smallish numbers

This commit is contained in:
jacksonmj 2015-08-25 00:46:44 +00:00
parent 1aeb847509
commit 97c7d0a9c3
2 changed files with 9 additions and 4 deletions

View File

@ -110,13 +110,18 @@ class Math(callbacks.Plugin):
return math.sqrt(x)
def _cbrt(x):
return math.pow(x, 1.0/3)
def _factorial(x):
if x<=10000:
return math.factorial(x)
else:
raise Exception('factorial argument too large')
_mathEnv['sqrt'] = _sqrt
_mathEnv['cbrt'] = _cbrt
_mathEnv['abs'] = abs
_mathEnv['max'] = max
_mathEnv['min'] = min
_mathSafeEnv = dict([(x,y) for x,y in _mathEnv.items()
if x not in ['factorial']])
_mathSafeEnv = dict([(x,y) for x,y in _mathEnv.items()])
_mathSafeEnv['factorial'] = _factorial
_mathRe = re.compile(r'((?:(?<![A-Fa-f\d)])-)?'
r'(?:0x[A-Fa-f\d]+|'
r'0[0-7]+|'

View File

@ -114,7 +114,7 @@ class MathTestCase(PluginTestCase):
self.assertNotError('calc (1600 * 1200) - 2*(1024*1280)')
self.assertNotError('calc 3-2*4')
self.assertNotError('calc (1600 * 1200)-2*(1024*1280)')
self.assertError('calc factorial(99)')
self.assertError('calc factorial(20000)')
def testCalcNoNameError(self):
self.assertNotRegexp('calc foobar(x)', 'NameError')
@ -139,7 +139,7 @@ class MathTestCase(PluginTestCase):
self.assertResponse('icalc 1^1', '0')
self.assertResponse('icalc 10**24', '1' + '0'*24)
self.assertRegexp('icalc 49/6', '8.16')
self.assertNotError('icalc factorial(99)')
self.assertNotError('icalc factorial(20000)')
def testRpn(self):
self.assertResponse('rpn 5 2 +', '7')