Signed word lengths and indexes

Graham Fawcett fawcett at uwindsor.ca
Tue Jun 15 08:46:47 PDT 2010


On Tue, 15 Jun 2010 11:26:34 -0400, Adam Ruppe wrote:

> Not referring to anyone in particular, but it just occurred to me:
> 
> Python's use of -1 to mean length, and -2 to mean length -1 isn't signed
> ints... it is using overflow! .... sort of. The thing is that they are
> overflowing at a dynamic amount (array.length) instead of the fixed
> size.

Well, conceptually maybe. :) Python integers don't overflow, they
automatically convert up to bigints. It's more correct to just say
that Python specifies that a negative array index means a reference
from the right end.

> Actually, if it is treated as full blown proper overflow, you could get
> some potentially useful stuff out of it. Given length == 5, -1 > 3. I
> don't know if Python actually lets you do that, but I doubt it.

Only in explicit modular arithmetic, e.g. '-1 % 5 == 4'. You can slice
a Python list using both postive and negative indexes:

      [10,20,30,40,50][3:-1]  ==> [40]

...but that doesn't imply overflow or modular arithmetic: it's just
the array-indexing contract.

Graham

> 
> 
> But I just had a chuckle about that thought :)



More information about the Digitalmars-d mailing list