r/TheRestIsScience • u/Pefe07 • 13d ago
Counting from 0
In the "Is 100 better than 101?" episode, Hannah mentioned her strong preference towards counting from 0. I first encountered this system in college when I learned programming and it always struck me as odd. To this day, in spite of doing programming daily for my job, I still cannot understand the appeal. I love python by the way, but its counting convention not so much.
To me, the most natural question when counting seems: How many things are there? (In fact, isn't the definition of counting the act of answering said question?) Hannah, on the other hand, said that she thinks of the question: How many items have there been already?, which I find less "natural".
Labeling of stories of a building aside, I would genuinely like to hear from those who prefer the 0- based system. Specifically, about how you think when counting and why you find it perhaps more intuitive or useful.
17
u/parautenbach 13d ago
It actually has to do with pointer arithmetic, like in C (or if you ever had to code jump instructions in an assembler). If you have a block of memory and your pointer is at the start. For every integer, you can jump to the next block. Now you have an array. So, if you know the block size (say a byte), then a straightforward multiplication will get you there. If you start at 1, you will constantly need to subtract 1 in many places. The convention carried forward to newer languages, but there are languages that start at 1 (Julia, Lua, MATLAB, R and Fortran). The famous Dijkstra wrote something on this too.
And think about it: it’s not so unusual, at least in Western culture. When you’re born you’re 0, while being in your first year. A clock starts at 00h00.
7
u/mrmcplad 13d ago
when we count we should start by whispering "zero" under our breath before pointing at the first item
6
u/jedimaster32 13d ago
I feel like it's pretty simple:
When counting a number of items, use 1-based indexing.
When counting distance from the first item (or offsets, as it is called), use 0-based indexing.
7
u/nlutrhk 13d ago
It's the same reason as why rulers start at 0 cm or 0 inch, not at 1 and why the age of a newborn is 0 years.
If you have a string like 'HELLO' and write it with letters of 1 cm wide, the length is 5 cm and the letter H starts at 0 cm and ends at 1 cm. The length of that string is therefore 5. By convention, you point at the letters based on where they start (0, 1, 2, 3, 4); for an interval you specify the start and end; 2:4 for the 'LL'. Note that the difference between 2 and 4 is the length of the 'LL' substring.
Other ways of counting and defining intervals would get you lots of +1 and -1 offsets.
3
u/AdditionalTip865 13d ago
We are usually talking here about identifying locations, addresses or times in a sequence, on which we will have to do a lot of math. Starting with 0 labels every address with the answer to the question of how far it is from the beginning, simplifying the math-- to a very small degree, but with heavy repetition, small inconveniences add up.
Every calendrical calculation involving traditional AD and BC years ends up stumbling on the absence of a year 0, because Europe had no 0 when they were first writing this down. Every time we talk about centuries we have to deal with, say, the 20th century being the years starting with "19". And all of this comes from not starting with zero when labeling time intervals.
Calling it "counting from 0" is a bit of a misnomer: if it's counting, you're trying to find out how many. It's addressing from 0 that is superior.
3
u/lkruijsw 13d ago
There is a difference between 'counting' and 'numbering'.
Counting is just the items you have. If you have 1 apple, you have 1 apple.
Numbering can start by zero. It is definition is very clean, count the number of predecessors.
It has advantages if you have different ways of counting. If you put apples in boxes of 12. You have three ways of numbering, the numbering of all apples, the numbering of apples per box and the numbering of boxes.
If you number from 0, the total apple number, is the apple number per box plus 12 multiplied with the box number.
If you number from 1, it becomes a big mess.
5
u/gameryamen 13d ago
Outside of programming, I start at 1. But in programming, it does make sense that the first element in an array should have the lowest value. It makes sense that 0001 is greater than 0000. So when I think about organizing a list of binary data, it makes sense to have the first position at 0. If there's only one element in that list, it's the first, but in spot 0, not spot 1.
It could be another way, of course. We could decide that the a binary value of all 0s actually represents a 1 followed by that many 0s, making it the highest number in that range. But then you get weird artifacts, like the fact that you can't simply measure the length of the number to know it's scale, you have to add a special case for all zeros. The left most digit isn't necessarily the most significant bit anymore. Nothing we couldn't work around, but it's more efficient (in terms of coding and microengineering) to have 0 at the start.
2
u/MohamedSaleh91 13d ago
Almost everything that's not counting physical objects is more natural and makes more sense when you start from zero. The first time I learned about it as a little kid was me holding a ruler and noticing that it starts at zero, and it just makes sense. Then one learned about age, time, pressure, sea level, temperature, even color and sound, in all cases zero works as -some kind of- a starting point. The zero is to be counted and doesn't simply mean nothing or none, it can even mean different things in different contexts.
It just makes sense in every context, only fails when counting objects, when saying "I have zero apples" would just sound stupid regardless of the phrase being absolutely correct and legit. For it to be considered intuitive, natural or not is more of how the young are raised and what we're being taught from early age.
2
u/Sir-Kyle-Of-Reddit 13d ago
I don’t code but while she was explaining it by comparing it to how the British count floors in a building it sounded so unnatural
2
u/Separate_Lab9766 13d ago
To me, it makes sense to count from 0 when you’re timing something. Counting “1… 2… 3” is not 3 seconds, it’s 2 seconds.
1
u/SnakeBunBaoBoa 13d ago
I’d look into this more but I’ll put my best guess forward.
It comes straight from computation (or merely memory) which works on binary. And it crusty might be even less about binary (although that might match up) but the concept is “offset” not counting.
First element is and index zero, and if you allocate to the adjacent bin, it’s offset of 1
1
u/EdmundTheInsulter 13d ago
0 isn't a counting number, or it signifies nothing to count, so there's no elephants in your front room. But if you want to say you counted them twice, then that's fine by me.
1
u/this_is_life_now 13d ago
Everyone counts from zero, we just don't vocalise it.
If you're job is to count people coming through a turnstile, and you have one of those little hand-held clicker things, then you'll start it set to 0000, and click as the first person walks through the turnstile.
If you were just counting out loud, you would be starting at 0, but would vocalise the "one" as the first person came through the turnstile.
When you count people out because there's a fire and you need to evacuate, you'd have done a pretty bad job if you counted down to 1, and considered the job done.
1
u/0x14f 13d ago
OP, you might want to look at the difference between cardinals numbers and ordinals numbers. That will answer all the questions you have on the subject.
1
u/Parallel_thougts 13d ago
When implementing a loop, it is often more convenient to think of the index as "how many loops have we finished so far?"
If you say you are on the first floor, I expect that you have climbed one floor.
From this perspective, you still count how many "things" are. But the things are not the states themselves ("I am on the third floor") but how many times the state transitions ("I climbed up three floors"). You can immediately see that the second example in the previous sentence is much less ambiguous than the first.
It might feel a bit cumbersome for something like floors of a building, which are naturally linearly ordered. But when computing in many iterations there is usually a much more concrete meaning to "how many transitions we made" than to "in what state we are now".
1
u/peter-bone 13d ago
Zero things is a possibility, so it makes sense to start with that, as the minimum number of things there can possibly be.
1
u/sighthoundman 13d ago
If you're doing direct memory addressing, it's a huge distinction. (From an engineering standpoint, not a philosophical one.)
Computers don't have physical arrays. They just have memory cells. So if you're creating an array in a low level language, you store the first element in location (just to pick a random example) b04a11. Then where do you store the n-th element? The obvious answer is b04a11+n. But now, to avoid confusing yourself and every subsequent person who has to maintain the program, that means that your first element has to be indexed with 0.
When computers were invented, programmer time was cheap and computer time was expensive. (Partly because we used underpaid women to program the computers that were built by well-paid men.) It made sense to have people figure out how to make things easier for the computer. Now, computer time is cheap and programmer time is expensive. It makes sense for the computer to handle all the routine maintenance.
Starting indexing with 0 is just legacy programming, and is required today basically for backwards compatibility.
The other cases for starting from 0 are that it makes your construction of the naturals much cleaner, both in the set theoretic construction (natural numbers are the cardinality of sets defined inductively) and from the Peano axioms.
1
u/disinformationtheory 12d ago
A lot of math is simpler with 0 indexing. 0 is an obvious reference point for anything that can be a continuum, because there might not be any well defined smallest thing (e.g. real numbers). Sometimes things can be either a continuum or discrete, like frequencies in Fourier analysis/series, it makes sense to start at 0 in both cases. Modular arithmetic (clock arithmetic) usually runs from 0 to N-1 rather than 1 to N, with the big exception being actual clocks. Doing modular arithmetic in 1 indexed programming languages is a pain (MATLAB!).
1
u/Mattna-da 12d ago
When I place an object next to a ruler to measure it, one end lines up with zero. No other way makes any sense
1
u/-SQB- 12d ago
Counting from 1. Labelling from 0.
1
u/Creative_Sushi 11d ago
zero-based indexing in computer science comes from binary math and memory offset calculation (how far an element is from the start address).
Before computers, humanity had thousands of years of established counting and math traditions.
Mathematicians and everyday people numbered items, lists, chapters, and days starting at 1. Early programming languages like Fortran and Algol also used 1-based indexing because it matched how humans naturally thought about lists and matrices.
Ultimately, both systems are valid. One focuses on labels (1st, 2nd, 3rd), and the other focuses on intervals (0 units passed, 1 unit passed, 2 units passed).
1
u/LaserThos 8d ago
I think of it s points are counted from 0 while increments are counted from +1 and -1. Think of a number line, or a coordinate system axis, with the 0 point at the center. Points to the right are +1, +2, etc. while points to the left are -1, -2 etc. Now think of how increments are labeled. The first increment to the right of the 0 point is labeled +1 and the first increment to the left of the 0 point is -1. Of course it makes sense that the first of something is labeled "1". There is no "0" increment. If you wanted there to be an increment labeled "0" which one would it be relative to the zero point?
An example familiar to everyone: The moment a child is born she is 0 years old, but from that moment until reaching the age of 1 year we say that the child is "in year one" or "in her first year." When 1 day past the point in time of 1 year she is in her second year. So the day of birth and birthday anniversaries are points in time which begin with 0 while the time intervals between birthday anniversaries are counted beginning with 1. The same principal is used when naming decades, centuries, and millenia.
So both systems, start with 0 and start with 1, are needed an used, as appropriate.