lua-users home
lua-l archive

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

Luiz Henrique de Figueiredo writes:
> > The manual should probably mention that NaN, like nil, is not an
> > acceptable index for setting values
> Lua does not depend in IEEE 754 and so does not mention NaN (of Inf)
> at all.

At the least, the Lua manual might say that type(x) == 'number' does
not imply that x can be stored as a table key.

lcode.c and ltable.c call the function luai_numisnan, which ltable.c
uses to raise the error "table index is NaN".  Lua does know of some
concept called "NaN".

However, luai_numisnan in turn is not defined in Lua's ANSI C sources
but rather in the system-dependent luaconf.h.  By default, luaconf.h
defines NaN as the number not equal to itself, which under IEEE 754
floating point is known as NaN as well.  However, you are free to
redefine Lua's concept of NaN to whatever you like--it's not limited
to IEEE semantics.

Note also:

  -- warning: system dependent (depends on definition in luaconf.h)
  local function isnan(x) return type(x) == 'number' and x ~= x end
  local nan = 0/0;
  local t = {}
  local mt = {}
  setmetatable(t, mt)
  function mt.__index(t, k)
    if isnan(k) then return 'baz' end
  print(t[nan]) -- > baz