<p>这在Python3.1中已经改变了。从<a href="http://docs.python.org/dev/whatsnew/3.1.html#other-language-changes" rel="noreferrer">what's new</a>页:</p>
<blockquote>
<p>Python now uses David Gay’s algorithm for finding the shortest
floating point representation that doesn’t change its value. This
should help mitigate some of the confusion surrounding binary floating
point numbers.</p>
<p>The significance is easily seen with a number like <code>1.1</code> which does
not have an exact equivalent in binary floating point. Since there is
no exact equivalent, an expression like <code>float('1.1')</code> evaluates to
the nearest representable value which is <code>0x1.199999999999ap+0</code> in hex
or <code>1.100000000000000088817841970012523233890533447265625</code> in decimal.
That nearest value was and still is used in subsequent floating point
calculations.</p>
<p>What is new is how the number gets displayed. Formerly, Python used a
simple approach. The value of <code>repr(1.1)</code> was computed as <code>format(1.1,
'.17g')</code> which evaluated to <code>'1.1000000000000001'</code>. The advantage of
using 17 digits was that it relied on IEEE-754 guarantees to assure
that <code>eval(repr(1.1))</code> would round-trip exactly to its original value.
The disadvantage is that many people found the output to be confusing
(mistaking intrinsic limitations of binary floating point
representation as being a problem with Python itself).</p>
<p>The new algorithm for <code>repr(1.1)</code> is smarter and returns <code>'1.1'</code>.
Effectively, it searches all equivalent string representations (ones
that get stored with the same underlying float value) and returns the
shortest representation.</p>
<p>The new algorithm tends to emit cleaner representations when possible,
but it does not change the underlying values. So, it is still the case
that <code>1.1 + 2.2 != 3.3</code> even though the representations may suggest
otherwise.</p>
<p>The new algorithm depends on certain features in the underlying
floating point implementation. If the required features are not found,
the old algorithm will continue to be used. Also, the text pickle
protocols assure cross-platform portability by using the old
algorithm.</p>
<p>(Contributed by Eric Smith and Mark Dickinson; <a href="http://bugs.python.org/issue1580" rel="noreferrer">issue 1580</a>)</p>
</blockquote>