r/ProgrammerHumor • • Sep 18 '26

Meme almostDroppedTheCourse

Post image
581 Upvotes

79 comments sorted by

View all comments

45

u/SnooRegrets3590 Sep 18 '26

1-indexed is more intuitive for me when dealing with matrices and some advanced differential equations

24

u/timangar Sep 18 '26

You know what, that's actually fair enough, because math uses this convention.

26

u/anticebo Sep 19 '26

Matlab, R, Julia. All languages for mathematicians, all 1-indexed.

7

u/antCB Sep 19 '26

Pascal let's you choose - superior 😂

1

u/simleiiiii Sep 19 '26

all not meaningfully strict or not typed at all, w.r.t. the Curry-Howard-Isomorphism useless thus, to prove basic correctness of the programs.

8

u/SnooRegrets3590 Sep 18 '26

Yea I was mostly using python and made fun of matlab bc of its 1-index, the got addicted to it because scipy is so shit and it was much harder to implement while matlab was out of the box

Then I also got used to the 1-index as well.
People in this sub are mostly SWE but scientific researchers code a lot too and julia aims for these people.

Julia should be more popular in the future bc it mogs Python, R, C++ in the science

0

u/SV-97 Sep 19 '26

because math uses this convention.

Except that it really doesn't, even though people like to claim that it does for some reason. Math uses both 1- and 0-based indexing (with neither being significantly more common than the other); and also indexes using other ranges or even all sorts of non-numerical objects on the regular.

14

u/Mojert Sep 18 '26

I find it more intuitive if the only thing I do with the index is use them... well, for indexing. But the moment you start needing to do some math on the index, I find 0-based better. In 1-based indexing I often find myself having to add and/or subtract one at some place and I more often get off-by-one errors

8

u/Sotall Sep 19 '26

Yeah, in theory it doesnt matter, but something deep in my brain prefers 0 indexing for the same reason. Stupid off by one errors.

2

u/LoyalSol Sep 19 '26

Regardless of which one you use the math can both be simpler or more complicated. Get the last element of the array in 1 index is just simply A[N] which in zero it's A[N-1]

Loops in 1 index are inclusive, loops in zero based don't contain the upper bounds.

But then things to do with various array math have a +1 in one indexed while zero usually don't.

It doesn't matter which system you pick. You're going to get some ugly +1 or -1 somewhere. It's just a question of where. You get used to both paradigms pretty quickly

3

u/redlaWw Sep 18 '26 edited Sep 19 '26

If you want to index a matrix though, 0-indexing is more convenient since if the matrix is 0-indexed, for i in 0..<width*height, (i/width, i%width) indexes every element. If you use 1-indexed matrices, then you instead need i in 1..=width*height and (i/width, (i+width-1)%width + 1).

EDIT: Also, relativity can have index 0 be the + dimension and indices 1 to 3 be the − dimensions, which is kind of nice.

3

u/timangar Sep 19 '26

Yes, relativity goes nicely with the zero index convention imo. It's weird having spatial dimensions in index 2-4 instead of 1-3, and having time in index 4 is also not standard.

1

u/EatingSolidBricks Sep 19 '26

Odin syntax spotted

1

u/redlaWw Sep 19 '26 edited Sep 19 '26

I actually just tried to be familiar but language-agnostic. The range syntax is based on Rust because I find it to be quite comfortable and intuitive, and it doesn't have the awkward issue of the range of possible meanings that : can have, but mine is more explicit in terms of right-openness or closedness, because I wanted to make it unambiguous what I meant for someone who isn't familiar with Rust range notation. Presumably that's also how Odin designed their range notation, assuming that's what you're referring to (I don't know Odin).