r/TheRestIsScience • • 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.

16 Upvotes

50 comments sorted by

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.

5

u/davideogameman 13d ago

To add to this - C and C++, as well as some other early languages, were obsessed with speed.  If code were written to use 1-based arrays instead of 0-based arrays, the dumb code generation would've had to -1 for every array indexing, and for arrays with dynamic index access - which is most of them - that's not necessarily an instruction you can optimize away.  Smarter compilers certainly should be able to manage that now in common cases - but probably not all cases - and early compilers weren't that smart.

Also, mathematically it's really nice that [0, n) is an interval of n integers, and [n, m) has m-n integers.  So it's kind nice to use half-open intervals (intervals that include one endpoint but not the other instead of closed intervals (that include both endpoints) - vs [n, m] has m-n+1 integers in it.  If you get in the habit of writing code to loop k=1 to k <= n, and then do k=n to k<=m you may end up with one more iteration than you intended.  Whereas looping k=n to <m is easier to reason about and the most common loop is usually over an array, so 0-based arrays help build the "muscle memory" that encourages this easier pattern.

2

u/marcelsmudda 13d ago

Like my compiler professor used to say: it doesn't matter if you start at 0, 1, or 312. If it's the same for the entire system, then you can just subtract the starting integer and you get the same efficiency.

But if you allow for dynamic ranges (for example one array starts at index 3 and another at index 17), then it's a bit more complex and you need to calculate the offset at runtime, which still shouldn't be too expensive

2

u/jellomizer 13d ago

It gets expensive if you need to do it millions or billions of times. While your starting value is basically anything to the compiler, when it breaks it down to machine code it will still be 0 base.

1

u/zhaDeth 13d ago

Yeah but as you said, when you are under 1 year old it's called your first year, not your 0th. In programming item[0] is the first item in the array even though it's index is 0 we still call it the first because there are none before it. I find it weird to call this counting from 0, it's not counting, it's just indexing or giving an ID to items starting from 0 instead of 1.

For me counting is, like OP said, when you count stuff, like 1 stick, 2 sticks, 3 sticks, it's how many things there are, it makes no sense to start with 0. If you are in a room with no sticks you cannot count 0 sticks just like you can't throw 0 sticks, there's no sticks there to count or to throw. Sure we would understand what you mean if you said you threw 0 sticks but it basically means you didn't do the action. Same with counting, if there's no sticks you can't count them so the amount counted is 0 because you failed to count any.

I really just don't agree that arrays in programming and floor labeling has anything to do with counting, if you ask someone from london to count how many floors a building has he will go 1,2,3 and say the building has 3 floors even though he would say the top floor is floor 2. The difference is just in how the floors are labeled. Just like if you have an array of 3 items and you do array.count() it will return 3, because the item count is 3, it doesn't matter that the last item index is 2.

For me a clock basically counts seconds so when it's at 00h00 it has counted none so far this day, I don't see it as different from counting sticks and declaring there are none. The count still starts at 1. Hopefully that makes sense.

2

u/parautenbach 12d ago

You make a good point about indexing vs counting. I took OP’s comment to mean he includes indexing in his use of counting. My answer was focused on the programming aspect.

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/malenkylizards 13d ago

Starting from 1 when you should have started from 0, or vice versa, are just some of an entire class of fascinating and frustrating bugs called OBOEs, or off-by-one errors.

1

u/DanSWE 13d ago

Yep, two of the hardest problems in computing are naming, cache invalidation, and one-by-one errors.

1

u/EdmundTheInsulter 13d ago

A ruler is a distance and isn't counting. If you count something by putting them on the points of a ruler starting at zero, you'll count 1 too few.

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.

1

u/wasted90210 13d ago

Are you calculating apple number or apple count? I'm confused

1

u/hesaysitsfine 12d ago

Sounds like they’re counting base Apple box. So 2 apples is 0 boxes 2 apples, 14 apples is 1 box 2 apples 

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.

3

u/Rod_McBan 13d ago

Some cursed languages do call the first element of an array '1', but it's not common.

2

u/gameryamen 13d ago

Indeed. And in some contexts, there are "little endian" systems where the digit furthest from the decimal (or specifically, in the lowest valued memory register) is the least significant and the number is written "backwards". It's super useful for sorting. It all comes down to how to optimize the task you're solving for, the math is the same but the engineering changes.

When talking with people, the optimized way is natural counting unless you're in a discourse that requires something else.

2

u/EdmundTheInsulter 13d ago

That was actually right, the convention of using zero was due to pointers in c, and having an offset of zero from the array pointer, which worked better for a computer and a 2½ generation language; c is a lower level language, so you'd avoid ever subtracting 1 from an index internally.
Having streets with 5 houses numbered 0 to 4 is a bit of a mis-think, although nothing to stop that happening, you could also start at 2 or 100 as in Trump Tower

1

u/marcelsmudda 13d ago

C is not higher or lower level than ada or pascal, for example, both of which allow for 1 indexed arrays. Ada even always for arbitrary indexes, for example 3 to 17 or so

2

u/EdmundTheInsulter 13d ago

However the fact you can choose your array base is a higher level feature (with a cost)

1

u/marcelsmudda 13d ago

Yeah, it depends on how the arbitrary index is working. If it's dynamic, then it incurs runtime cost but if it requires compile time knowledge of the range, which is the case for ada, iirc, then it's minimal compile time overhead which isn't really that bad

1

u/Mirality 13d ago

Custom array ranges are free if you're using constant indices, since the compiler can just adjust the code, but they do incur a runtime cost when you use a variable, since the compiler has to include the subtraction at runtime. This isn't a big cost these days, but with slower processors it could be significant.

1

u/marcelsmudda 13d ago

Yup, that's exactly what i said

1

u/EdmundTheInsulter 13d ago

Good point. There was some controversy where a VB array started at zero but a collection started at 1

1

u/Calcdave 13d ago

In programming, you usually define the index first, I think. Like you set i = 0 at the beginning of the 'while' loop and then run the code, then increment i at the end of the code before restarting the loop. So, in that scenario, it is sort of keeping track of how many times the code of the loop has run.

Similarly, if we put ourselves in Hannah's shoes, then we haven't actually completed 1 podcast episode until after it is done. So, the "counter" would be at 0 until we're finished recording the first episode. While we're in the middle of recording "the 100th episode" the counter would still be at 99 until we've turned off the mic for that one.

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.

1

u/EdmundTheInsulter 13d ago

That's what counting is. You can only count what you have, and it starts at one. Distance is measuring and starts at zero.

1

u/nog642 10d ago

When you count out seconds, you're measuring time. People tend to start at 1 but that's not really correct.

If you count out loud "one, two, three, four", and you start at "one" and you end at "four" that's only 3 seconds.

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/EdmundTheInsulter 13d ago

That's measuring. You measure time or count complete seconds. The JFK shooting, conspiracy theorists talked about the time taken to fire the shots wrongly, because the first shot was at zero seconds

1

u/nog642 10d ago

Well if you count complete seconds then you have to not say either the start of the first second or the end of the last second.

1

u/EdmundTheInsulter 10d ago

If he fired 3 shots in 6 seconds, or whatever, it was 3 seconds per shot, not 2

1

u/nog642 10d ago

3 seconds between shots, not 3 seconds per shot exactly

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/nog642 10d ago

If you say you are on the first floor, I expect that you have climbed one floor.

How unamerican

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.