Index: Lib/decimal.py =================================================================== --- Lib/decimal.py (revision 59809) +++ Lib/decimal.py (working copy) @@ -135,6 +135,7 @@ ] import copy as _copy +import numbers # Rounding ROUND_DOWN = 'ROUND_DOWN' @@ -497,7 +498,7 @@ ##### Decimal class ####################################################### -class Decimal(object): +class Decimal(numbers.Real, numbers.Inexact): """Floating point class for decimal arithmetic.""" __slots__ = ('_exp','_int','_sign', '_is_special') @@ -527,7 +528,7 @@ # and the Decimal constructor still deal with tuples of # digits. - self = object.__new__(cls) + self = super(Decimal, cls).__new__(cls) # From a string # REs insist on real strings, so we can too. @@ -761,11 +762,16 @@ return NotImplemented return self.__cmp__(other) == 0 - def __ne__(self, other): + def __lt__(self, other): if not isinstance(other, (Decimal, int, long)): return NotImplemented - return self.__cmp__(other) != 0 + return self.__cmp__(other) < 0 + def __le__(self, other): + if not isinstance(other, (Decimal, int, long)): + return NotImplemented + return self.__cmp__(other) <= 0 + def compare(self, other, context=None): """Compares one to another. @@ -1405,6 +1411,10 @@ def __int__(self): """Converts self to an int, truncating if necessary.""" + return int(self.__trunc__()) + + def __trunc__(self): + """Truncates self to an int or long.""" if self._is_special: if self._isnan(): context = getcontext() @@ -1420,9 +1430,9 @@ def __long__(self): """Converts to a long. - Equivalent to long(int(self)) + Equivalent to long(trunc(self)) """ - return long(self.__int__()) + return long(self.__trunc__()) def _fix_nan(self, context): """Decapitate the payload of a NaN to fit the context""" Index: Lib/numbers.py =================================================================== --- Lib/numbers.py (revision 59809) +++ Lib/numbers.py (working copy) @@ -45,7 +45,6 @@ Inexact.register(complex) Inexact.register(float) -# Inexact.register(decimal.Decimal) class Complex(Number): @@ -257,7 +256,6 @@ return +self Real.register(float) -# Real.register(decimal.Decimal) class Rational(Real, Exact): Index: Lib/test/test_decimal.py =================================================================== --- Lib/test/test_decimal.py (revision 59809) +++ Lib/test/test_decimal.py (working copy) @@ -27,6 +27,8 @@ import unittest import glob +import math +import numbers import os, sys import pickle, copy from decimal import * @@ -829,7 +831,15 @@ self.assertEqual(-Decimal(45), Decimal(-45)) # - self.assertEqual(abs(Decimal(45)), abs(Decimal(-45))) # abs + def test_trunc(self): + self.assertEqual(int, type(trunc(Decimal("17.5")))) + self.assertEqual(17, trunc(Decimal("17"))) + self.assertEqual(17, trunc(Decimal("17.0001"))) + self.assertEqual(17, trunc(Decimal("17.999999"))) + self.assertEqual(-17, trunc(Decimal("-17.999999"))) + self.assertEqual(10**30, trunc(Decimal("1e30"))) + # The following are two functions used to test threading in the next class def thfunc1(cls): @@ -1172,7 +1182,13 @@ d = d1.max(d2) self.assertTrue(type(d) is Decimal) + def test_base_classes(self): + self.assertTrue(issubclass(Decimal, numbers.Real)) + self.assertTrue(issubclass(Decimal, numbers.Inexact)) + self.assertFalse(issubclass(Decimal, numbers.Rational)) + self.assertFalse(issubclass(Decimal, numbers.Exact)) + class DecimalPythonAPItests(unittest.TestCase): def test_pickle(self):