Live data from Hacker News

Numbering should start at zero (1982)

cs.utexas.edu

151–160 of 309 posts

Re: Numbering should start at zero (1982)

#151
post #16
post #6

Earlier quoted context omitted.

Zero-based counting works better with modular arithmetic. Like arr[(i++ % arr.length)] = foo; Is certainly nicer than the equivalent in one-based subscripting arr[(i++ % arr.length) + 1] = foo; (The above is actually wrong, which helps the idea) I'll concede that it's not all that significant as a difference, but at least IMO it's nicer. Also could argue that modular arithmetic and zero-based indexing makes more sens…

But on the other hand, last := vector[vector-length(vector)]; is nicer than last := vector[vector-length(vector) - 1]; so in the end I'd say 'de gustibus non est disputandum' and people who prefer 0-based indexing can use most languages and I can dream of my own.

That's arguably one of the only downsides of zero-based, and can be handled easily with negative indexing. Basically all indexing arithmetic is easier with zero-based.

Re: Numbering should start at zero (1982)

#152

In France, street-level is the 0th floor, and the one above is the first floor. You see zero in elevators all the time.

Same in Germany, just that we usually call it ground floor instead of 0th floor.

You could argue it's a bit of a translation error. The French and German words for floor are referring to ways to add platforms above ground. Either by referring to walls, wooden columns or floor joists. Over the course of language evolution those words have both broadened and specialized, referring to building levels in general. But the way they are counted still reflects that they originally refer to levels built above ground. The English "floor" on the other hand counts the number of levels that are ground-like, which naturally starts at the actual ground.

Re: Numbering should start at zero (1982)

#153
post #147

Earlier quoted context omitted.

That's a good example! Elevators sometimes have a 0 for the ground floor, but they often have an "E" in German-speaking countries or a "C" in English-speaking countries. In this example, people also call the floor with index 1 the "first floor," although they don't call the ground floor the zeroth floor, as you say. Since floors can go below ground, the first underground floor is floor -1, so everything works out. Th…

"C"? It's more likely to be "G" round here. By the way, has anyone else had this problem: you're on floor 10, say, and you want to get to floor 15, say, so you run up 5 flights of stairs and try to find the room but then after a while you realise that somehow you've ended up on floor 16 so you think you're going demented and can't count but after this has happened a few times you realise ... THE IDIOTS HAVE OMITTED F…

> It's more likely to be "G" round here

I see what you did there.

Somebody should create a programming language that implements all real-world number idiosyncracies. Don't have 4 or 13; define π as 3 and τ as 6, print 6 three times every time it occurs for good luck (or bad luck, depending on where you live), replace all numbers close to 8 with 8 in money values for great prosperity, have a constant for "dozen" that's 13 in case you're counting loaves of bread, have a rounding function that rounds to a close number that's easy to say depending on your locale...

And let's go with 0.5-based indexing as a compromise.

Re: Numbering should start at zero (1982)

#154
post #103

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…

I totally disagree, but it's only my opinion and probably not scientific at all. From a logical point of view I think it's totally unnatural to start at 1. You have 10 diffferent "chars" available in the decimal system. Starting at 1 mostly leads to counting up to 10. But 10 is already the next "iteration". How do you explain a kid, who's learning arithmetics, that the decimal system is based on 10 numbers and at the…

I think it's totally natural to start counting at 1, because you start with one of something, not zero. How do you explain to a kid that although they're counting objects, the first one is labelled zero, and that when they've counted 10 objects, they use the number 9?

Re: Numbering should start at zero (1982)

#155

Perhaps ideally we'd change English to count the "first" entry in a sequence as the "zeroth" item, but the path dependency and the effort required to do that is rather large to say the least. At least we're not stuck with the Roman "inclusive counting" system that included one extra number in ranges* so that e.g. weeks have "8" days and Sunday is two days before Monday since Monday is itself included in the count. *…

Who knew those bodybuilders were such history buffs?

Re: Numbering should start at zero (1982)

#156
post #96

I see people bringing up arrays, and an array index is represented by a number, you can do math on it, but it's not a regular number for counting a sequence of items. It's a unique reference to a location in the memory, and it's dangerous to treat an array index like it's just any old number. Behold, the really stupid things you can do in Javascript: let myArr = []; let index = 0; myArr[--index] = 5; console.log(myAr…

An index is a specific kind of number, but so is a count. Indexing should clearly start from 0. It leads to far more elegant code and lower risk of off-by-one mistakes.

Then why do the scientific computing languages start at 1? Fortran started at 1 before C was invented.

Re: Numbering should start at zero (1982)

#157
post #3

The 1980s were not a particularly enlightened time for programming language design; and Dijkstra's opinions seem to carry extra weight mainly because his name has a certain shock and awe factor. It isn't usual for me to agree with the mathematical convention for notations, but the 1st element of a sequence being denoted with a "1" just seems obviously superior. I'm sure there is a culture that counts their first fing…

0-based indexing aligns better with how memory actually works, and is therefore more performant, all things being equal. Assuming `a` is the address of the beginning of the array, the 0-based indexing on the left is equivalent to the memory access on the right (I'm using C syntax here): a[0] == *(a + 0) a[1] == *(a + 1) a[2] == *(a + 2) ... a[i] == *(a + i) For 1-based indexing: a[1] == *(a + 1 - 1) a[2] == *(a + 2 -…

But then again Fortran proceeded C, is known for being very performant, and is 1-based by default.

Re: Numbering should start at zero (1982)

#158

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…

Just speaking anecdotally, I had the impression that math people prefer 1-based indexing. I've heard that Matlab is 1-based because it was written by math majors, rather than CS majors.

Indeed. I was going to point out that mathematicians choose the index based on whatever is convenient for their problem. It could begin at -3, 2, or whatever. I've never heard a mathematician complain that another mathematician is using the "wrong" index. That's something only programmers seem to do.

Re: Numbering should start at zero (1982)

#160
Always beware the word should. I agree with Dijkstra's logic in the context that he presents it, but there are other contexts where I don't think it applies.

Personally, I find that in compiler writing, which is the only programming I do these days, the only things I use indexes for are line numbers and character offsets into strings. Calling the first character the zeroth character is ridiculous to me, so I just store a leading 0 byte in all strings and then can use one based indexing with no performance hit. Alternatively, since I am the compiler writer, I could just internally store the pointer to the string - 1 to avoid the If you are often directly working with array indices, you are likely doing low level programming. It is worth asking if the task at hand requires that, or if you would be better off using higher level constructs and/or a higher level language. Low level details ideally should not leak into the application level.

Post reply on HN