Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions Doc/library/decimal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,22 @@ Decimal objects
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
53 or more digits of precision. For example, ``Decimal(float('1.1'))``
converts to
``Decimal('1.100000000000000088817841970012523233890533447265625')``.
converted to its exact decimal equivalent. This means, for example, that
``Decimal(0.1)`` is not the same as ``Decimal('0.1')``.
Since 0.1 is not exactly representable in binary floating point, the
value is stored as the nearest representable value which is
``0x1.999999999999ap-4``.

.. doctest::

>>> Decimal(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> Decimal(float('nan'))
Decimal('NaN')
>>> Decimal(float('inf'))
Decimal('Infinity')
>>> Decimal(float('-inf'))
Decimal('-Infinity')

The *context* precision does not affect how many digits are stored. That is
determined exclusively by the number of digits in *value*. For example,
Expand Down Expand Up @@ -571,29 +583,9 @@ Decimal objects
>>> Decimal(321).exp()
Decimal('2.561702493119680037517373933E+139')

.. method:: from_float(f)
.. classmethod:: from_float(f)

Classmethod that converts a float to a decimal number, exactly.

Note `Decimal.from_float(0.1)` is not the same as `Decimal('0.1')`.
Since 0.1 is not exactly representable in binary floating point, the
value is stored as the nearest representable value which is
`0x1.999999999999ap-4`. That equivalent value in decimal is
`0.1000000000000000055511151231257827021181583404541015625`.

.. note:: From Python 3.2 onwards, a :class:`Decimal` instance
can also be constructed directly from a :class:`float`.

.. doctest::

>>> Decimal.from_float(0.1)
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> Decimal.from_float(float('nan'))
Decimal('NaN')
>>> Decimal.from_float(float('inf'))
Decimal('Infinity')
>>> Decimal.from_float(float('-inf'))
Decimal('-Infinity')
Alternative constructor that only accepts instances of :class:`float`.

.. versionadded:: 3.1

Expand Down
26 changes: 6 additions & 20 deletions Doc/library/fractions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,15 @@ another rational number, or from a string.

.. versionadded:: 3.8

.. method:: from_float(flt)
.. classmethod:: from_float(flt)

This class method constructs a :class:`Fraction` representing the exact
value of *flt*, which must be a :class:`float`. Beware that
``Fraction.from_float(0.3)`` is not the same value as ``Fraction(3, 10)``.
Alternative constructor which only accepts instances of
:class:`~numbers.Integral` or :class:`float`.

.. note::

From Python 3.2 onwards, you can also construct a
:class:`Fraction` instance directly from a :class:`float`.


.. method:: from_decimal(dec)

This class method constructs a :class:`Fraction` representing the exact
value of *dec*, which must be a :class:`decimal.Decimal` instance.

.. note::

From Python 3.2 onwards, you can also construct a
:class:`Fraction` instance directly from a :class:`decimal.Decimal`
instance.
.. classmethod:: from_decimal(dec)

Alternative constructor which only accepts instances of
:class:`decimal.Decimal`.

.. method:: limit_denominator(max_denominator=1000000)

Expand Down