48 points · by thunk · 17y ago · 71 comments
It has two C "intrinsics" called vec_mule() and vec_mulo(). These take in two vectors of integer datatypes (char, short) for multiplication and produces one vector with items that are twice as big. char becomes short, short becomes long. To make room for these larger datatypes it only multiplies the even (vec_mule) or the odd (vec_mulo) items.
It's a nice construction, until you start to wonder which elements are even. Is "element 0" even, or is the "first element" odd? Should I really have to wonder about that?
I have never questioned the utter coolness of starting at index 0, until I encountered this. I do not know why we start at 0 if the rest of the world starts at 1, but it seems silly and without reason.
1 sheep -> the number of sheep present when there is one
0 sheep -> the number of sheep present when there are none
Because you normally only enumerate things that are there it seems like you are starting at '1', but really you start relative to the situation where there are 'none'.
You can't have 'negative' presence of anything so it took a while longer to realize that there might be negative numbers too.
As for element 0 being odd or even, it's even.
So, it isn't silly and it has a reason.
0 is one of the biggest inventions in the history of mankind, programmers have given it it's natural place because we could not make our computers work if we didn't do that.
Imagine binary logic based on '1' and '2'....
index = position, label
The count is the number of occupied slots and is totally independent of the positions or labels.
Otherwise how would you count the number of elements in a sparse matrix ?
Yes, they do, when they have no elements. But if an array has an element 0, it actually has 1 element, not 0. Whereas if I have a sheep 1, I have 1 sheep. There is no sheep 0.
I think it would be good to note that I have nothing against 0 as a number. It's useful for many purposes including to indicate that a set actually has 0 elements, but I'm not so sure it's such a good idea to actually call the first element element 0.
int * a = (int * )malloc(5);
a is a pointer to the beginning of an integer (default 4 byte) array. a[i] == a + i == ((uint8_t * )a)+i*sizeof(int). Therefore, a[0] == a + 0 == the first element in a contiguous block of memory.
My mind has been blown.
*("abcdefg" + 3) == 'd'He arguments that option A is the least ugly one, but forgets that computers have maximum numbers.
According to Dijkstra, you should test if a variable is a valid integer like: 0 <= var < MaxInt + 1. The best possible outcome of such a test would be a compiler error.
His argument that sequences defined as min <= i < (max + 1) because (max + 1) - min = total number of items is just silly. Maybe true for maths, not so for programmers. When reading code, you want to know if the code is valid, not how many items there are in a list. And reading i <= Max instead of i < (max + 1) is simpler.
Secondly, his article is about 0 or 1 based arrays, not random selections. And in case of 1 based arrays, the max = total number of elements.
So option C is best.
About whether 0 or 1 based arrays are better. They both have their uses. 0 based for spatial coordinates, 1 based for lists and character positions.
2) You frequently want to know the length of a subseq, and you frequently want successive subseq ranges to dovetail.
3) What in the world does rand() have to do with this?
2) Yes, but that has nothing to do with anything I wrote. Also calculating the length of a subset out of a 0 or 1 based array is the same, making your point completely irrelevant.
3) Where in the world do you read anything about rand()?
You really need to give up one of the possibilities, or use an extra bit.
2) If you test the upper bound inclusively, then the upper bound of the previous subseq is not the lower bound of the next subseq.
3) I misinterpreted GP. Scratch that :)
"0 based for spatial coordinates, 1 based for lists and character positions."
Sums up my thoughts pretty well.
The real reason we do zero-based counting in computers is because of bits, speed and laziness.
Oh, and obviously 0 is even. Why? Because 0 mod 2 = 0, 0 is evenly divided by 2, and that's what "even" means. If you need more intuition, though: 1 is indisputably odd, and even and odd numbers alternate, so 0 is even. The rest is philosophy -- you can probably find definitions for "odd" and "even" where 0 is a problem. That's fine, but those don't help. Ignore them. Mathematically there's no problem whatsoever.
The trickiness only comes if you insist in thinking in terms of "the first element", rather than "element number 0". Some people use "zeroth", but this seems to invite more confusion because it induces two meanings for all the other ordinal forms -- if there's a zeroth element, does that mean the "first" element is in fact the second element? Best to avoid ordinals altogether -- you usually don't need any more than "first" and "last" anyway.
Yes, I did. Does it hurt to offer a different perspective? I feel it doesn't. Approaching a subject with an open mind or from a different angle tends to be good for discussion. Offering a contradicting opinion or observation helps everyone to understand an issue better.
> Oh, and obviously 0 is even.
I might counter this with a similar question you asked me: "did you read my post?" I didn't make a case that 0 was odd, I asked how I should look at it: as element 0, or as the first element. If you look at it as element 0, the first element of the vector is an even element. If, on the other hand, you look at it as the first element, you will think it is an odd element. This is the case I made, not that 0 is even.
No, I was questioning whether you had considered his arguments at all, given the "why do we start counting at 0" question which he tried to answer.
> I didn't make a case that 0 was odd, I asked how I should look at it: as element 0, or as the first element.
You can look at it either way, as both are correct.
> If you look at it as element 0, the first element of the vector is an even element. If, on the other hand, you look at it as the first element, you will think it is an odd element.
Ah, I see your point now. To me this wouldn't be a question because everyone knows programmers start counting at 0 -- a machine instruction would operate on indexes, not ordinals. "The first element" is the element with index 0. It is not "element number one", or if you do want to see it that way, "number one" is the ordinal you use when you start counting on your fingers, which is something we deliberately ignore.
Ah, yes. No, I wasn't trying to disprove him. I just gave a different approach to the issue. My mind is not made up on this subject. I've been a programmer for too long: 0 is burned in my fingers and my mind.
> a machine instruction would operate on indexes, not ordinals
The interesting aspect isn't the difference between indexes and ordinals, but the difference between indexes and cardinals. The cardinals match the ordinals for normal people. For normal people, indexes have no meaning.
But the difference between indexes and cardinals aren't that easy for programmers either. In an example presented by Dijkstra a young programmer used 0 in everyday language as a cardinal replacing 1. Indexes have little meaning in real life, but cardinals do.
Good response, BTW. Thanks. It's interesting to link the concepts of ordinals and cardinals to the discussion.
element 0, aka the 0th element..
Apart from FP people - they need "the first" and "the rest" ;)
To be honest, I think one reason why this grew is probably the way C handles array indexing. a[0] = *(a + 0) and this symmetry is a nice property to have in many cases. I think I use that symmetry in my MSc project a lot more than I use vec_mule/vec_mulo.
(Yeah, I'm not of a fixed mind in this matter :-))
Learning that such article could exist (it DOES exist), and reading it was a shocking experience to me.
'A second-year was "quite convinced" that zero was odd, on the basis that "it is the first number you count"'
Sounds like a bit of an odd statement to me.
Not withstanding Dijkstra's logical arguments, I have adopted starting at 1 for 2 reasons:
1. I don't have to think about "shifting" data in order to look at it in the third language.
2. I don't have to "think" at all. It's intuitive that the first element is "1".
I just insert a null in the first position of any array in a 0-starting language.
Reason 1 has more force. I don't run into it very often when transferring data from one language to another -- I don't do very much of that -- but I do when, e.g., translating between code and standard mathematical notation.
i see a lot about code being written for humans, but i'm not onboard with it. elegance in how it executes is #1, and elegance of maintenance is #2.
Now I'm not agreeing about the particular indexing argument put forth by edw519, I think zero indexes lead to cleaner code whenever you have to do math with them, but unless you can make an honest case for maximum performance, I have to disagree with your assertion about elegance.
You see, the backend of our project is in Coldfusion which has 1 based indexes for Array's and it has led to head-butting screen confusion when requesting the current index from the server. Then the visual side is always 1 based when showing data to the end-user, I'm always thinking I'm the problem in the middle.
There have been numerous occasions where QA have called me to ask why the first slide in a slideshow is called "Slide 0"!
All they told us was "because that's how things are. [MEMORIZE IT!]"
Perhaps I should read more of Dijkstra's writings.
Edit: I love when directory indexing is NOT forbidden. http://www.cs.utexas.edu/~EWD/ewd08xx/
Maybe it is too late, but I don't understand this sentence.
A sequence starting at the smallest natural number would be: 0, 1, 2. The variant Dijkstra is arguing about is: 0 <= i <= 2. How can the latter be unnatural? And what does he mean with "shrunk"?
[0,1) = (0)
[0,0) = ()
[0,0] = (0)
[0,x] = ()
Probably you'd say x=-1 but if you're using unsigned indices, then you can't distinguish the biggest possible sequence from the empty one (this is true of all schemes, actually, but at least the other don't require negative numbers).Computers were made to serve us and as such they should translate all their inner thoughts to more human consumable data.
Five is 5, not 101.
So no, numbering should start at ONE, even if most programmers have already been hardwired to start counting from zero.
We could still achieve the same effect if we started indexing at 1, but it would require more code, and it would be less clear.
Really?
How about dealing with strings?
"123456789" how many chars we have? 9
char[1] = 1
char[2] = 2
:
char[9] = 9
Now, let's try the C approach:
"123456789"
char[0] = 1
char[1] = 2
:
char[8] = 9
where is char "1"? at zero index!
how many elements we have?
last index plus one!
see? there is already confusion in place, or as you say, more code and less clear...
ch[0] = 0;
ch[1] = 1;
ch[2] = 2;
:
ch[9] = 9;
ch[1] = 0;
ch[2] = 1;
ch[3] = 2;
....When we start indexing from 0, the only time we need to do a +/- 1 is when we need to index the last element. All other times (iterating, mapping) we don't need to.
Also, starting indices at 1 would change the idiomatic iteration to:
for (int i = 1; i <= SIZE; ++i)
I find that the <= and >= operators add more to my cognitive load than the strict relation operators < and > because there's an or in there. I don't find the alternative to that idiom any better: for (int i = 1; i < SIZE + 1; ++i)
Your original argument was that we should make it easy for humans to understand, not computers. I think that starting indices from 0 is easier for humans to understand because it simplifies the code we must write and read.Read your post and understand the same can be said of using one-based indexing.
No, the only way there's no lump in the carpet is his way.
[1] thumb
[2] index
[3] middle
[4] ring
[5] pinkie
so we have a simple range [1..5]
which can be represented in so many ways in computer programs:
for i=1 to 5 print finger[i]
for(i in [1..5]) print finger[i]
my first finger[1] is my thumb
my last finger[5] is my pinkie
how many fingers we have? as many as the last index in the list: 5
-
now, C programmers like to count this way:
for(i=0;i<5;i++) print finger[i]
where finger[0] is thumb
and finger[4] is pinkie
how many fingers we have?
as many as the last index in the list plus one: 4+1
how human-like!
And that's why you get so messy when trying to get the string position of a substring:
if(pos>-1) exists, since pos=0 means the substring is in the starting position
again, how human-like!
But how dare I argue with C programmers without being burned at the stake?
4+1=5 never enters into it. What a rubbish argument. I might as well complain that your [a,b] has (b-a+1) integers in it. (5-0)=5 so the half open interval has 5 things in it.
What's the measure of a real interval 1<=x<=5? 4. 0<=x<5? 5.
We go from 1 to 5, so the math would be 5-1 +1
You go from 0 to 4, the math would be the same, 4-0 +1
Using 5 as your upper bound, then starting from 0 to 4 is the same as me using 6 as my upper bound then going from 1 to 5.
for(i in [1..5]) print finger[i]
You're using an inclusive upper range here. Not that that's necessarily wrong, but it isn't standard for the reason Dijkstra mentioned (how many fingers do I have? 5 - 1 = 4. Oops.)
Upper-Lower+1, easy. Whenever the lower is 1, just the upper is enough.
In your case you go from 0 to 4, how's that different from how many fingers you have? 4-0=4 Oops.