4 ms·
Lua was originally created to be used by a group of FORTRAN programmers. Lua starts with 1 because FORTRAN starts with 1. FORTRAN starts with 1 because Math s
by corysama 6y ago
Lua was originally created to be used by a group of FORTRAN programmers. Lua starts with 1 because FORTRAN starts with 1. FORTRAN starts with 1 because Math starts with 1 (Sum of f(Xi) for i in 1 to N)
- tzs 6y agoSometimes math starts with 1 and sometimes with 0. I was once implementing some math algorithm whose description had various subscripted variables. Some ran from 0 to whatever, and some ran from 1 to whatever. I was implementing in C++, with each of these variables becoming an array. I didn't want to write var[i-1] all over the place for the ones that ran from 1 in the book--it was a tricky algorithm and for maximum clarity I wanted my code to read as close to the book as possible. What I did was overload the () operator to make it a 1-based array access operator. I.e., var(i) == var[i-1]. That worked out reasonably, although it probably would have been better just to make the arrays for the 1-based variables one longer and just ignore var[0]. In other words, if var in the text has subscripts that run from 1 through 10, make the var array in C++ 11 long, and just use var[1] through var[10].
- lordgrenville 6y agoSame story with Julia. Julia is 1-indexed because MATLAB is 1-indexed, and MATLAB is 1-indexed because maths is 1-indexed.