My favorite example of this confusion is JS and (original) Java dates: > new Date() Wed Aug 24 2022 ... > new Date().getFullYear() 2022 > new Date().getMonth() 7 > new Date().getDate() 24 So 2022-08-24 comes out as (2022, 7, 24). One-based indexing for the year and day, zero-based indexing for the month.
Why do arrays start at 0?
271–280 of 702 posts
Re: Why do arrays start at 0?
#272It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. The 0 makes a lot of sense in a C pointer world where memcpy and other alike functions can be written very thight. The 1 makes a lot of sense in a human world, when we count, we start at 1, we talk about the "1st", counting on finger starts with 1, etc. I once were at a Lua (1 indexed language) conference where this was disc…
> The 1 makes a lot of sense in a human world, when we count, we start at 1 As a kid I learned "one one-thousand, two one-thousand, three one-thousand" when counting time out loud, but at some point I realized this was incorrect. The prefix is the start of the nth second but it isn't complete yet, so for example stopping in the middle of saying "two one-thousand" you actually haven't reached two seconds yet. My fix w…
Re: Why do arrays start at 0?
#273To write 4 numbers (including zero) you only need two bits
0: 00
1: 01
2: 10
3: 11
To write 4 numbers if you avoid using the number zero, you need three bits 1: 001
2: 010
3: 011
4: 100
If you extrapolate that a little bit, you'll realize that you'll need two bytes (1 Byte + 1 bit from another byte) to store the indices of an array with 2^8 elements, which is just dumb.Some of you might be thinking: you don't need to store it the same way it's written, you can just substract 1 from whatever the user typed and convert it behind the scenes.
Yes, you could subtract 1, but then you would be making the whole system more complex, opaque, and unelegant, while hiding information from the programmer for no good reason.
Re: Why do arrays start at 0?
#274I don't quite understand the argument "0-based being easier for pointer arithmetic is nonsense because the language doesn't have pointers". Whether or not the language presents the concept of "pointer" to the user is independent of whether or not it uses pointers internally. And if it exposes arrays as a concept, it has to implement them somehow. The simplest possible implementation of arrays is having a start addres…
A pointer is just one kind of array-like indexing scheme. Good pointery languages will distinguish Address from Offset from Integer.
Re: Why do arrays start at 0?
#275It really comes down to a choice between a machine-focused (0) or human-focused (1) approach. The 0 makes a lot of sense in a C pointer world where memcpy and other alike functions can be written very thight. The 1 makes a lot of sense in a human world, when we count, we start at 1, we talk about the "1st", counting on finger starts with 1, etc. I once were at a Lua (1 indexed language) conference where this was disc…
The problem it that most people, even programmers, don't understand how computer arithmetic works and have fantasies of mathematical number lines that the hardware only partially simulates. You see this consistently in the post-Java crowd who think that unsigned integers are some sort of unholy aberration because the languages they've grown up went further to maintain the fictional number line semantics.
Re: Why do arrays start at 0?
#276Re: Why do arrays start at 0?
#277Earlier quoted context omitted.
A disadvantage comes to mind. While the following loop works as expected: for (size_t i = 0; i The following causes an unsigned integer underflow and is an infinite loop: for (size_t i = length - 1; i >= 0; i--) ...
As Jens Gustedt points out[1], the following intentional unsigned overflow works perfectly for downwards iteration (even when length is 0 or SIZE_MAX), though it looks a bit confusing at first: for (size_t i = length - 1; i You are also free to start at any other (not necessarily in-bounds) index, just like with ascending iteration. [1] https://gustedt.wordpress.com/2013/07/15/a-praise-of-size_t-...
why doesn't that loop end instantly?
I mean length - 1 Or does it only terminate when the number underflows? Terribly confused here
Re: Why do arrays start at 0?
#278I don't quite understand the argument "0-based being easier for pointer arithmetic is nonsense because the language doesn't have pointers". Whether or not the language presents the concept of "pointer" to the user is independent of whether or not it uses pointers internally. And if it exposes arrays as a concept, it has to implement them somehow. The simplest possible implementation of arrays is having a start addres…
Re: Why do arrays start at 0?
#279I'm fine with 0-based, 1-based or anything-based arrays (I recall it being convenient solving 8queen with pascal), but for Rage-Over-A-Lost-Penny sake, music note intervals are always beyond my understanding. Same pitched notes are called "interval 1" and there goes thirds, fifths, sevenths... All off-by-one in my base-offset-addressing mind... And then major vs. minor which creates all kinds of "aliased addresses"..…
Re: Why do arrays start at 0?
#280My favorite example of this confusion is JS and (original) Java dates: > new Date() Wed Aug 24 2022 ... > new Date().getFullYear() 2022 > new Date().getMonth() 7 > new Date().getDate() 24 So 2022-08-24 comes out as (2022, 7, 24). One-based indexing for the year and day, zero-based indexing for the month.
That comes from C’s standard library (and presumably from somewhere else before that). Classic bad design, took a very long time for people to figure out it was unhelpful and dangerous.
tm_mday: The day of the month, in the range 1 to 31.
tm_mon: The number of months since January, in the range 0 to 11.
I wonder where C got it? It goes back to at least 1973's V4: https://github.com/dspinellis/unix-history-repo/commit/92779...