[Tutor] Integer division (was wxPython? (fwd))

Tom Jenkins tjenkins@devis.com
31 Dec 2001 14:00:40 -0500


Hey Karshi,
These results are consistent with the notion of integer division.

int/int will return an int
int/float will return a float
float/int will return a float

so

2/8 = 0 with remainder of .25

2/-10 = 0 with remainder of -.2

now python will round down the remainders so

2/8 = 0
2/-10 = -1

Now this will change with python 2.2, I believe
int/int will return a float
float/float will return a float

if you want to your divide to return a float then you can change it to:
def divide(x,y):
  return float(x)/y

or alternatively:
def divide(x,y):
  return divmod(x,y)[0]


On Mon, 2001-12-31 at 13:16, Danny Yoo wrote:
> Hi Karshi,
> 
> Let me forward this to the rest of Tutor.  I hate saying this, but I
> can't reproduce the wackiness.
> 
> The only thing I can think that of that can trigger this behavior is if
> divide() itself was defined as:
> 
> ###
> def divide(x,y):
>     return x
> ###
> 
> But then, I might not be considering some strange side effect.  Can you
> check to see if this is what's happening?  Also, what version of Python
> are you using?
> 
> 
> 
> ---------- Forwarded message ----------
> Date: Mon, 31 Dec 2001 09:13:45 -0500
> From: Karshi <karshi.hasanov@utoronto.ca>
> To: Danny Yoo <dyoo@hkn.eecs.berkeley.edu>
> Subject: Re: [Tutor] wxPython?
> 
> Hi Danny,
> Can you tell me why I am getting the strange results here:
> 
> from Numeric import *
> 
> def divide(x,y):
>     return x/y
> 
> for y in range(-10,10,1):
>     x=2
>     try:
>         print divide(x,y), y
>         
>     except ZeroDivisionError:
>         print "You are trying to divide by zero"
>     
> ----------------
> 
> -1 -10
> -1 -9
> -1 -8
> -1 -7
> -1 -6
> -1 -5
> -1 -4
> -1 -3
> -1 -2
> -2 -1
> You are trying to divide by zero
> 2 1
> 1 2
> 0 3
> 0 4
> 0 5
> 0 6
> 0 7
> 0 8
> 0 9
> ------------------------------
> Why x/y gives wrong results?
> 
> 
> On Tuesday 25 December 2001 02:32 pm, you wrote:
> > On Mon, 24 Dec 2001, Karshi Hasanov wrote:
> > >  I wanna use python GUI's , like wxPython or Tkinter.
> > > Which one would be easy to learn, or what 's advantage of using wxPython
> > > than Tkinter?
> >
> > Both are pretty nice.  I think that Tkinter is easier to learn, but many
> > people feel that Tkinter looks ugly.  *grin*
> >
> >
> > Tkinter is also installed by default when we install Python, so it's used
> > quite a lot.  Have you seen Fredrik Lungh's 'An Introduction to Tkinter'
> > web site?
> >
> >     http://www.pythonware.com/library/tkinter/introduction/
> >
> > It's a great tutorial on Tkinter programming.  Take a look at it, and see
> > if Tkinter is a good fit.
> >
> > Good luck!
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
-- 

Tom Jenkins
Development InfoStructure
http://www.devis.com