Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Lua, what is #INF and #IND?

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, #IND, and similar (such as negatives) and how do you generate and use them?

like image 758
Nathan Goings Avatar asked Oct 01 '13 03:10

Nathan Goings


2 Answers

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

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

Output on my Windows machine:

1.#INF
-1.#IND

As there's no standard representation for these in ANSI C, you may get different result. For instance:

inf
-nan
like image 97
Yu Hao Avatar answered Oct 03 '22 00:10

Yu Hao


Expanding @YuHao already good answer.

Lua does little when converting a number to a string, since it heavily relies on the underlying C library implementation. In fact Lua print implementation calls Lua tostring which in turn (after a series of other calls) uses the lua_number2str macro, which is defined in terms of C sprintf. Thus in the end you see whatever representation for infinities and NaNs the C implementation uses (this may vary according to which compiler was used to compile Lua and which C runtime your application is linked to).

like image 45
Lorenzo Donati -- Codidact.com Avatar answered Oct 03 '22 01:10

Lorenzo Donati -- Codidact.com