[MATRIX-SIG] Re: int. division

Rupert Mazzucco maz@pap.univie.ac.at
Sat, 17 Jan 1998 13:52:31 +0100 (MET)

[long posting, maybe interesting (?) suggestion at the end]

On Sat, 17 Jan 1998, David J. C. Beach wrote:
> 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

Actually, I would do system level stuff in C. I thought Python was
meant to be more a kind of high-level language.

> 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.

No, I'm not *that* egoistic :-) But I find that this particular 
_useful feature_ is actually quite problematic.

>             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
> 0.5.

But such is life. Suppose two people have four whole apples. If they
divide them, each one will get two whole apples. OK. Now suppose they
had three whole apples they want to divide. How do we do that, they ask
themselves? After scratching their heads for a while, each one takes
one whole apple, and the remaining apple they cut in two halves.

I think most people would agree that this is a more sensible solution,
than to throw the third apple away. And this kind of solution is not new. 

[pragraph paraphrased, hopefully correctly]
> ... integer division is ... VERY NECESSARY FOR MANY OTHER PURPOSES.
> 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:
> ...
> Program with care.

Now this leads exactly to the point I'm trying to make.
Even if you _program with care_, how can you ever be sure
you have been careful *enough*? If somewhere (n+1) slipped
into the code instead of (n+1.0), the numerical results
may be wrong. Yet this kind of bug is very difficult
to detect in the first place and hard to find in the second.
I would really appreciate some help from Python here.

> [insightful solution, but for a different problem, snipped]

> 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.

Hm, I don't know much about the internals of python, but IMHO the problem
I'm talking about really arises if you have expressions like

  I[n] = float1 + ( n / ( n+1 )) * float2

Maybe this could be evaluated differently, so that the interpreter
sees we're really talking about floats here and does the correct
conversion for n/(n+1) *before* dividing? (because I is typed float,
that is, not primarily because of float1 or float2)

Rupert Mazzucco

MATRIX-SIG  - SIG on Matrix Math for Python

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