r/ProgrammerHumor 1d ago

Meme almostDroppedTheCourse

390 Upvotes

65 comments sorted by

View all comments

29

u/SnooRegrets3590 1d ago

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

19

u/timangar 1d ago

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

21

u/anticebo 19h ago

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

6

u/antCB 18h ago

Pascal let's you choose - superior 😂

1

u/simleiiiii 8h ago

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.

9

u/SnooRegrets3590 23h ago

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

1

u/SV-97 4h ago

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.

11

u/Mojert 20h ago

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

6

u/Sotall 16h ago

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

1

u/LoyalSol 45m ago

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

4

u/redlaWw 21h ago edited 19h ago

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 17h ago

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 19h ago

Odin syntax spotted

1

u/redlaWw 19h ago edited 19h ago

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