Add versionchanged entry for Decimal(float) construction.

Also add an example of constructing a Decimal directly from a float,
update the quickstart tutorial, and tweak another couple of
sentences.
This commit is contained in:
Mark Dickinson 2010-04-04 22:09:21 +00:00
parent bb006cf26c
commit b1affc517f
1 changed files with 15 additions and 7 deletions

View File

@ -133,10 +133,9 @@ precision, rounding, or enabled traps::
>>> getcontext().prec = 7 # Set a new precision
Decimal instances can be constructed from integers, strings, or tuples. To
create a Decimal from a :class:`float`, first convert it to a string. This
serves as an explicit reminder of the details of the conversion (including
representation error). Decimal numbers include special values such as
Decimal instances can be constructed from integers, strings, floats, or tuples.
Construction from an integer or a float performs an exact conversion of the
value of that integer or float. Decimal numbers include special values such as
:const:`NaN` which stands for "Not a number", positive and negative
:const:`Infinity`, and :const:`-0`.
@ -145,6 +144,8 @@ representation error). Decimal numbers include special values such as
Decimal('10')
>>> Decimal('3.14')
Decimal('3.14')
>>> Decimal(3.14)
Decimal('3.140000000000000124344978758017532527446746826171875')
>>> Decimal((0, (3, 1, 4), -2))
Decimal('3.14')
>>> Decimal(str(2.0 ** 0.5))
@ -343,8 +344,9 @@ Decimal objects
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')``.
53 or more 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,
@ -362,6 +364,9 @@ Decimal objects
leading and trailing whitespace characters are permitted when
creating a Decimal instance from a string.
.. versionchanged:: 2.7
The argument to the constructor is now permitted to be a :float:`instance`.
Decimal floating point objects share many properties with the other built-in
numeric types such as :class:`float` and :class:`int`. All of the usual math
operations and special methods apply. Likewise, decimal objects can be
@ -523,6 +528,9 @@ Decimal objects
`0x1.999999999999ap-4`. That equivalent value in decimal is
`0.1000000000000000055511151231257827021181583404541015625`.
.. note:: From Python 2.7 onwards, a :class:`Decimal` instance
can also be constructed directly from a :class:`float`.
.. doctest::
>>> Decimal.from_float(0.1)
@ -1955,7 +1963,7 @@ value unchanged::
Q. Is there a way to convert a regular float to a :class:`Decimal`?
A. Yes, all binary floating point numbers can be exactly expressed as a
A. Yes, any binary floating point number can be exactly expressed as a
Decimal though an exact conversion may take more precision than intuition would
suggest: