Numbers in python

Diez B. Roggisch deets at nospam.web.de
Fri Mar 10 09:48:27 EST 2006


> However, I can't seem to get the program to treat the numbers as
> numbers. If I put them in the dictionary as 'THE' = int(0.965) the
> program returns 1.0

It certainoly does _not_ return 1.0 - it returns 1. And that is all it can
return for being an integer that has by definition no fractional part.

> and if I put 'THE' = float(0.965) it returns 
> 0.96555555549 or something similar. Neither of these are right! I
> basically need to access each item in the string as a number, because
> for my last function I want to multiply them all together by each
> other.

It _is_ the right number if you use floats - you just have to take into
account that 10-based fractions can't always be represented properly by
2-based IEEE floating points, resulting in rounding errors. Which is what
you see.

http://wiki.python.org/moin/RepresentationError?highlight=%28float%29

Either you don't care about the minor differences, the use float. Or you do,
then use the decimal-class introduced in python 2.4

Diez



More information about the Python-list mailing list