6 ms·
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 yo
by InsideOutSanta 2y ago
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<3; $i++) {
echo($array[$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.
- sim7c00 2y agoif 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.
- InsideOutSanta 2y agoYes, it's the offset from a pointer in some languages. But to the user, it's presented as an index, not an offset. In normal syntax, you don't access an array value using a construct like *(arraypointer+offset) If you did that, then using 0 as the start offset would make intuitive sense. In fact, if not for this technical reason, I doubt that any programming language would have 0-based indices.
- sim7c00 2y agoim not debating the user level stuff. honestly i think there's fair argument for not letting users get bogged down in system level details. but in my opinion, if people dont understand this why, its a useful thing to learn. its only a small detail in the end. i think, because software is built up abstractions over abstractions, its unavoidable such details creep into languages from the depths of the system. its not too long ago people didnt even have C or such high level constructs. so coming from there and building up its logical. Now, making something new, from above, one might put 1 as an index for the first element, but its likely during writing of a language you'll end up in the depths and come up with 0 anyway. think of how a 1 index will work vs a 0 index on ptrs. (pseudo code) ptr += index x size_of_object ptr += (index-1) x size_of_object if you wanna run that on the cpu, either compiler needs to come up with the first one or it will have an additional sub or decrement? or do you want the compiler to have a duty of doing the index-1, it will result in the original code again, also possible. it will take extra compiler or interpreter time. (or am i missing something completely?)
- InsideOutSanta 2y agoI don't disagree with anything you say. I'm not saying that C should have 1-based indices, for example. I'm only saying that from a language design perspective, ignoring technical limitations and historical precedents, 1-based indices would be preferable.
- School-Cotton 2y ago0 is logically superior regardless of how any of it is implemented under the hood. It just makes more sense.
- School-Cotton 2y agoYou are just citing cultural factors like how we happen to do things in the English language, not any logical argument.
- InsideOutSanta 2y agoAre there any languages that call the first element the zeroth element in everyday speech? I can't think of one, Google can't come up with one, and neither can ChatGPT. This isn't just cultural; it's universal or almost universal.
- nl 2y agoNot zeroth but ground floor vs 1st floor in buildings is common.
- InsideOutSanta 2y agoThat'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. There's a floor for every number, unlike with 1-based floor numbering, where floors go from -1 to 1.
- bloak 2y ago"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 FLOOR 13! If there's one thing worse than 1-based indexing it's leaving-out-13-based indexing ... Has any programming language tried doing that with arrays, I wonder?
- 34679 2y agoIt's only weird in certain cultures. For example, it's common in Germany to start counting at zero. "Null, eins, zwei.."
- InsideOutSanta 2y agoDas erste Element in dieser Aufzählung ist jedoch null, und das zweite ist eins. Deutsche verwenden einsbasierte Indizes, genau wie die Amerikaner, auch wenn sie manchmal bei null zu zählen beginnen. (The first element in this list, however, is zero, and the second is one. Germans use one-based indices, just like Americans, even though they sometimes start counting from zero.)
- adrian_b 2y agoYour arguments using "first" and "second" are invalid, because those words have nothing to do with numbers, like also "last". If you argue based on English, you should point only to the ordinals "third", "fourth" and the like. In the older European languages, the initial position in a sequence was named using words like "first", which mean "closest to the front". The next position was named with words meaning "the other", e.g. "alter" in Latin (the old European languages had 2 distinct words for "the other from two" and "another one from many", e.g. "alter/alius" in Latin). However when the need for naming other positions in a sequence besides the first, the second, the last or the next to the last (penultimate) has appeared, then the ordinals derived from numbers have been invented, like third, fourth etc. In later Latin, the word meaning "the other" has been replaced with a word meaning "the following". This has been inherited in the Romance languages and it has been borrowed in English as "second". Also in Old English, "other" had been used for "second", before "second" was taken from French.
- InsideOutSanta 2y ago> Your arguments using "first" and "second" are invalid, > because those words have nothing to do with numbers "First" is the ordinal number corresponding to the number one, while "second" is the ordinal number corresponding to the number two. You can represent "first" as 1st and "second" as 2nd. I believe the origins of these two words do not significantly impact my argument.
- adrian_b 2y agoNothing in the word "first" indicates that it corresponds to "one". The same for "second". Those words are not numerals, "first" is a superlative adjective, while "second" is an active participle. They are perceived as ordinal numerals only because English does not have ordinal numerals for the 2 initial positions of a sequence and "first" and "second" are used instead of the missing ordinal numerals. The abbreviations "1st" and "2nd" are very recent and they cannot be used as an argument that there is a long tradition of correspondence between "first" and "one". Like I have said, the correct argument based on English is that the position after the second is called "third", which is derived from "three", and the next position is called "fourth" from "4", so extrapolating backwards that sequence results in decrementing "3", which gives a correspondence between "2" and "second", and decrementing the number once more gives a correspondence between "1" and "first". This is the exact reasoning that has lead to the abbreviations "1st" and "2nd", which have no relationship with the pronunciation or the meaning of the abbreviated words, which mean "closest to the front" and "the following", meanings that are unrelated to any numbers.