[Numpy-discussion] ?

Huaiyu Zhu hzhu at users.sourceforge.net
Mon Oct 30 19:49:02 EST 2000


On Mon, 30 Oct 2000 18:10:52 GMT, Rainer Deyke <root at rainerdeyke.com> wrote:
>
>Many things cannot be cut in half this easily.

That's why integers are not suitable for every division.

>a = [1, 2, 3]
>first_half, second_half = a[:1.5], a[1.5:]

You mean 

first_half, second_half = a[:floor(1.5)], a[floor(1.5):]

Or do you mean 

first_half, second_half = a[:ceil(1.5)], a[ceil(1.5):]

What does a[:-5/2] mean?  And a[:-5/-2]?  And a[:5/-2]?  Did you get all of
them right without experimenting?

Given the various different ways to convert a fraction into integers and
remainders (as evident from other posts in this thread), it is much safer to
use the correct mathematical result (or a best approx to it) and let users
specify what method they want to use to map it to a smaller domain

floor(1.2) == 1;  floor(-1.2) == -2; 
ceil(1.2) == 2;  floor(-1.2) == -1; 
int(1.2) == 1;  omt(-1.2) == -1; 

When something is not inherently uniquely defined, taking a single
convention out of several equally valid candidates is bound to generate
confusion.

Case in point:

>>> from Numeric import arange
>>> a = arange(-4, 4)
>>> a/2                                     # Using int
array([-2, -1, -1,  0,  0,  0,  1,  1])
>>> map(lambda x:x/2, a)                    # Using floor
[-2, -2, -1, -1, 0, 0, 1, 1]                

The mathematical result is good not just because mathematicians like it -
they like it because it is consistent.  For example

-5/2 = (-5)/2 = -(5/2) = 5/(-2) = -(-5/-2)

Likewise, sqrt(5) is not an integer, and sqrt(-4) is not a real number.  You
could map them to the smaller domain by int(x) or real(x) or infinitely many
other methods, but doing that by default is a guaranteed trouble maker.
Whichever mapping you use, the result will not be as consistent as the
original.

You may say math is only useful for scientific work.  That may be true a few
hundred years ago, but not now, not at least for that kind of math we are
talking about.  Most people wouldn't insist on using physics at the level
their primary school teacher told them.

<rant>
But at least here in the US there is a perception that math is not for
everybody.  This is unfortunate, because it actually makes math harder here
than in almost anywhere else in the world.  Many teachers try various
superficially simple rules instead of genuine underlying principles.  These
often generate a lot of trouble down the road.  And Americans thought that
their trouble of comparing mile, foot and inch to a metre is the most they
can take for science.  :-)
</rant>

I forgot who's the author of this famous quote: 
  Things should be made as simple as possible, but not simpler.


Huaiyu




More information about the Python-list mailing list