[MATRIX-SIG] Re: int. division

David J. C. Beach beach@verinet.com
Sat, 17 Jan 1998 03:07:33 -0700

Rupert Mazzucco wrote:

> What do you mean, hardware level? Python is supposed to be a
> dynamically typed OO language, abstraction, encapsulation,
> all that stuff. Hardware level doesn't seem to be a particularly
> strong argument in this case.

Although the hardware level storage of ints and floats is indeed very
different, that is not actually the issue.  The issue here is type

Have none of you people (those who believe that 1/2 should be 0.5) ever
done any system level programming?  Have you ever worked with any graphics
code?  Have you ever actually needed integer division for anything you ever
wrote?  If the answer is no, then I can only assume that you think that
_features_ which are not immediately useful to you should be removed from
the language.

Allow me to reiterate Paul Dubois's argument.  Consider the code:

            x = i / j

Now, suppose i=2 and j=4.  Then x will be the integer, 2.  Everybody agrees
on this.  But suppose i=1 and j=2.  Then x, one argues, should be the FLOAT

Does anybody understand why this is a VeryBadThing(tm)?  It is for two

1) the type of x (int or float) depends on the values (not just the types)
of i and j.

2) there is no way to do integer division, which (while confusing to
mathematicians who only use math programming languages which tend to
interpret everything either symbolically or as floats) is

But there _is_ a way that we can make it so that everybody can still get
their respective jobs done.  How do we do this?  simple:

If you want to do operations on integers, use only integers, and you will
get an integer result.  If you want to do operations with float results,
use float arguments.

This does have the unfortunate side-effect of actually requiring one to
think about which type of numbers (s)he wants to use before hacking away.
Program with care.

> But even the Babylonians had some notion of rational numbers already.
> It's high time modern programming languages catch up with them!

And it's high time you caught up with modern programming languages.  Yes,
even a humble integer-division supporting language like Python gives you
this ability.  Through the wonderful techniques of aggregation and
encapsulation, you could certainly talk about rational numbers:

class Rational:

    def __init__(self, p, q):
        self.p, self.q = int(p), int(q)

    def __repr__(self):
        return "%i/%i" % (self.p, self.q)


(You could add all the operators, along with code to keep p and q in a
reduced form.  You could even use long integers, which would yield a very
precise Rational type.)

Isn't *that* what OO is all about?

BTW, while I know I sound biased, I hold a double B.S. in computer science
and applied mathematics, so I'm not really here to anger any
mathematicians.  I simply believe that integer division is a necessary and
important part of Python.  I think that anybody who has a well rounded
programming background would understand why.

David J. C. Beach
Colorado State University

MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org