[Tutor] list of floats, strange behavior

Alan Gauld alan.gauld at blueyonder.co.uk
Wed Sep 1 14:23:54 CEST 2004


> 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

It doesn't expand the largest value as such it just expands to the
best accuracy that it can provide. YThis is a problem with the way
computers store floating point numbers. Its related to the fact that
one third in decimal is 0.3333333...forever. In other words you can't
exactly represent it in decimal. The same happens in binary.

However if you want to print it out you have lots of options for
tidying it up.

The default print will tidy it a bit, and for more control you can use
the format string:

>>> print "%7.5f" % 0.12479999999999999
0.12480

ie 7 characters long with 5 decimal places...


> >>> list01
> [0.0998, 0.1028, 0.1062, 0.1101, 0.12479999999999999]

Trying print:

>>> print list01[-1]
0.1248

HTH,

Alan G



More information about the Tutor mailing list