Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e534a07

Browse files
committed
Merged revisions 79756 via svnmerge from
svn+ssh://[email protected]/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. ........
1 parent 414e737 commit e534a07

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

Doc/library/decimal.rst

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,9 @@ precision, rounding, or enabled traps::
127127

128128
>>> getcontext().prec = 7 # Set a new precision
129129

130-
Decimal instances can be constructed from integers, strings, or tuples. To
131-
create a Decimal from a :class:`float`, first convert it to a string. This
132-
serves as an explicit reminder of the details of the conversion (including
133-
representation error). Decimal numbers include special values such as
130+
Decimal instances can be constructed from integers, strings, floats, or tuples.
131+
Construction from an integer or a float performs an exact conversion of the
132+
value of that integer or float. Decimal numbers include special values such as
134133
:const:`NaN` which stands for "Not a number", positive and negative
135134
:const:`Infinity`, and :const:`-0`.
136135

@@ -139,6 +138,8 @@ representation error). Decimal numbers include special values such as
139138
Decimal('10')
140139
>>> Decimal('3.14')
141140
Decimal('3.14')
141+
>>> Decimal(3.14)
142+
Decimal('3.140000000000000124344978758017532527446746826171875')
142143
>>> Decimal((0, (3, 1, 4), -2))
143144
Decimal('3.14')
144145
>>> Decimal(str(2.0 ** 0.5))
@@ -336,8 +337,9 @@ Decimal objects
336337

337338
If *value* is a :class:`float`, the binary floating point value is losslessly
338339
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')``.
340+
53 or more digits of precision. For example, ``Decimal(float('1.1'))``
341+
converts to
342+
``Decimal('1.100000000000000088817841970012523233890533447265625')``.
341343

342344
The *context* precision does not affect how many digits are stored. That is
343345
determined exclusively by the number of digits in *value*. For example,
@@ -351,6 +353,9 @@ Decimal objects
351353

352354
Once constructed, :class:`Decimal` objects are immutable.
353355

356+
.. versionchanged:: 3.2
357+
The argument to the constructor is now permitted to be a :float:`instance`.
358+
354359
Decimal floating point objects share many properties with the other built-in
355360
numeric types such as :class:`float` and :class:`int`. All of the usual math
356361
operations and special methods apply. Likewise, decimal objects can be
@@ -490,6 +495,9 @@ Decimal objects
490495
`0x1.999999999999ap-4`. That equivalent value in decimal is
491496
`0.1000000000000000055511151231257827021181583404541015625`.
492497

498+
.. note:: From Python 3.2 onwards, a :class:`Decimal` instance
499+
can also be constructed directly from a :class:`float`.
500+
493501
.. doctest::
494502

495503
>>> Decimal.from_float(0.1)
@@ -1846,7 +1854,7 @@ value unchanged:
18461854

18471855
Q. Is there a way to convert a regular float to a :class:`Decimal`?
18481856

1849-
A. Yes, all binary floating point numbers can be exactly expressed as a
1857+
A. Yes, any binary floating point number can be exactly expressed as a
18501858
Decimal though an exact conversion may take more precision than intuition would
18511859
suggest:
18521860

0 commit comments

Comments
 (0)