lua-users home
lua-l archive

Infinity as index

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi,
I don't know whether this was thought through in any way, but lua defines
+inf and -inf as well as not-a-number.
 Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio
 > return math.huge
 inf
 > return 1/0
 inf
 > return 0/0
 -nan
Of course, we can negate it as well (except for nan)
 > return -math.huge
 -inf
 > return -1/0
 -inf
 > return -0/0
 -nan
Now my point is that we can use this as indices. My intuition tells me the
following:
 -nan - behaves like a nil as index or throws error (number expected)
 inf - behaves like the end of a sequence as index
 -inf - behaves like the beginning of a sequence as index
I guess -inf and nan behave as expected, because of checks like
 if (init < 1) init = 1;
in lstrlib.c, but inf does not:
 > return string.find("asd", "a", 1/0)
 1 1
Thus, inf behaves like "nil" or "0". This is against my intuition. What is
your opinion?
best regards,
meisterluk
[0] https://twitter.com/meisterluk/status/457946586778464257
[1] A patch has to provided somewhere between lstrlib.c in function
str_find_aux line 582 and lapi.c in function lua_tointegerx line 349.
Somehow I only received a "1" from optinteger for the infinity value. This
got me confused.

AltStyle によって変換されたページ (->オリジナル) /