In Lua, what are #INF and #IND?

I am new to Lua. During testing, I discovered #INF / #IND . However, I cannot find a good link that explains this.

What is #INF , #IND , etc. (e.g. negatives) and how do you generate and use them?

+6
source share
3 answers

#INF infinite, #IND is NaN. Give him a test:

 print(1/0) print(0/0) 

The output on my windows machine is:

 1.#INF -1.#IND 

Since there is no standard view for ANSI C, you might get a different result. For instance:

 inf -nan 
+7
source

The @YuHao extension is already a good answer.

Lua does little when converting a number to a string, since it relies heavily on the implementation of the C base library. In fact, the Lua print implementation calls Lua tostring , which in turn (after a number of other calls) uses lua_number2str macro , which is defined in terms C sprintf . So, at the end, you see any view for infinities and NaNs that the C implementation uses (this may vary depending on which compiler was used to compile Lua and what C environment your application is associated with).

+6
source

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

  • "inf" (+/- 1. # INF) are the higher values ​​that can be represented (Lua / C), and the language provides this constant for you: "math.huge". That way you can test the number inside Lua for + -INF; the "isINF ()" function below shows how to use it.
  • "nan" (- 1. # IND) is something that cannot be processed numerically: it must be a number, it does not exist, and all you do with it is something like a number. Bearing in mind that no NaN is equal to other NaN; check for NaN as 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 

+2
source

Source: https://habr.com/ru/post/954988/


All Articles