4 ms·
I have a similar experience with pythons negative-indexing. In Python, you can access elements counting from the back by using negative numbers. But for this, t
by slightwinder 2y ago
I have a similar experience with pythons negative-indexing. In Python, you can access elements counting from the back by using negative numbers. But for this, they start with 1, not 0. Which is inconsistent, as they start for the normal forward indexing at 0. I guess it comes from reducing n.length-1 to -1, but it's still kinda annoying to have two different indexing-systems at work.
- pansa2 2y agoIt makes more sense if you think of indices as pointing between elements: 0 1 2 3 4 ----------------- | A | B | C | D | ----------------- -4 -3 -2 -1 -0 Except, of course, -0 doesn't exist. AFAIK that's why C# chose to add a special "index from end" operator, `^`, instead of using negative indices.
- Someone 2y ago> Except, of course, -0 doesn't exist. Not for integers on modern hardware. If only hardware used ones’ complement (https://en.wikipedia.org/wiki/Ones'_complement)… https://en.wikipedia.org/wiki/Ones'_complement)… ;-) Meanwhile, the workaround is to use -1 through -5 to index from the end of the array.
- kragen 2y agoThat makes sense; in the diagram of the 4-element array, 3 points at the same point as -1.
- deleted 2y ago[deleted]
- slightwinder 2y agoYour visualization makes sense if you always count them going from left to right. But with negative index you naturally count them going from right to left, from the last element backward to the first element. So -0 is the natural starting-point, except it's -1 in python.
- pansa2 2y ago> Your visualization makes sense if you always count them going from left to right. Yes - personally, I do always count them left-to-right. I don't think negative indices implies counting right-to-left. A negative step does, but I never use one because IMO it doesn't make sense to have an exclusive LHS and inclusive RHS.
- mixmastamyk 2y ago-1 is correct, there is no -0. The math works correctly the Python way.
- slightwinder 2y agoIndexing is not really math, it's a reference-system which is using numbers for convenience. You could use letters or even emojis to get the same result.
- mixmastamyk 2y ago[closed, open) intervals on the number line are well-known in math, as are operations on them. -0 is not, nor supported by hardware.
- slightwinder 2y agoYes, but Python is not math. This is a syntax-feature we are talking about, math is here just a tool, not the purpose. And it's also not running on hardware, but multiple layers higher.
- mixmastamyk 2y agoPython already has the best design (fewest tradeoffs) in this small area. You're proposing to break the expectations of millions and break offset math while you're at it. Not very compelling if you ask me.
- forgotpwd16 2y agoCan visualize it as wrapping to the sequence's other side. That is you start at elem 0 and going backwards to -1 gets you to the other side (up to -len(seq) that returns to elem 0). Kinda like border wrapping in most modern Snake variants. Although this is only for negative indexing.
- kibwen 2y agoI don't particularly like negative indexing, but if we assume that -0 represents the address of the first element past the end of the array (the logical "end" of the array's span in memory), then -1 is naturally the starting address of the last element in the array, as measured in its offset from the end.