[Tutor] Division question, simple
Mon, 17 Dec 2001 23:30:29 +0100
The answer is, that / simply performs two
different kinds of division (or as some say,
the / - Operator is overloaded ) depending
on the type of the operands.
If both operands ar of type int (or long int)
(as is the case when evaluating 75/100)
/ delivers the integer quotient of the operands.
if at least one of the operands is of type float
(for instance 75.0/100 or equivalently float(75)/100)
the result will also be of type float.
So if you want to perform floating-point-division you
have to ensure, that at least one operand is of type
As the designers of Python recognized this to be
an error-prone approach, they decided (as usual after a
lengthy discussion) to change this
int the __future__ and to introduce a special
operator // for integer division - the / at the same
time reserving to conventional division.
You can take advantage of this from Dec. 21th (approx.) on
- if you like - by using Python 2.2 an the statement
from __future__ import division
at the beginning of your modules.
In any case Python 2.2 will provide the integer-division-operator //.
But beware of breaking older modules which depend
on the / - integer-division.
(I assume there will certainly be a paragraph on this in
the new Python 2.2 Documentation)
----- Original Message -----
From: "Sean 'Shaleh' Perry" <email@example.com>
To: "Frank Peavy" <firstname.lastname@example.org>
Cc: "tutor-python.org" <email@example.com>
Sent: Monday, December 17, 2001 10:11 PM
Subject: Re: [Tutor] Division question, simple
> On 17-Dec-2001 Frank Peavy wrote:
> > In the interactive mode:
> > >>> 75/100
> > 0
> > >>> float(75/100)
> > 0.0
> > >>> x = 75
> > >>> y = 100
> > >>> float(x/y)
> > 0.0
> > >>> float(75)/float(100)
> > 0.75
> > In order to get 75%, I almost need to know my desired result, before I
> > my values. Is this how everyone handles simple division in Python?
> > When I am dividing 100 by 25 the result is an integer of 4, fine.
> > But, when I don't know my second value is 25... maybe it is 33, then my
> > result would be 3.33333
> > BUT, Python gives me 3. How does everyone else handle this?
> if I know I want a decimal, I cast one number to a float.
> Tutor maillist - Tutor@python.org