4 ms·
Arrays are indexed starting at 1. I asked about this and Jeff said it just depends which types of problems end up having to use +1 or -1. I said anything mod n
by phkahler 12y ago
Arrays are indexed starting at 1. I asked about this and Jeff said it just depends which types of problems end up having to use +1 or -1. I said anything mod n is now going to have the +1 and he actually paused for quite a while. I know it's an unsolvable debate, but I thought indexing from 1 went out with Fortran. I also would have thought the math folks would prefer to start at zero.
- RogerL 12y agoDon't forget matlab starts at 1. R starts at 1. Mathematica starts at 1 by default, though you can override that for any given array. 1-based indexing is very common in mathematical software.
- stan_rogers 12y agoIndeed, zero-based indexing is sort of a ridiculous concession to the hardware (whether that be data/address line labelling by power of two or offsets from a memory location) if bit-twiddling isn't your goal. (Even binary operations don't necessarily imply bit-twiddling at the conceptual level, even if that's what's going on under the hood.) We've just become rather used to counting by calling the first thing zero (and somehow have managed to forget that we had to learn that).
- Ntrails 12y agoHaving started in Matlab I still regularly forget that indexes start at zero in c# and end up with errors. I feel like either option is just catering to user preference at this point, as I don't feel there's a conclusive winner logically.
- one-more-minute 12y agoFor me it's less about saving characters overall and more about making things as simple as possible in the basic case. I'd much rather have +1 scattered throughout my code (though I tend not to) than have to do -1 in my head at the repl, personally.