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.
Why do arrays start at 0?
651–660 of 702 posts
Re: Why do arrays start at 0?
#652Re: Why do arrays start at 0?
#653Earlier 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…
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?
#654Earlier 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.
Re: Why do arrays start at 0?
#655Earlier 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.
Re: Why do arrays start at 0?
#656Earlier 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.
Re: Why do arrays start at 0?
#657Earlier 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).
Re: Why do arrays start at 0?
#658Earlier 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.
Re: Why do arrays start at 0?
#659Like 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?
#660Earlier 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…
No, you misunderstood. You subtract one from the pointer at allocation time. You don’t add an instruction every time you access.