|
|
|
|
|
by 0-_-0
2798 days ago
|
|
Well for one, most situations I can think of when an index is calculated a 0-based index is more useful. Addressing an element T[h][w][c] in a linearized 3D tensor with 0-based indexing: T[h*W*C+w*C+c]
with 1-based indexing: T[(h-1)*W*C+(w-1)*C+c]
Or let's say you want to take a string "abc" and repeat it until the length is 10, getting "abcabcabca". With 0-based indexing: a,b = "abc", [" "]*10
for i in range(0,10):
b[i]=a[i%3]
In a 1-based language that's: for i in range(1,11):
b[i]=a[(i-1)%3+1]
Here's what Dijkstra has to say about it:http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EW... |
|
>with 1-based indexing: T[(h-1) * W * C+(w-1) * C+c]
Your example showing how cumbersome and ugly it is to subtract 1 -- isn't how the intended audience uses mathematics software. Instead, they would use idiomatic multidimensional access with commas or multi-brackets syntax that understands 1-based indexes. They do not manually multiply and add the offsets into a raw 1-dimensional array. (E.g. idiomatic T[x,y] or T[x][y] instead of contrived T[(x-1) * W+(y-1)])
It's also the same concept for MS Excel. In VBA macro programming code, one doesn't need to litter the code with "minus 1" everywhere. The 1st row and 1st column (cell A1) is addressed as "1,1" in the Visual Basic function "Worksheets!Sheet1.Cells(1, 1)"
Mathematics software has a tradition of 1-based indexing probably because many (most?) equations in written mathematics are 1-based. (Examples [2] [3].) It doesn't seem so unreasonable that scientists prefer that their math programming code has 1-based indexes to match the 1-based indexes in traditional math notation.
[1] https://news.ycombinator.com/item?id=11072438
[2] average : summation symbol ∑ has "i=1..n" not "i=0..n-1": https://en.wikipedia.org/wiki/Average#Arithmetic_mean
[3] linear algebra matrices : i and j subscripts begin at 1,1 and not 0,0: https://en.wikipedia.org/wiki/Matrix_(mathematics)