Live data from Hacker News

Numbering should start at zero (1982)

cs.utexas.edu

61–70 of 309 posts

Re: Numbering should start at zero (1982)

#62
post #27

Earlier quoted context omitted.

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

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

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

Re: Numbering should start at zero (1982)

#63

Earlier quoted context omitted.

For math too, 0-based indexing is superior. When taking sub-matrices (blocks), with 1-based indexing you have to deal with + 1 and - 1 terms for the element indices. E.g. the third size-4 block of a 16x16 matrix begins at (3-1)*4+1 in 1-based indexing, at 2*4 in 0-based indexing (where the 2 is naturally the 0-indexed block index). Also, the origin is at 0, not at 1. If you begin at 1, you've already moved some dista…

In mathematics, if it matters what index your matrix starts on then you're likely doing something wrong. Besides, in the rare cases where it does matter you're free to pick whichever is convenient.

[deleted]

Re: Numbering should start at zero (1982)

#64

1-based numbering is nonsense. How many years old are you when you’re born? I notice almost all defenses of 1-based indexing are purely based on arbitrary cultural factors or historical conventions, e.g. “that’s how it’s done in math”, rather than logical arguments.

So if you go into a bar and order a beer, you drink the zeroth beer before the first?

Re: Numbering should start at zero (1982)

#66

Numbering should start at π (2025) (umars.edu) Seriously, it all depends on whether u're counting the items themselves (1-based) or the spaces btwn them (0-based). The former uses natural numbers, while the latter uses non-negative integers For instance, when dealing with memory words, do u address the word itself or its starting location (the first byte)? The same consideration applies to coordinate systems: r u pos…

Zero is a natural number. It is in the axioms of Peano arithmetic, and any other definition is just teachers choosing a taxonomy that best fits their lesson.

[deleted]

Re: Numbering should start at zero (1982)

#67

1 or 0-based index... I recently picked up Lua for a toy project and I got to say that decades of training with 0-based indexes makes it hard for me to write correct lua code on the first try. I suppose 1-based index is more logical, but decades of programming languages choosing 0-based index is hard to ignore.

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.

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

Re: Numbering should start at zero (1982)

#68

Earlier quoted context omitted.

> I suppose 1-based index is more logical Why?

It's interesting how most people find learning 0-based indexes confusing, but after a few years of programming, they don't even notice how odd it is. How do you number things in real life? If you have two options, do you number them "option 0" and "option 1" or "option 1" and "option 2"? If you create a presentation with numbered bullet points, do you start numbering them from 0 or 1? 1. This is my first point 2. Thi…

if you see how its implemented in machine code (on most modern archs) you will see how it is logical. once you see that, you cannot unsee it.

people call these things an index, but really it is an offset from the base of the array. not an index into it. hence, base + 0 is the first entry. as the offset to that entry is 0. thats how it will work in generated machine code on all the machines i saw (i did not see all of them obviously).

i think people struggle with these concepts because they never bother to see whats under the hood. a bit of assumption ofc on my part i cant see into ppls minds nor do i know all architectures. x86 and x64 (amd/intel) definitely work like this.

Re: Numbering should start at zero (1982)

#69

Earlier quoted context omitted.

> I suppose 1-based index is more logical Why?

It's interesting how most people find learning 0-based indexes confusing, but after a few years of programming, they don't even notice how odd it is. How do you number things in real life? If you have two options, do you number them "option 0" and "option 1" or "option 1" and "option 2"? If you create a presentation with numbered bullet points, do you start numbering them from 0 or 1? 1. This is my first point 2. Thi…

It's only weird in certain cultures. For example, it's common in Germany to start counting at zero.

"Null, eins, zwei.."

Post reply on HN