diff options
author | Raymond Hettinger <python@rcn.com> | 2010-04-02 18:39:24 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2010-04-02 18:39:24 (GMT) |
commit | ed171abd93c7a507cb26339aa2d68a7e980378f6 (patch) | |
tree | 315daef20f38f289972e450f267b3576abdbce44 /Doc | |
parent | dc36d7cf85b143995b8f0d3c0aaa35416d56a468 (diff) | |
download | cpython-ed171abd93c7a507cb26339aa2d68a7e980378f6.zip cpython-ed171abd93c7a507cb26339aa2d68a7e980378f6.tar.gz cpython-ed171abd93c7a507cb26339aa2d68a7e980378f6.tar.bz2 |
Issue 8257: Decimal constructor to accept float.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/decimal.rst | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/Doc/library/decimal.rst b/Doc/library/decimal.rst index 7c03b6a..6addd48 100644 --- a/Doc/library/decimal.rst +++ b/Doc/library/decimal.rst @@ -314,7 +314,7 @@ Decimal objects Construct a new :class:`Decimal` object based from *value*. - *value* can be an integer, string, tuple, or another :class:`Decimal` + *value* can be an integer, string, tuple, :class:`float`, or another :class:`Decimal` object. If no *value* is given, returns ``Decimal('0')``. If *value* is a string, it should conform to the decimal numeric string syntax after leading and trailing whitespace characters are removed:: @@ -341,6 +341,11 @@ Decimal objects digits, and an integer exponent. For example, ``Decimal((0, (1, 4, 1, 4), -3))`` returns ``Decimal('1.414')``. + If *value* is a :class:`float`, the binary floating point value is losslessly + converted to its exact decimal equivalent. This conversion can often require + upto 53 digits of precision. For example, ``Decimal(float('1.1'))`` converts + to ``Decimal('1.100000000000000088817841970012523233890533447265625')``. + The *context* precision does not affect how many digits are stored. That is determined exclusively by the number of digits in *value*. For example, ``Decimal('3.00000')`` records all five zeros even if the context precision is @@ -1948,17 +1953,16 @@ value unchanged:: ''' return d.quantize(Decimal(1)) if d == d.to_integral() else d.normalize() -Q. Is there a way to convert a regular float to a Decimal? +Q. Is there a way to convert a regular float to a :class:`Decimal`? -A. Yes, the classmethod :meth:`from_float` makes an exact conversion. +A. Yes, all binary floating point numbers can be exactly expressed as a +Decimal though an exact conversion may take more precision than intuition would +suggest: -The regular decimal constructor does not do this by default because there is -some question about whether it is advisable to mix binary and decimal floating -point. Also, its use requires some care to avoid the representation issues -associated with binary floating point: +.. doctest:: - >>> Decimal.from_float(1.1) - Decimal('1.100000000000000088817841970012523233890533447265625') + >>> Decimal(math.pi) + Decimal('3.141592653589793115997963468544185161590576171875') Q. Within a complex calculation, how can I make sure that I haven't gotten a spurious result because of insufficient precision or rounding anomalies. |