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' @@ -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,6 @@ return NotImplemented return self.__cmp__(other) == 0 - def __ne__(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. @@ -881,6 +877,20 @@ """ return self.__str__(eng=True, context=context) + @property + def real(self): + """The real component of a Decimal is itself.""" + return +self + + @property + def imag(self): + """The imaginary component of a Decimal is 0.""" + return Decimal(0) + + def conjugate(self): + """The complex conjugate of a Decimal is itself.""" + return +self + def __neg__(self, context=None): """Returns a copy with the sign switched. @@ -1405,6 +1415,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 +1434,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""" @@ -3328,6 +3342,9 @@ return self # My components are also immutable return self.__class__(str(self)) +numbers.Real.register(Decimal) +numbers.Inexact.register(Decimal) + def _dec_from_triple(sign, coefficient, exponent, special=False): """Create a decimal instance directly, without any validation, normalization (e.g. removal of leading zeros) or argument @@ -3336,7 +3353,7 @@ This function is for *internal use only*. """ - self = object.__new__(Decimal) + self = super(Decimal, Decimal).__new__(Decimal) self._sign = sign self._int = coefficient self._exp = exponent 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,7 @@ import unittest import glob +import numbers import os, sys import pickle, copy from decimal import * @@ -829,7 +830,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 +1181,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): @@ -1191,6 +1206,17 @@ r = d.to_integral(ROUND_DOWN) self.assertEqual(Decimal(int(d)), r) + def assertTypedEquals(self, a, b): + self.assertEquals(type(a), type(b)) + self.assertEquals(a, b) + + def test_Complex(self): + """Check the implementation of numbers.Complex.""" + d = Decimal("-3.14") + self.assertTypedEquals(d, d.real) + self.assertTypedEquals(Decimal(0), d.imag) + self.assertTypedEquals(d, d.conjugate()) + class ContextAPItests(unittest.TestCase): def test_pickle(self):