# String negative indices?

Discussion in 'Python' started by drtimhill@comcast.net, Jun 23, 2006.

1. ### Guest

I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled.

For example, to get the last character in a string, I can enter "x[-1]". To get the 2nd and 3rd to last, I can enter x[-3:-1] etc. This is fine.

Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].

With simple constants, this is ok, but it's a little more annoying when the start and end of the range are in variables somewhere. The only way I can find of making this work without lots of special-case "if" logic is to translate negative subscripts to positive, which kinda defeats the purpose of the negative subscripts anyway.

Is there some magic I'm missing here? Wouldn't it actually be better for Python to treat 0 as a special case here, so that x[-2:0] and x[-2:] generated the same result?

--Tim

, Jun 23, 2006

2. ### Erik Max FrancisGuest

wrote:

> Is there some magic I'm missing here? Wouldn't it actually be better for Python to treat 0 as a
> special case here, so that x[-2:0] and x[-2:] generated the same result?

No, since x[-2:0:-1] already has meaning and it isn't what you want.

--
Erik Max Francis && && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
The mind is not a vessel to be filled but a fire to be kindled.
-- Plutarch

Erik Max Francis, Jun 23, 2006

3. ### George SakkisGuest

wrote:

> I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled.
>
> For example, to get the last character in a string, I can enter "x[-1]". To get the 2nd and 3rd to last, I can enter x[-3:-1] etc. This is fine.
>
> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].
>
> With simple constants, this is ok, but it's a little more annoying when the start and end of the range are in variables somewhere. The only way I can find of making this work without lots of special-case "if" logic is to translate negative subscripts to positive, which kinda defeats the purpose of the negative subscripts anyway.
>
> Is there some magic I'm missing here? Wouldn't it actually be better for Python to treat 0 as a special case here, so that x[-2:0] and x[-2:] generated the same result?
>
> --Tim

x[-2:None]

I'm not sure if it qualifies as "magic" but you're right, it's not more
obvious than 0 would be.

George

George Sakkis, Jun 23, 2006
4. ### Filip WasilewskiGuest

wrote:

> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].

Hooray! Logically there is no such thing as positive or negative zero,
or did I miss something in the primary?

PS. x[len(x)-2 : len(x)-0]

cheers,
fw

Filip Wasilewski, Jun 23, 2006
5. ### Steven D'ApranoGuest

On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:

> wrote:
>
>> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].

>
> Hooray! Logically there is no such thing as positive or negative zero,
> or did I miss something in the primary?

No, not in the primary, or even in the secondary, but possibly in the
tertiary.

For many purposes, it doesn't make sense to distinguish +0 from -0. But
for other purposes, it does.

For instance, in floating point maths, it may be useful for negative
numbers that underflow to be distinguished from positive numbers that
underflow. See, for example,
http://www.savrola.com/resources/negative_zero.html

In statistical mechanics, some systems can have negative absolute
temperatures, including negative zero. Counter-intuitively, negative
absolute temperatures aren't colder than absolute zero, but hotter than
any positive temperature. So, strangely enough, a temperature of -0K is
hotter than a infinitely hot temperature!

(Those wacky physicists and their mathematics...)

See http://en.wikipedia.org/wiki/Negative_absolute_temperature for a
description. And in case you think this is just a modern version of angels
dancing on the head of a pin, negative absolute temperatures are essential
for lasers.

In pure mathematics, zero is usually considered unsigned. However, in
"non-standard analysis" using so-called "hyperreal" or "surreal" numbers,
mathematicians use infinitesimals which are [sloppy hand-waving] like
signed zeroes. To put it another way, only slightly less sloppy,
infinitesimals are zero, but not all the same zero.

When doing calculus with complex numbers, it is very important to
distinguish which direction you are taking your limits in, and so
lim z -> 0+0i is not necessarily the same as lim z -> 0-0i.

--
Steven

Steven D'Aprano, Jun 23, 2006
6. ### Filip WasilewskiGuest

Steven D'Aprano wrote:
> On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
>
> > wrote:
> >
> >> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].

> >
> > Hooray! Logically there is no such thing as positive or negative zero,
> > or did I miss something in the primary?

>
> No, not in the primary, or even in the secondary, but possibly in the
> tertiary.
>
> For many purposes, it doesn't make sense to distinguish +0 from -0. But
> for other purposes, it does.

I believe you will agree that this is mostly a matter of notation of
some fact rather than really trying to sign zero.
For example I use to denote numbers that approach zero from "the right
side" by x -> [0 uppercase +], which is definitely harder to type and
display in poor man's text editor than x -> +0.
Otherwise I should also rethink the meaning of positive (>0) and
negative (<0) numbers (at least where such relations are defined).

[...]

> In statistical mechanics, some systems can have negative absolute
> temperatures, including negative zero. Counter-intuitively, negative
> absolute temperatures aren't colder than absolute zero, but hotter than
> any positive temperature. So, strangely enough, a temperature of -0K is
> hotter than a infinitely hot temperature!

Yeah, this is really cool ;-)

--
Filip

"During board meeting: We have great news. Our earnings reached highest
value ever - a positive 0."

Filip Wasilewski, Jun 24, 2006
7. ### Steven D'ApranoGuest

On Sat, 24 Jun 2006 05:36:17 -0700, Filip Wasilewski wrote:

> Steven D'Aprano wrote:
>> On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
>>
>> > wrote:
>> >
>> >> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].
>> >
>> > Hooray! Logically there is no such thing as positive or negative zero,
>> > or did I miss something in the primary?

>>
>> No, not in the primary, or even in the secondary, but possibly in the
>> tertiary.
>>
>> For many purposes, it doesn't make sense to distinguish +0 from -0. But
>> for other purposes, it does.

>
> I believe you will agree that this is mostly a matter of notation of
> some fact rather than really trying to sign zero.

No.

In floating point, +0 *really is* different from -0 -- the two floats
have different bit patterns. Floating point libraries must be specifically
programmed to ignore that difference, making zero treated differently than
other floats. That's -- usually -- a good thing.

In mathematics, well, maybe... certainly in the Real number system, there
is no difference, and +0 and -0 are just two ways of writing the same
thing. In the hyperreals, +0 and -0 are the same, but there are
infinitesimals which are different, and signed. I don't know enough about
the surreals to comment. In matrix maths, there are an infinite number of
different matrices where all the elements are zero -- they are all
distinct, different, zeroes.

And in "the real world", all numbers are an abstraction anyway, so I'm
not too worried about correspondence to reality. If mathematicians find a
use for distinguishing +0 from -0 (as the statistical physicists have
done) they will treat them as "really" different. If they don't, they
won't.

--
Steven.

Steven D'Aprano, Jun 25, 2006
8. ### Erik Max FrancisGuest

Steven D'Aprano wrote:

> In mathematics, well, maybe... certainly in the Real number system, there
> is no difference, and +0 and -0 are just two ways of writing the same
> thing. In the hyperreals, +0 and -0 are the same, but there are
> infinitesimals which are different, and signed. I don't know enough about
> the surreals to comment. In matrix maths, there are an infinite number of
> different matrices where all the elements are zero -- they are all
> distinct, different, zeroes.

What do you even mean by that? By "matrix maths," do you just mean
matrices whose elements are reals, or something else?

--
Erik Max Francis && && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
My heart is pure as the driven slush.

Erik Max Francis, Jun 25, 2006
9. ### Steven D'ApranoGuest

On Sat, 24 Jun 2006 21:15:17 -0700, Erik Max Francis wrote:

> Steven D'Aprano wrote:
>
>> In mathematics, well, maybe... certainly in the Real number system, there
>> is no difference, and +0 and -0 are just two ways of writing the same
>> thing. In the hyperreals, +0 and -0 are the same, but there are
>> infinitesimals which are different, and signed. I don't know enough about
>> the surreals to comment. In matrix maths, there are an infinite number of
>> different matrices where all the elements are zero -- they are all
>> distinct, different, zeroes.

>
> What do you even mean by that? By "matrix maths," do you just mean
> matrices whose elements are reals, or something else?

Given any matrix M, there is a matrix Z such that M+Z = M. That matrix Z
is equivalent to zero in the reals, where x+0 = x.

In the reals, there is only one "zero", 0.

In matrices, there are an infinite number of "zeroes":

1x1 matrix: [0]
1x2 matrix: [0 0]
1x3 matrix: [0 0 0]
2x2 matrix:
[0 0]
[0 0]

etc. It is true that none of these are exactly equivalent to +0 and -0,
but my point was that there can be more than one distinct zero in pure
mathematics, and there is nothing wrong with the concept of a system with
more than one distinct zero.

--
Steven

Steven D'Aprano, Jun 25, 2006
10. ### Erik Max FrancisGuest

Steven D'Aprano wrote:

>> Steven D'Aprano wrote:
>>
>>> In matrix maths, there are an infinite number of
>>> different matrices where all the elements are zero -- they are all
>>> distinct, different, zeroes.

>
>> What do you even mean by that? By "matrix maths," do you just mean
>> matrices whose elements are reals, or something else?

>
> Given any matrix M, there is a matrix Z such that M+Z = M. That matrix Z
> is equivalent to zero in the reals, where x+0 = x.

Ah, of course. I knew this, I just misinterpreted your "distinct,
different, zeroes" as referring to the elements but not the matrices.
Just a misunderstanding.

Every zero matrix is an additive identity, and there are an infinite
number of them. That's certainly true.

--
Erik Max Francis && && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
There's a reason why we / Keep chasing morning
-- Sandra St. Victor

Erik Max Francis, Jun 25, 2006
11. ### Filip WasilewskiGuest

Steven D'Aprano wrote:
> On Sat, 24 Jun 2006 05:36:17 -0700, Filip Wasilewski wrote:
>
> > Steven D'Aprano wrote:
> >> On Fri, 23 Jun 2006 02:17:39 -0700, Filip Wasilewski wrote:
> >>
> >> > wrote:
> >> >
> >> >> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].
> >> >
> >> > Hooray! Logically there is no such thing as positive or negative zero,
> >> > or did I miss something in the primary?
> >>
> >> No, not in the primary, or even in the secondary, but possibly in the
> >> tertiary.
> >>
> >> For many purposes, it doesn't make sense to distinguish +0 from -0. But
> >> for other purposes, it does.

> >
> > I believe you will agree that this is mostly a matter of notation of
> > some fact rather than really trying to sign zero.

>
> No.
>
> In floating point, +0 *really is* different from -0 -- the two floats
> have different bit patterns. Floating point libraries must be specifically
> programmed to ignore that difference, making zero treated differently than
> other floats. That's -- usually -- a good thing.

So does the fact that the "-0.0" and "+0.0" strings can be converted by
C compilator to floats and stored with different bit patterns means
that they can behave much different in computation and comparison than
real numbers written in an expression on a piece of paper? Rather not.
Floating point is only a (very useful) finite precision model (there
are others) of real (rational) numbers and these differences are
implementation details for me.

And as far as the division by signed floating point zero and similar
operations in IEEE 754 are concerned, again, this is only a model in
which 0.0 means a value that is either equal to real zero or greater,
but too small to be distinguished from zero using finite amount of bits
(similarly -0.0), so the expressions like
1.0/-0.0 -> -1.#INF
1.0/0.0 -> 1.#INF
have some justification, although being meaningless in ordinary
arithmetic.

To be not so off-topic, I also expect from Python's integer, float and
Decimal computer formats to behave as much similar to their equivalents
known from math lessons as possible, which in particular means '=='
equality of -0 and 0, -0.0 and 0.0, and Decimal("-0") and Decimal("0"),
no matter what.

> In mathematics, well, maybe... certainly in the Real number system, there
> is no difference, and +0 and -0 are just two ways of writing the same
> thing. In the hyperreals, +0 and -0 are the same, but there are
> infinitesimals which are different, and signed. I don't know enough about
> the surreals to comment. In matrix maths, there are an infinite number of
> different matrices where all the elements are zero -- they are all
> distinct, different, zeroes.
>
> And in "the real world", all numbers are an abstraction anyway, so I'm
> not too worried about correspondence to reality. If mathematicians find a
> use for distinguishing +0 from -0 (as the statistical physicists have
> done) they will treat them as "really" different. If they don't, they
> won't.

As far as math stuff is concerned I will make it short. You can't deny

I also wouldn't mix symbols of physics states or others with ordinary
math expressions as this is only a matter of notation of some concept
(feel free to use any symbol you want).

Thanks for stimulating my neural cells, cheers,
fw

Filip Wasilewski, Jun 26, 2006
12. ### Antoon PardonGuest

On 2006-06-23, Filip Wasilewski <> wrote:
> wrote:
>
>> Logically, I should be able to enter x[-2:-0] to get the last and next to last characters. However, since Python doesn't distinguish between positive and negative zero, this doesn't work. Instead, I have to enter x[-2:].

>
> Hooray! Logically there is no such thing as positive or negative zero,
> or did I miss something in the primary?
>
> PS. x[len(x)-2 : len(x)-0]

This seems to defeat the purpose of allowing negative indexes. My
understanding was that negative indexes were introduced to avoid
things like seq[len(seq) - i]

--
Antoon Pardon

Antoon Pardon, Jun 26, 2006