Skip to content

Commit 9679859

Browse files
committed
Issue 8257: Decimal constructor to accept float argument.
1 parent 63b4355 commit 9679859

3 files changed

Lines changed: 41 additions & 28 deletions

File tree

Doc/library/decimal.rst

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ Decimal objects
308308

309309
Construct a new :class:`Decimal` object based from *value*.
310310

311-
*value* can be an integer, string, tuple, or another :class:`Decimal`
311+
*value* can be an integer, string, tuple, :class:`float`, or another :class:`Decimal`
312312
object. If no *value* is given, returns ``Decimal('0')``. If *value* is a
313313
string, it should conform to the decimal numeric string syntax after leading
314314
and trailing whitespace characters are removed::
@@ -334,6 +334,11 @@ Decimal objects
334334
digits, and an integer exponent. For example, ``Decimal((0, (1, 4, 1, 4), -3))``
335335
returns ``Decimal('1.414')``.
336336

337+
If *value* is a :class:`float`, the binary floating point value is losslessly
338+
converted to its exact decimal equivalent. This conversion can often require
339+
upto 53 digits of precision. For example, ``Decimal(float('1.1'))`` converts
340+
to ``Decimal('1.100000000000000088817841970012523233890533447265625')``.
341+
337342
The *context* precision does not affect how many digits are stored. That is
338343
determined exclusively by the number of digits in *value*. For example,
339344
``Decimal('3.00000')`` records all five zeros even if the context precision is
@@ -1824,36 +1829,14 @@ value unchanged:
18241829
Q. Is there a way to convert a regular float to a :class:`Decimal`?
18251830

18261831
A. Yes, all binary floating point numbers can be exactly expressed as a
1827-
Decimal. An exact conversion may take more precision than intuition would
1828-
suggest, so we trap :const:`Inexact` to signal a need for more precision:
1829-
1830-
.. testcode::
1831-
1832-
def float_to_decimal(f):
1833-
"Convert a floating point number to a Decimal with no loss of information"
1834-
n, d = f.as_integer_ratio()
1835-
with localcontext() as ctx:
1836-
ctx.traps[Inexact] = True
1837-
while True:
1838-
try:
1839-
return Decimal(n) / Decimal(d)
1840-
except Inexact:
1841-
ctx.prec += 1
1832+
Decimal though an exact conversion may take more precision than intuition would
1833+
suggest:
18421834

18431835
.. doctest::
18441836

1845-
>>> float_to_decimal(math.pi)
1837+
>>> Decimal(math.pi)
18461838
Decimal('3.141592653589793115997963468544185161590576171875')
18471839

1848-
Q. Why isn't the :func:`float_to_decimal` routine included in the module?
1849-
1850-
A. There is some question about whether it is advisable to mix binary and
1851-
decimal floating point. Also, its use requires some care to avoid the
1852-
representation issues associated with binary floating point:
1853-
1854-
>>> float_to_decimal(1.1)
1855-
Decimal('1.100000000000000088817841970012523233890533447265625')
1856-
18571840
Q. Within a complex calculation, how can I make sure that I haven't gotten a
18581841
spurious result because of insufficient precision or rounding anomalies.
18591842

Lib/decimal.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -648,8 +648,12 @@ def __new__(cls, value="0", context=None):
648648
return self
649649

650650
if isinstance(value, float):
651-
raise TypeError("Cannot convert float in Decimal constructor. "
652-
"Use from_float class method.")
651+
value = Decimal.from_float(value)
652+
self._exp = value._exp
653+
self._sign = value._sign
654+
self._int = value._int
655+
self._is_special = value._is_special
656+
return self
653657

654658
raise TypeError("Cannot convert %r to Decimal" % value)
655659

Lib/test/test_decimal.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ def init():
5252
)
5353
setcontext(DefaultTestContext)
5454

55+
# decorator for skipping tests on non-IEEE 754 platforms
56+
requires_IEEE_754 = unittest.skipUnless(
57+
float.__getformat__("double").startswith("IEEE"),
58+
"test requires IEEE 754 doubles")
59+
5560
TESTDATADIR = 'decimaltestdata'
5661
if __name__ == '__main__':
5762
file = sys.argv[0]
@@ -504,6 +509,27 @@ def test_explicit_from_Decimal(self):
504509
self.assertEqual(str(e), '0')
505510
self.assertNotEqual(id(d), id(e))
506511

512+
@requires_IEEE_754
513+
def test_explicit_from_float(self):
514+
r = Decimal(0.1)
515+
self.assertEqual(type(r), Decimal)
516+
self.assertEqual(str(r),
517+
'0.1000000000000000055511151231257827021181583404541015625')
518+
self.assertTrue(Decimal(float('nan')).is_qnan())
519+
self.assertTrue(Decimal(float('inf')).is_infinite())
520+
self.assertTrue(Decimal(float('-inf')).is_infinite())
521+
self.assertEqual(str(Decimal(float('nan'))),
522+
str(Decimal('NaN')))
523+
self.assertEqual(str(Decimal(float('inf'))),
524+
str(Decimal('Infinity')))
525+
self.assertEqual(str(Decimal(float('-inf'))),
526+
str(Decimal('-Infinity')))
527+
self.assertEqual(str(Decimal(float('-0.0'))),
528+
str(Decimal('-0')))
529+
for i in range(200):
530+
x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0)
531+
self.assertEqual(x, float(Decimal(x))) # roundtrip
532+
507533
def test_explicit_context_create_decimal(self):
508534

509535
nc = copy.copy(getcontext())

0 commit comments

Comments
 (0)