[Tutor] list of floats, strange behavior

Barry Tice python at pointcontrol.com
Wed Sep 1 14:09:04 CEST 2004


Yes, the explanation of floating-point rounding errors is in Appendix B
of the tutorial.

The long and short of it is, floating point numbers are stored as binary
fractions.

Consider that in a base-three number system, the number that in decimal
we refer to as 1/3 would be 0.1. In decimal, it's 0.3333333333(etc.).
There is no exact value in base 10 for something that's easy to
represent in base 3.

Similarly, there are many things that can be represented in base 10 that
can't be exactly specified in binary, base 2. (0.1, for example.) So
based on the underlying C representation, Python makes its best guess
and comes up with the closest number it can.

-- b.r.t.

On Wed, 2004-09-01 at 03:18, Bernard Lebel wrote:
> Hi,
> 
> In the tutorial (I think) from the Python docs there is an explanation about
> this problem, wich is inherent to floating point numbers in computers.
> Very interesting read.
> 
> 
> Cheers
> Bernard
> 
> ----- Original Message ----- 
> From: <tpc at csua.berkeley.edu>
> To: <tutor at python.org>
> Sent: Wednesday, September 01, 2004 8:35 AM
> Subject: [Tutor] list of floats, strange behavior
> 
> 
> >
> > hi everybody, I was wondering if someone could help me understand why IDLE
> > seems to expand the largest value in a series of floating points I decided
> > to put in a list:
> >
> > >>> list01 = [.1028, .1248, .0998, .1101, .1062]
> > >>> list01.sort()
> > >>> list01
> > [0.0998, 0.1028, 0.1062, 0.1101, 0.12479999999999999]
> >
> > I don't particularly want that value expanded like that.
> >



More information about the Tutor mailing list