Message155303
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. |
|
Date |
User |
Action |
Args |
2012-03-10 11:26:28 | mark.dickinson | set | recipients:
+ mark.dickinson, zbysz, docs@python |
2012-03-10 11:26:28 | mark.dickinson | set | messageid: <1331378788.28.0.772857729367.issue14245@psf.upfronthosting.co.za> |
2012-03-10 11:26:27 | mark.dickinson | link | issue14245 messages |
2012-03-10 11:26:26 | mark.dickinson | create | |
|