Live data from Hacker News

Numbering should start at zero (1982)

cs.utexas.edu

51–60 of 309 posts

Re: Numbering should start at zero (1982)

#51
post #39
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.

> 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)… ;-) Meanwhile, the workaround is to use -1 through -5 to index from the end of the array.

[deleted]

Re: Numbering should start at zero (1982)

#52
post #17

I appreciate Dijkstra's arguments, but the fact remains that no non-technical user is ever going to jibe with a zero-indexed system, no matter the technical merits. Languages aimed at casual audiences (e.g. scripting languages like Lua) should maybe just provide two different ways of indexing into arrays: an `offset` method that's zero-indexed, and an `item` method that's one-indexed. Let users pick, in a way that's…

There is no good reason to cater to non-technical users when designing programming languages. In what way is Lua “aimed at casual audiences”?

A lot of Lua users are kids playing Luanti or Roblox or WoW who also spend a little time modding them—mostly editing textures or 3-D model meshes, but also scripting. Lua is a small and simple language which can be learned easily, prefers to produce incorrect answers instead of throwing exceptions when confronted with ambiguous situations (for example, permitting undeclared variables), has an interactive REPL, is memory-safe to avoid crashes, and uses dynamic typing (thus avoiding type declarations) and has garbage collection, as well as using 1-based indexing.

All of these design features seem to be helpful to casual programmers and are common in languages and programming environments designed for them, such as BASIC, Smalltalk, sh, Python, Tcl, and Microsoft Excel.

pansa2's comment https://news.ycombinator.com/item?id=43435736 also has a citation to Ierusalemschy, who said in https://old.reddit.com/r/lua/comments/w8wgqb/complete_interv...:

> And at that time, the only other option would be Tcl, "tickle.” But we figured out that Tcl was not easy for nonprogrammers to use. And Lua, since the beginning, was designed for technical people, but not professional programmers only.In the beginning, the typical users of Lua, were civil engineers, geologists, people with some technical background, but not professional programmers. And "Tcl" was really, really difficult for a non-programmer. All those substitutions, all those notions, etc. So we decided to create a language because we actually needed it.

(Tcl, of course, was designed for chip designers.)

Re: Numbering should start at zero (1982)

#53
Pretty much any algorithm that involves mul/div/mod operations on array indexes will naturally use 0-based indexes (i.e. if using 1-based indexes they will have to be converted to/from 0-based to make the math work).

To me this is a far more compelling argument for 0-based indexes than anything I've seen in favor of 1-based indexes.

Re: Numbering should start at zero (1982)

#54

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 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. This is my second point
It would be odd to have your first point be point number 0 and your second point be point number 1, wouldn't it?

Outside of programming, even most programmers use zero-based indexing only when they're making a programming joke.

Zero-based indices create odd effects even in programming, although we don't really notice them anymore.

Consider:

    $array := ['a','b','c']
    
    for($i:=0; $i
$array has 3 entries, but your for-loop says you should stop iterating before you reach 3. This isn't really consistent with how we intuitively understand numbers to work outside of programming.

Re: Numbering should start at zero (1982)

#55

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.

> How many years old are you when you’re born?

You have lived zero full years and are in the first year of your life. In most (but not all) countries the former is considered "your age".

That's consistent with both zero-based and one-based indexing. Both agree on cardinal numbers (an array [1, 2] has length 2), just not on ordinal numbers (whether the 1 in that array is the "first" or "zeroth" element).

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

I think it's largely a matter of taste in either direction. But, I'd raise this challenge:

    arr = ['A', 'B', 'C', 'D', 'E', 'F']
    slice = arr[3:1:-1]
    print(slice)
If you're unfamiliar with Python (zero-based, half-open ranges exclusive of end), that's taking a slice from index 3 to index 1, backwards (step -1). How quickly can you intuit what it'll print?

Personally I feel like I have to go through a few steps of reasoning to reach the right answer - even despite having almost exclusively used languages with 0-based indexing. If Python were instead to use MatLab-style indexing (one-based, inclusive ranges), I could immediately say ['C', 'B', 'A'].

Re: Numbering should start at zero (1982)

#56
post #27

Earlier quoted context omitted.

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.

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.

Re: Numbering should start at zero (1982)

#57

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.

Re: Numbering should start at zero (1982)

#58

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.

> 1-based numbering is nonsense. How many years old are you when you’re born?

Typically 3 months shy of 1 year, so about 0.75.

Re: Numbering should start at zero (1982)

#59

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.

Yes but I think it might be just habit, and it's exactly in matlab that dealing with for loops over sub matrices is so annoying due to this

Re: Numbering should start at zero (1982)

#60

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…

The center of the debate is that outside of pure mathematics numbers and number systems can only be signifiers for some physical or conceptual object. It is the signified object that determines the meaning of the number and the semantics of the mathematics.
Post reply on HN