3 ms·
> 1-based indexing was a mistake The most software I write, the more I am convinced that 1-based indexing is what most languages should be using, either the ex
by FridgeSeal 6y ago
> 1-based indexing was a mistake
The most software I write, the more I am convinced that 1-based indexing is what most languages should be using, either the exception being the very specific use-case of needing to track offsets by hand for some reason. 1-based indexing is so much easier to reason about, but zero based indexing is seen as “the one true way” due its ubiquity, and not because it’s actually better.
> and I have seen it emitting inefficient code in the PTX compiler.
Have you raised this with the devs? This seems like something worth raising as an issue.
- tgv 6y agoBoth approaches have their advantages, IMO. Inclusive ranges are usually easier to understand, but e.g. when picking a (uniformly distributed) random element from an array, nothing beats a[floor(rnd() * len(a)].
- dunefox 6y ago> nothing beats a[floor(rnd() * len(a)] What's wrong with rand(a)?
- socialdemocrat 6y agoHow is that very readable? In Julia, this will pick a random value from an array: rand([3, 4, 5, 1]) While this picks a value from a range: rand(1:10) Much more straightforward to read IMHO. Where I prefer 0-based index is when dealing with coordinate systems and memory locations.
- deleted 6y ago[deleted]
- vcxy 6y agoAlready mentioned, in julia just do rand(a). But if you didn't have that, couldn't you just switch out floor for ceil and have the exact equivalent for 1 based indexing?
- Sebb767 6y agoNo - rnd() can not return one, but can return zero, at which point you'd access index 0 even with ceil().
- vcxy 6y agoAh, thanks! I think I'll stick with rand(indexable_collection) in Julia since anything else just doesn't feel Julian, but I'll remember that for other 1-based languages if I come across it.
- xiphias2 6y agoTo tell you the truth, I don’t really care about 0 or 1 based indexing, there’s not much difference when I’m coding. At the same time if you look at TIOBE, 0 based indexing won. The biggest problem for me was porting numerical code from Ruby: the syntax is so similar to Julia, that I didn’t have much to do, but porting 0 based code to 1 based was the biggest work actually. I’m sure it’s painful to port code from R or Marlab if it’s 0 based, but that will need to happen anyways.