Hacker News new | ask | show | jobs
by jasode 2797 days ago
Julia was developed by scientists that used math software like MATLAB. Most mathematics software uses 1-based indexing. (Previous comment about this.[1])

>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)

1 comments

My argument was that in situations where the difference between 0 and 1 based indexing makes a difference it's ususally 0-based indexing that leads to simpler code.
I guess I find it ironic then, that one of the most common programming errors, is of the "off-by-one" variety[1]. It is in part due to differences in convention and notation in various fields[2].

This isn't common in 1-based indexing languages. Very common in 0-based indexing languages[3].

If the 0-based indexing was objectively superior, wouldn't there be fewer instances of such bugs?

[1] https://en.wikipedia.org/wiki/Off-by-one_error

[2] https://en.wikipedia.org/wiki/Zero-based_numbering

[3] https://softwareengineering.stackexchange.com/questions/4289...

I was hoping to read some code analysis that supported your claim of off-by-one being more common in 0-based languages, but all I see in your [3] is a few different speculative reasons - am I missing something?

Off-by-one can show up in a bunch of ways that are agnostic to the underlying indexing, so it isn't obvious to me that there would be a substantial difference at all. Also, given what's said below about the ease of writing code which fails to consider custom indices, I'm not sure I believe just switching to 0-based or anything else in Julia is as painless as you suggested earlier - it could introduce its own off-by-one errors!

Agreed. Off-by-one often shows up when calculating indexes e.g. differences in indexes or offsets. These calculations are independent of the base of the index (0 or 1 or even N).
This implies that off by one is more common in zero based counting. Evidence? Your link didn't seem tob support that changing convention of indexing mattered. Rather, no longer indexing at all is what helped. (E.g., iterators.)

This fits my understanding. I've seen people do off by one just in the wild when counting how many posts/nails/etc are needed.

> simpler code

Have you used julia? You're almost never calculating your offset to a pointer. The VM does that for you. (As it does for python, ruby, erlang). Or actually in the case of julia it's more accurate to say the (extremely lazy ahead of time) compiler does it for you.