@@ -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:
18241829Q. Is there a way to convert a regular float to a :class: `Decimal `?
18251830
18261831A. 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-
18571840Q. Within a complex calculation, how can I make sure that I haven't gotten a
18581841spurious result because of insufficient precision or rounding anomalies.
18591842
0 commit comments