[docs] [issue14245] float rounding examples in FAQ are outdated

Mark Dickinson report at bugs.python.org
Sat Mar 10 12:26:28 CET 2012


Mark Dickinson <dickinsm at gmail.com> added the comment:

Proposed rewrite:




Why are floating point calculations inaccurate?
-----------------------------------------------

Users are often surprised by results like this::

   >>> 1.2 - 1.0
   0.199999999999999996

and think it is a bug in Python.  It's not.  This has little to do with Python,
and much more to do with how the underlying platform handles floating-point
numbers.

Python floats are stored internally in binary floating-point, using a fixed
precision (typically 53 bits).  Many numbers that can be written easily in
decimal notation (``1.2``, for example), cannot be expressed exactly in this
internal binary format.  After::

   >>> x = 1.2

the value stored for x is a (very good) approximation to the value ``1.2``, but
is not exactly equal to it.  (On a typical machine, the actual stored value
is::

   1.1999999999999999555910790149937383830547332763671875

which is accurate to around 16 decimal digits.)  Similarly, the result of any
floating-point operation must often be rounded to fit into the internal format,
resulting in another tiny error.

For a more detailed explanation of what's involved, please see the chapter on
:ref:`floating point arithmetic <tut-fp-issues>` in the Python tutorial.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue14245>
_______________________________________


More information about the docs mailing list