Live data from Hacker News

Why do arrays start at 0?

buttondown.email

651–660 of 702 posts

Re: Why do arrays start at 0?

#651

Earlier quoted context omitted.

“machine code” isn't really the zero point that's special nowadays. The code the programmer wrote is compiled to something such as LLVM IR, LLVM IR is further compiled by LLVM to Assembly, this is further compiled by an assembler into machine code, and then the c.p.u. further compiles this to it's internal code as it executes it. “machine code” really is no more special in this chain of events than, say, LLVM IR.

Machine code is special because its the only thing the CPU can actually execute.

It's slightly different - machine code is special because it's a visible API between our programming work and the CPU sillicon. Microcode is private implementation detail which you usually can't affect as a software developer. Machine code is something you are directly creating by writing things though.

Re: Why do arrays start at 0?

#652
Its an absolute pain in my arse, i honestly don't know what problems i would have with an array system that started at 1, but since the length method sometimes starts at 0 and sometimes 1, (I'm thinking Perl vs JS) I'm always forgetting whether to put = or <= into for loops and when to add one for the read out or subtract 1 when looking at the array again. I mean in what universe should [2]+[3]=7?

Re: Why do arrays start at 0?

#653
post #629

Earlier quoted context omitted.

Closed ranges (with both ends inclusive) are super annoying to work with. You can represent the empty range (unless you are willing to do [i : i-1]), and they don't compose like half open ones: [a : b) + [b : c) = [a : c).

For some reason I am thinking in the terms of closed ranges if not specified otherwise/doing it for myself. [i:i-1] is how i think of empty ranges, and [i:i] if I want to capture the element i with a range. Also [a:b] + [b+1:c] corresponds more what I want, than [a:b) + [b:c). I guess the majority of the people are not like this, but arguments like "just look at it how strange it looks" don't do it for me, because it…

I'm curious if you would also accept [i:i-2] as a an empty list, or in general anything where the right side is smaller than the left?

If I am working with closed ranges, [i:i-1] looks like the list [i, i-1]. Like [5:2] would be [5, 4, 3, 2].

With [b+1:c] I would feel like I needed to insert a check to ensure b+1 right as the empty list.

The issues with compositionality become even more noticeable with floats. Then you would need [a:b] + [b + minimum_float : c], or something like that.

Re: Why do arrays start at 0?

#654
post #553

Earlier quoted context omitted.

True, and it's a great example of how this whole drama is about a practical trade-off, not about a unique Right Answer. If you play piano, the second is the second finger; the fifth is the fifth finger, and it all makes sense. No problem. On the other hand trying to actually count that way (two thirds make a fifth and so forth) is maddening.

Clearly the solution is we need to start numbering fingers from 0.

Maybe that would do it. The only problem is that I'd have four fingers on each hand.

Re: Why do arrays start at 0?

#655
post #219

Earlier quoted context omitted.

The ground floor is 0. The floor above 0 is 1. The floor below 0 is -1. Anything else is crazy TBH, the whole point of integer numbers is to count things that start at a defined point and go opposite ways. Why shift it and then reinvent weird pseudo-negative numbers like S1? But then again people measure distance in feet and write dates as month day year :)

This is how it should be, but the only country in the world, AFAIK, that does it that way is Germany.

There’s some confusion here about the exact word being used. The German word that is mistranslated here as “floor” is “Stock”, which actually means “addition”. So the floor above the ground floor is the first addition, not the “first floor”.

Re: Why do arrays start at 0?

#656

Earlier quoted context omitted.

Are you sure it doesn't just look that way because you are used to monospaced fonts? Ask a kid to show you their zeroth finger.

Counting fingers (or other items) isn't a positional numeral system, so it's really apples to oranges. Also, "first" has a meaning of "nothing precedes it", which is separate from the indexing system", it shouldn't mean "at index 1", unless otherwise specified.

Though the idea of an index as a position instead of an address is weird to me, you can indeed make that analogy. The "first" element of an array has the index zero, as in "zero elements precede it".

Re: Why do arrays start at 0?

#657
post #475

Earlier quoted context omitted.

An empty set is countable; it has an empty mapping to the natural numbers. Its cardinality is zero.

And its ordinalitiy is also 0. Standard construction of ordinals is that each ordinal is the set of all its predecessors. (0 has no predecessors , hence 0 is the empty set.) (And so finite ordinals have the same ordinaliity as cardinality).

Show me a mathematical text where ‘ordinality’ is defined.

Re: Why do arrays start at 0?

#658
post #624

Earlier quoted context omitted.

But it will still execute with likely no extra time at all due to OOE and how fast arithmetics are.

If it is not on the hot path, it is likely free, but not guaranteed. If it is on the hot path then it is wasting a whole cycle. And of course in highly ALU-dependent code it is another instruction, so a fraction of a clock.

What do you mean it wastes a whole cycle? It may indeed have worse performance due to blowing the instruction cache, but I don’t see why would out-of-order execution be slower on the hot path - I doubt there would be too many hot paths without any dependence on memory fetches outside specific benchmarks - the memory loads will take significantly more time even if they hit cache.

Re: Why do arrays start at 0?

#659
An Array in Ada is interesting. https://learn.adacore.com/courses/intro-to-ada/chapters/arra...

Like other languages, it is a collection of contiguous elements that can be selected by indexing.

Unlike other languages:

- any discrete type can be used for indexing, not just integers

- bounds can be any value (doesn’t have to start at 0 or 1. First index could be 13)

- one consequence of the aforementioned is that arrays are types that map to the problem domain, rather than being tied to the computing domain

- and more…

Re: Why do arrays start at 0?

#660
post #503

Earlier quoted context omitted.

Integer add/subtract never really took a long time. Slower than today’s sub-nanosecond, and it depends on the computer, but there were CPUs in the mid 1960s doing millions of instructions per second. They weren’t crazy slow, they were just crazy expensive.

A million, perhaps. But you're talking about adding one instruction to every single data access. Plus your code bloats up with all those extra dec instructions. BCPL and C were created for departmental and lab computers like the pdp-9. Note, for example that the mighty 6502 succeeded in the market because its designers had heard from customers that the $100 price for the 6800 was too much, so they removed some instru…

> But you're talking about adding one instruction to every single data access.

No, you misunderstood. You subtract one from the pointer at allocation time. You don’t add an instruction every time you access.

Post reply on HN