Hello.

Learning language Lua. I am trying to understand the cocenput moment: there are only three atomic data types in the language: 1. boolean 2. Number 3. String

As for Number, delving into the configuration file https://www.lua.org/source/5.1/luaconf.h.html

there is the following definition:

#define LUA_NUMBER double 

There is no int type but there is a built-in interpreter:

 LUA_API lua_Integer lua_tointeger (lua_State *L, int idx) { TValue n; const TValue *o = index2adr(L, idx); if (tonumber(o, &n)) { lua_Integer res; lua_Number num = nvalue(o); lua_number2integer(res, num); return res; } else return 0; } 

Can anyone give arguments for and against such a conceptual structure of the language and its atomic data types?

How does this help, how does it complicate work in general?

  • Lua 5.3 added an integer type as a subtype number. The problem is that the sizes of these types are determined at compile time. And it is only possible with a certain degree of probability that the type will be able to accommodate a certain value. - moteus
  • I am interested in the question regarding the old version 5.2. Just as a theoretical conceptual question. - Ilya.K.

1 answer 1

Such a justification is cited by Robert Ieruzalimsky in his book “Programming in the Lua language”:

Some fear that even simple operations such as incrementing by one (increment) and comparison may not work correctly with floating point numbers. However, in reality it is not. Almost all platforms now support the IEEE 754 standard for floating-point numbers. According to this standard, the only possible source of error is when the number cannot be accurately represented. An operation rounds its result only if the result cannot be accurately represented as the corresponding floating point value. Any operation whose result can be accurately represented will have an exact meaning.

Indeed, any integer up to 2 ^ 53 (approximately 10 ^ 16) has an exact representation as a double-precision floating-point number (double). When you use a double-precision floating-point value to represent integers, there are no round-off errors, unless the value of the module exceeds 2 ^ 53. In particular, Lua is able to represent any 32-bit integer values ​​without any problems with rounding. Of course, fractional numbers will have problems with rounding.

Before we continue, remember that integers have an accurate representation and therefore do not have rounding errors. Most modern CPUs perform floating-point operations as fast (or even faster) than with integers.

  • I read and looked in the book itself. It is not completely clear to me from this why the int type was deprived and what could be the pros and cons of this. Except that double-type calculations are not inferior in speed by combining with integers. - Ilya.K.
  • It was just convenient. The language was used as a configuration. There was also no need to support Int64. Lua 5.3 just added this support. - moteus
  • @ Ilya.K. When the Lua language was created (mid-nineties), there was still no support for Int64 in C (the long long type appeared only in C99), and since the double does an excellent job with int32, there was no point in a separate type. Although, it is worth noting that at first the type float generally used, and it was switched to double in order to fit the ints. The report of Jerusalem on this topic: youtube.com/watch?v=bjqNK1jA77M - zed