Commit e534a077 authored by Mark Dickinson's avatar Mark Dickinson

Merged revisions 79756 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r79756 | mark.dickinson | 2010-04-04 23:09:21 +0100 (Sun, 04 Apr 2010) | 6 lines

  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.
........
parent 414e7376
...@@ -127,10 +127,9 @@ precision, rounding, or enabled traps:: ...@@ -127,10 +127,9 @@ precision, rounding, or enabled traps::
>>> getcontext().prec = 7 # Set a new precision >>> getcontext().prec = 7 # Set a new precision
Decimal instances can be constructed from integers, strings, or tuples. To Decimal instances can be constructed from integers, strings, floats, or tuples.
create a Decimal from a :class:`float`, first convert it to a string. This Construction from an integer or a float performs an exact conversion of the
serves as an explicit reminder of the details of the conversion (including value of that integer or float. Decimal numbers include special values such as
representation error). Decimal numbers include special values such as
:const:`NaN` which stands for "Not a number", positive and negative :const:`NaN` which stands for "Not a number", positive and negative
:const:`Infinity`, and :const:`-0`. :const:`Infinity`, and :const:`-0`.
...@@ -139,6 +138,8 @@ representation error). Decimal numbers include special values such as ...@@ -139,6 +138,8 @@ representation error). Decimal numbers include special values such as
Decimal('10') Decimal('10')
>>> Decimal('3.14') >>> Decimal('3.14')
Decimal('3.14') Decimal('3.14')
>>> Decimal(3.14)
Decimal('3.140000000000000124344978758017532527446746826171875')
>>> Decimal((0, (3, 1, 4), -2)) >>> Decimal((0, (3, 1, 4), -2))
Decimal('3.14') Decimal('3.14')
>>> Decimal(str(2.0 ** 0.5)) >>> Decimal(str(2.0 ** 0.5))
...@@ -336,8 +337,9 @@ Decimal objects ...@@ -336,8 +337,9 @@ Decimal objects
If *value* is a :class:`float`, the binary floating point value is losslessly If *value* is a :class:`float`, the binary floating point value is losslessly
converted to its exact decimal equivalent. This conversion can often require converted to its exact decimal equivalent. This conversion can often require
upto 53 digits of precision. For example, ``Decimal(float('1.1'))`` converts 53 or more digits of precision. For example, ``Decimal(float('1.1'))``
to ``Decimal('1.100000000000000088817841970012523233890533447265625')``. converts to
``Decimal('1.100000000000000088817841970012523233890533447265625')``.
The *context* precision does not affect how many digits are stored. That is The *context* precision does not affect how many digits are stored. That is
determined exclusively by the number of digits in *value*. For example, determined exclusively by the number of digits in *value*. For example,
...@@ -351,6 +353,9 @@ Decimal objects ...@@ -351,6 +353,9 @@ Decimal objects
Once constructed, :class:`Decimal` objects are immutable. Once constructed, :class:`Decimal` objects are immutable.
.. versionchanged:: 3.2
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 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 numeric types such as :class:`float` and :class:`int`. All of the usual math
operations and special methods apply. Likewise, decimal objects can be operations and special methods apply. Likewise, decimal objects can be
...@@ -490,6 +495,9 @@ Decimal objects ...@@ -490,6 +495,9 @@ Decimal objects
`0x1.999999999999ap-4`. That equivalent value in decimal is `0x1.999999999999ap-4`. That equivalent value in decimal is
`0.1000000000000000055511151231257827021181583404541015625`. `0.1000000000000000055511151231257827021181583404541015625`.
.. note:: From Python 3.2 onwards, a :class:`Decimal` instance
can also be constructed directly from a :class:`float`.
.. doctest:: .. doctest::
>>> Decimal.from_float(0.1) >>> Decimal.from_float(0.1)
...@@ -1846,7 +1854,7 @@ value unchanged: ...@@ -1846,7 +1854,7 @@ value unchanged:
Q. Is there a way to convert a regular float to a :class:`Decimal`? 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 Decimal though an exact conversion may take more precision than intuition would
suggest: suggest:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment