Fixed the sqrt bug submitted by kmj.

This commit is contained in:
Jeremy Fincher 2004-01-24 18:57:01 +00:00
parent f3b49e4e88
commit 78eafe3fa6
2 changed files with 10 additions and 1 deletions

View File

@ -90,6 +90,12 @@ class Math(callbacks.Privmsg):
_mathEnv = {'__builtins__': types.ModuleType('__builtins__'), 'i': 1j}
_mathEnv.update(math.__dict__)
_mathEnv.update(cmath.__dict__)
def _sqrt(x):
if isinstance(x, complex) or x < 0:
return cmath.sqrt(x)
else:
return math.sqrt(x)
_mathEnv['sqrt'] = _sqrt
_mathRe = re.compile(r'((?:(?<![A-Fa-f\d])-)?'
r'(?:0x[A-Fa-f\d]+|'
r'0[0-7]+|'
@ -178,7 +184,7 @@ class Math(callbacks.Privmsg):
except NameError, e:
irc.error('%s is not a defined function.' % str(e).split()[1])
except Exception, e:
irc.error(utils.exnToString(e))
irc.error(str(e))
def icalc(self, irc, msg, args):
"""<math expression>

View File

@ -56,6 +56,9 @@ class MathTestCase(PluginTestCase, PluginDocumentation):
def testCalcImaginary(self):
self.assertResponse('calc 3 + sqrt(-1)', '3+i')
def testCalcFloorWorksWithSqrt(self):
self.assertNotError('calc floor(sqrt(5))')
def testRpn(self):
self.assertResponse('rpn 5 2 +', '7')