Integers and Floats

Aahz Maruch aahz at netcom.com
Mon Oct 11 12:29:32 EDT 1999


In article <19991011180728.A14620 at optiplex.palga.uucp>,
Gerrit Holl  <gerrit at nl.linux.org> wrote:
>
>I know there is a difference between integers and floats, but I think the
>following is a little strange:
>
>18:07:48:3/499:gerrit at gerrit:~$ python
>Python 1.5.2 (#1, Aug  8 1999, 01:35:00)  [GCC egcs-2.91.66 19990314/Linux (egcs- on linux2
>Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>>> 131 / 4
>32
>>>> 4 * 32
>128
>>>> 131 / 4 * 4
>128
>
>I think the ouput of the first two statement is ok, but why can't python store
>an integer and a float as the same thing? And is the conversion between an
>integer and a float so big? Or are floats just slower? Do I have to do the
>following:

The problem is that floats involve loss of precision.  If speed is not
absolutely essential, I would suggest going to Python longs and doing
all multiplication before division.  131*4/4 gives the correct result.

(The other option is to implement a Rational class -- there may even be
one already.)

But, yeah, you could just force everything to float -- just don't be
surprised when you get an answer like "9.00000000001".
--
                      --- Aahz (@netcom.com)

Androgynous poly kinky vanilla queer het    <*>      http://www.rahul.net/aahz/
Hugs and backrubs -- I break Rule 6  (if you want to know, do some research)




More information about the Python-list mailing list