This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients docs@python, mark.dickinson, zbysz
Date 2012-03-10.11:26:26
SpamBayes Score 1.1867457e-10
Marked as misclassified No
Message-id <1331378788.28.0.772857729367.issue14245@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2012-03-10 11:26:28mark.dickinsonsetrecipients: + mark.dickinson, zbysz, docs@python
2012-03-10 11:26:28mark.dickinsonsetmessageid: <1331378788.28.0.772857729367.issue14245@psf.upfronthosting.co.za>
2012-03-10 11:26:27mark.dickinsonlinkissue14245 messages
2012-03-10 11:26:26mark.dickinsoncreate