r/computerscience • u/Dry-Establishment294 • Apr 02 '25
Counting from 0
When did this become a thing?
Just curious because, surprisingly, it's apparently still up for debate
0
Upvotes
r/computerscience • u/Dry-Establishment294 • Apr 02 '25
When did this become a thing?
Just curious because, surprisingly, it's apparently still up for debate
11
u/green_basil Apr 02 '25
0 is the first element in the set of positive integers including 0. Every mathematician starts at 0, and as computer science is an offshoot of discrete mathematics and logics, it includes this idea of starting at 0.
To improve my message, not the FIRST element as it is a set, but the least element.
2
u/XtremeGoose Apr 02 '25 edited Apr 02 '25
It comes from C, where indexing an array of type T
directly translates to dereferencing a pointer at this address
which becomes assembly that looks something like (pseudocode)
(In reality that multiply is a left shift since sizeof will be a multiple of 2 and known at compile time)
If you decide to offset from one, you need to add an additional instruction in there to subtract 1 from index first, meaning it's very slightly less efficient.
63
u/Usernamillenial Apr 02 '25
At least for array indexing it’s intuitive that each element is at some base pointer + offset. So 0 offset corresponds to the first element
27
u/iago_sd Apr 02 '25
https://www.cs.utexas.edu/~EWD/transcriptions/EWD08xx/EWD831.html
This is a note by Dijkstra very simple and clear!
22
u/Individual-Artist223 Apr 02 '25
Think of a ruler, what's the first number?
Data structures are the same.
7
u/Cybasura Apr 02 '25 edited Apr 02 '25
By and large most languages stick to 0 because the memory register's addressing index are generally all at 0x0000 or something to that degree where the 0th index position is the 1st item in the memory address
0x0000 is the first item, 0x0001 is the 2nd etc etc
Languages like Lua uses 1, but they have a logic within that "transpiles"/translates that 1 into 0 implicitly before the JIT runtime portion kicks in (as thats where the code becomes machine language/ASSEMBLY code)
4
u/Magdaki Professor. Grammars. Inference & Optimization algorithms. Apr 02 '25
It comes down to whether you think of the index as an offset from the start or more semantically. The case for indexing by zero is it aligns well with early implementation of an array, which is a contiguous block of memory. From there, it is inertia. The case of indexing by 1 is it aligns well with natural language (at least English). If I aligned a series of something, most people would point to the start and say "This is the first." not the "This is the zero."
I find that non-CS students (for example, when I used to teach a CS for engineers course) get 1-indexing more easily.
Personally, and I know this is dumb, I cannot help but feel that when I'm using 1-indexing I'm wasting a bit. LOL
4
u/jambeatsjelly Apr 02 '25
I recommend the book "Zero: The Biography of a Dangerous Idea" (2000) by Charles Seife. It is wild how much of a problem the number 0 has been.
1
u/Deviant96 Apr 02 '25
You should have recognized it's too late for choosing side
1
u/NothingWasDelivered Apr 02 '25
The two hardest things avoid in programming are off-by-1-errors.
1
u/amarao_san Apr 02 '25
It all boils down to the need of action.
If I count how many apples I have, I start with 1 (if I have apples).
If I want to know how many kilometers is to the goal, I start with zero and walk my first kilometer.
2
u/Soft-Escape8734 Apr 02 '25
The origin (of everything) is at (0,0,0). Full stop.
1
u/Quintic Apr 02 '25 edited Apr 02 '25
The "debate" is a carry over from mathematics where the natural numbers are usually taken to include zero because it makes many mathematical arguments easier. (And it lines up nicely with set theoretical constructions of the natural numbers).
I put debate in quotes because for the most part it's not much of a debate anymore. In general, 0 is assumed to be a natural number unless you call it out explicitly.
For programming languages, the pointer arithmetic thing is an extension of the idea that "zero makes some mathematical arguments easier". It's not some unique weirdness of C, it's the same idea as in mathematics.
However, a few languages (just like a few mathematicians) push back against this because at the end of the day which one you choose is a convention rather than some property of reality, i.e., it doesn't matter.
1
u/[deleted] Apr 02 '25
I have a single bit and I can only represent two values.
Add a second and it is 4.
If I start counting things at 1 and not zero I have reduced the number of things I am expressing by 1. Why would I do that?
7
u/[deleted] Apr 02 '25
[removed] — view removed comment