From 29e05573dd8f69fe474433dac8873fab7aeda6f0 Mon Sep 17 00:00:00 2001 From: Reinier Heeres Date: Sun, 18 Nov 2007 13:57:40 +0000 Subject: Fixed large factorial issue --- (limited to 'mathlib.py') diff --git a/mathlib.py b/mathlib.py index 77ab2c2..6693bc6 100644 --- a/mathlib.py +++ b/mathlib.py @@ -88,9 +88,17 @@ class MathLib: return None def d(self, val): - s = '%.10e' % val - d = Decimal(s) - return d.normalize() + if isinstance(val, Decimal): + return val + elif type(val) is types.FloatType or type(val) is types.IntType: + s = '%.10e' % val + d = Decimal(s) + return d.normalize() + elif type(val) is types.StringType or type(val) is types.LongType: + d = Decimal(val) + return d.normalize() + else: + return None def parse_number(self, s): try: @@ -115,6 +123,8 @@ class MathLib: n = self.d(n) elif type(n) is types.FloatType: n = self.d(n) + elif type(n) is types.LongType: + n = self.d(n) elif not isinstance(n, Decimal): return _('Error: unsupported type') (sign, digits, exp) = n.as_tuple() @@ -171,6 +181,12 @@ class MathLib: return ret def is_int(self, n): + if type(n) is types.IntType: + return True + + if not isinstance(n, Decimal): + n = self.d(n) + (sign, d, e) = n.normalize().as_tuple() return e >= 0 @@ -244,10 +260,12 @@ class MathLib: if n == 0: return 1 - res = n + n = long(n) + res = long(n) while n > 2: res *= n - 1 n -= 1 + return res def sin(self, x): -- cgit v0.9.1