In Lua, what is #INF and #IND?

后端 未结 3 588
庸人自扰
庸人自扰 2021-02-06 07:22

I\'m fairly new to Lua. While testing I discovered #INF/#IND. However, I can\'t find a good reference that explains it.

What are #INF

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-06 07:49

    @YuHao has already pointed out what means +/-1.#INF (+-inf) and -1.#IND (nan), so I will just add how to deal with it (which I just needed to) in Lua:

    • "inf" (+/- 1.#INF) are the higher number values that (Lua/C) can represent and the language provides that constant for you: "math.huge". So you can test a number inside Lua for +-INF; the function "isINF()" below shows how to use it.
    • "nan" (- 1.#IND) is something that can not be handled numerically: it should be a number, its not, and anything you do with it is anything but a number also. with that in mind remember that no NaN is equal to other NaN; check for NaN like the function "isNAN()" below.

    local function isINF(value)
      return value == math.huge or value == -math.huge
    end
    
    local function isNAN(value)
      return value ~= value
    end
    

提交回复
热议问题