Workaround Python's idiocy with float -> str -> float conversions

This commit is contained in:
James Vega 2009-01-05 16:08:50 +00:00
parent 0bc16641b9
commit 62897a9663
2 changed files with 11 additions and 1 deletions

View File

@ -1,5 +1,6 @@
###
# Copyright (c) 2002-2004, Jeremiah Fincher
# Copyright (c) 2008, James Vega
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -181,6 +182,11 @@ class Math(callbacks.Plugin):
x = complex(i)
if x == abs(x):
x = abs(x)
# Need to use string-formatting here instead of str() because
# use of str() on large numbers loses information:
# str(float(33333333333333)) => '3.33333333333e+13'
# float('3.33333333333e+13') => 33333333333300.0
return '%f' % x
return str(x)
text = self._mathRe.sub(handleMatch, text)
try:

View File

@ -1,5 +1,6 @@
###
# Copyright (c) 2002-2004, Jeremiah Fincher
# Copyright (c) 2008, James Vega
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
@ -126,6 +127,9 @@ class MathTestCase(PluginTestCase):
self.assertResponse('calc max(1,2)', '2')
self.assertResponse('calc min(1,2)', '1')
def testCalcStrFloat(self):
self.assertResponse('calc 3+33333333333333', '33333333333336')
def testICalc(self):
self.assertResponse('icalc 1^1', '0')
self.assertResponse('icalc 10**24', '1' + '0'*24)