Numbers in python

Cameron Laird claird at lairds.us
Fri Mar 10 13:08:03 EST 2006


In article <47di13Ff4q11U1 at uni-berlin.de>,
Diez B. Roggisch <deets at nospam.web.de> wrote:
>> 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

I wonder if the original poster might not be best off--or at
least think he is--with

    str(0.965)



More information about the Python-list mailing list