mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-27 05:09:23 +01:00
Math: Allow 'factorial()' in icalc.
Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
parent
e45b9abd23
commit
7593edb6fc
@ -103,7 +103,8 @@ class Math(callbacks.Plugin):
|
|||||||
_mathEnv['abs'] = abs
|
_mathEnv['abs'] = abs
|
||||||
_mathEnv['max'] = max
|
_mathEnv['max'] = max
|
||||||
_mathEnv['min'] = min
|
_mathEnv['min'] = min
|
||||||
_mathEnv.pop('factorial')
|
_mathSafeEnv = dict([(x,y) for x,y in _mathEnv.items()
|
||||||
|
if x not in ['factorial']])
|
||||||
_mathRe = re.compile(r'((?:(?<![A-Fa-f\d)])-)?'
|
_mathRe = re.compile(r'((?:(?<![A-Fa-f\d)])-)?'
|
||||||
r'(?:0x[A-Fa-f\d]+|'
|
r'(?:0x[A-Fa-f\d]+|'
|
||||||
r'0[0-7]+|'
|
r'0[0-7]+|'
|
||||||
@ -192,7 +193,7 @@ class Math(callbacks.Plugin):
|
|||||||
text = self._mathRe.sub(handleMatch, text)
|
text = self._mathRe.sub(handleMatch, text)
|
||||||
try:
|
try:
|
||||||
self.log.info('evaluating %q from %s', text, msg.prefix)
|
self.log.info('evaluating %q from %s', text, msg.prefix)
|
||||||
x = complex(eval(text, self._mathEnv, self._mathEnv))
|
x = complex(eval(text, self._mathSafeEnv, self._mathSafeEnv))
|
||||||
irc.reply(self._complexToString(x))
|
irc.reply(self._complexToString(x))
|
||||||
except OverflowError:
|
except OverflowError:
|
||||||
maxFloat = math.ldexp(0.9999999999999999, 1024)
|
maxFloat = math.ldexp(0.9999999999999999, 1024)
|
||||||
|
@ -110,6 +110,7 @@ class MathTestCase(PluginTestCase):
|
|||||||
self.assertNotError('calc (1600 * 1200) - 2*(1024*1280)')
|
self.assertNotError('calc (1600 * 1200) - 2*(1024*1280)')
|
||||||
self.assertNotError('calc 3-2*4')
|
self.assertNotError('calc 3-2*4')
|
||||||
self.assertNotError('calc (1600 * 1200)-2*(1024*1280)')
|
self.assertNotError('calc (1600 * 1200)-2*(1024*1280)')
|
||||||
|
self.assertError('calc factorial(99)')
|
||||||
|
|
||||||
def testCalcNoNameError(self):
|
def testCalcNoNameError(self):
|
||||||
self.assertNotRegexp('calc foobar(x)', 'NameError')
|
self.assertNotRegexp('calc foobar(x)', 'NameError')
|
||||||
@ -134,6 +135,7 @@ class MathTestCase(PluginTestCase):
|
|||||||
self.assertResponse('icalc 1^1', '0')
|
self.assertResponse('icalc 1^1', '0')
|
||||||
self.assertResponse('icalc 10**24', '1' + '0'*24)
|
self.assertResponse('icalc 10**24', '1' + '0'*24)
|
||||||
self.assertRegexp('icalc 49/6', '8.16')
|
self.assertRegexp('icalc 49/6', '8.16')
|
||||||
|
self.assertNotError('icalc factorial(99)')
|
||||||
|
|
||||||
def testRpn(self):
|
def testRpn(self):
|
||||||
self.assertResponse('rpn 5 2 +', '7')
|
self.assertResponse('rpn 5 2 +', '7')
|
||||||
|
Loading…
Reference in New Issue
Block a user