← Back to context

Comment by IshKebab

1 year ago

Those people are wrong. Maths is wrong too but it doesn't matter so much for maths because you can hand wave syntax to do whatever you want. Guess what style of indexing formal mathematical proof systems use...

Fundamentally either work, but 1-based indexing is a mistake mainly because it leads to much less elegant code.

A simple example is accessing rows in a flattened matrix (or 2D array).

With 0-based intervals and right-open intervals it is

  array[stride*y .. stride*y + width]

With 1-based it is

  array[stride*(y-1)+1 .. stride*(y-1)+1+width]

Ouch. Code dealing with intervals is simpler with 0-based indexing & right-open intervals 99.9% of the time.

Take a look at the example code here:

https://www.lua.org/pil/11.2.html

What index does the flattened `mt` start at?

I think you're still conflating two different concepts, but this is mostly natural at this point since I assume you program in languages that use 0-based indexing. 1-based indexing focuses on the conceptual sequence rather than the underlying implementation details. Even your matrix flattening example is conflating concepts, although that's "normal" at this point. 0-based indexing certainly lends itself nicely to interval arithmetic, I agree.

> What index does the flattened `mt` start at?

I'm not sure if I missed a point you were getting at with this? Lua array table indexes start at 1.

  • My first boss who started in the late 60's said 1 based indexing exists because the early programmers with mathematics training thought ordinary people are too stupid to get zero based indexing.

    • Yeah I think that's the only plausible reason. Quite patronising but I think it probably came from an honest "make programming more accessible to non-programmers" place. They just forgot that the rest of programming exists...

      You see that in books and papers and tutorials a lot. They'll explain some basic concepts nicely and then realise that they can't really explain ELI5 calculus and skip straight into Laplace transforms.

      I once read a highly technical manual for a SystemVerilog simulator that began by explaining what double-clicking was. No joke. That's basically 1-based indexing.

  • > I'm not sure if I missed a point you were getting at with this? Lua array table indexes start at 1.

    The code example is

        mt = {}          -- create the matrix
        for i=1,N do
          for j=1,M do
            mt[i*M + j] = 0
          end
        end
    

    What index do I use to access the first element in mt? It isn't 1!

    If this was written using 0-based indices then the answer would be zero.