cpython: f3b09c269af0 (original) (raw)

Mercurial > cpython

changeset 99700:f3b09c269af0

Issue #25928: Add Decimal.as_integer_ratio(). Python parts and docs by Mark Dickinson. [#25928]

Stefan Krah skrah@bytereef.org
date Mon, 28 Dec 2015 23:02:02 +0100
parents 1472c08d9c23
children 7f44e0ff6d72
files Doc/library/decimal.rst Lib/_pydecimal.py Lib/test/test_decimal.py Misc/NEWS Modules/_decimal/_decimal.c Modules/_decimal/docstrings.h Modules/_decimal/tests/deccheck.py
diffstat 7 files changed, 213 insertions(+), 3 deletions(-)[+] [-] Doc/library/decimal.rst 13 Lib/_pydecimal.py 52 Lib/test/test_decimal.py 33 Misc/NEWS 2 Modules/_decimal/_decimal.c 101 Modules/_decimal/docstrings.h 9 Modules/_decimal/tests/deccheck.py 6

line wrap: on

line diff

--- a/Doc/library/decimal.rst +++ b/Doc/library/decimal.rst @@ -448,6 +448,19 @@ Decimal objects Decimal('321e+5').adjusted() returns seven. Used for determining the position of the most significant digit with respect to the decimal point.

+

+

+

--- a/Lib/_pydecimal.py +++ b/Lib/_pydecimal.py @@ -1010,6 +1010,58 @@ class Decimal(object): """ return DecimalTuple(self._sign, tuple(map(int, self._int)), self._exp)

+

+

+

+

+

+

+

+

+ def repr(self): """Represents the number as an instance of Decimal.""" # Invariant: eval(repr(d)) == d

--- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -2047,6 +2047,39 @@ class UsabilityTest(unittest.TestCase): d = Decimal( (1, (0, 2, 7, 1), 'F') ) self.assertEqual(d.as_tuple(), (1, (0,), 'F'))

+

+

+

+

+

+ def test_subclassing(self): # Different behaviours when subclassing Decimal Decimal = self.decimal.Decimal

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -123,6 +123,8 @@ Core and Builtins Library ------- +- Issue #25928: Add Decimal.as_integer_ratio(). +

--- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -3380,6 +3380,106 @@ dec_as_long(PyObject *dec, PyObject *con return (PyObject ) pylong; } +/ Convert a Decimal to its exact integer ratio representation. */ +static PyObject * +dec_as_integer_ratio(PyObject *self, PyObject *args UNUSED) +{

+

+

+

+

+

+

+

+

+

+

+

+ + +error:

+} + static PyObject * PyDec_ToIntegralValue(PyObject *dec, PyObject *args, PyObject kwds) { @@ -4688,6 +4788,7 @@ static PyMethodDef dec_methods [] = / Miscellaneous */ { "from_float", dec_from_float, METH_O|METH_CLASS, doc_from_float }, { "as_tuple", PyDec_AsTuple, METH_NOARGS, doc_as_tuple },

--- a/Modules/_decimal/docstrings.h +++ b/Modules/_decimal/docstrings.h @@ -70,6 +70,15 @@ PyDoc_STRVAR(doc_as_tuple, Return a tuple representation of the number.\n[](#l6.4) \n"); +PyDoc_STRVAR(doc_as_integer_ratio, +"as_integer_ratio($self, /)\n--\n\n[](#l6.8) +Decimal.as_integer_ratio() -> (int, int)\n[](#l6.9) +\n[](#l6.10) +Return a pair of integers, whose ratio is exactly equal to the original\n[](#l6.11) +Decimal and with a positive denominator. The ratio is in lowest terms.\n[](#l6.12) +Raise OverflowError on infinities and a ValueError on NaNs.\n[](#l6.13) +\n"); + PyDoc_STRVAR(doc_canonical, "canonical($self, /)\n--\n\n[](#l6.17) Return the canonical encoding of the argument. Currently, the encoding\n[](#l6.18)

--- a/Modules/_decimal/tests/deccheck.py +++ b/Modules/_decimal/tests/deccheck.py @@ -50,8 +50,8 @@ Functions = { 'abs', 'bool', 'ceil', 'complex', 'copy', 'floor', 'float', 'hash', 'int', 'neg', 'pos', 'reduce', 'repr', 'str', 'trunc',

Functions that require a restricted exponent range for reasonable runtimes.

UnaryRestricted = [ 'ceil', 'floor', 'int', 'trunc',