[MATRIX-SIG] integer division -- what a concept!

David J. C. Beach beach@verinet.com
Sat, 17 Jan 1998 10:56:15 -0700

Rupert Mazzucco wrote:

> > Two examples from the competition:
> >
> > <phil@peacock: /home/phil> matlab
> > >> 5/4
> > 5/4
> > ans =
> >     1.2500
> >
> > <phil@peacock: /home/phil> Splus
> > > 5/4
> > 5/4
> > [1] 1.25

That's funny, I've never seen anybody do *any* system programming in
matlab or Splus...  In fact, I think that those languages are used
almost exclusively by mathematicians.  And whadya know?  I think we may
have stumbled across the reason why.

You see, you're just looking to change Python in a way that's only
useful for people who do mathematics.  As far as you're concerned, those
features you don't use (e.g. integer division) have no purpose, so we
might as well remove them, right?  I'm not convinced at all (especially
since I need and use integer division in python.)

Suppose you have three cars that you want to divide among two people.
(There are some things in this world that it's just not a good idea to
divide, and not all math is done with apples....sometimes it's done with
bits and bytes.)

> or take Perl:
>   print 5/4    ;
>   1.25

Yes, but you forget that Perl doesn't differentiate it's scalar types at
all.  You can do something like this, too:

$x = "5";  # the string "5"
print $x; #prints the string "5"
print $x + 1; # prints the integer 6

Perl just doesn't care.  It will try to interpret any scalar "type" in
almost any way possible so that the operation you can work.  If you
would like to make python much more error prone, we could adopt this
policy.  Unfortunately, it is the source of many many errors in perl,
and probably a bad thing in python.


x = '5'
print x # prints the string '5'
print x + 1 # error! can't add a string to an int

If you really like the perl way better, go ahead and use perl.  My guess
is that -- truth be told -- you don't.

If you have a function, foo, and you want to make sure that it is
getting floats as it's arguments, you can always either test the
arguments, or:

def foo(x, y):  # x and y need to be floats
    x, y = float(x), float(y)
    # at this point, x and y are either floats, or an exception was
    # now, go about your usual business with confidence that operations
    # involving x and y will return floats (principle of least surprise)


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