Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum value of a number in Lua?

Tags:

There doesn't seem to be a clear answer to this in the documentation.

I'm interested in incrementing a variable time that counts the seconds since the program started. If the maximum value can count far into the future, like 100 years, then I don't care about letting the variable increment forever. Otherwise I'm going to have to think of a good point to reset time back to 0.

like image 811
Kai Avatar asked Jun 03 '09 16:06

Kai


People also ask

What is the maximum value an integer can hold?

The number 2,147,483,647 (or hexadecimal 7FFFFFFF16) is the maximum positive value for a 32-bit signed binary integer in computing. It is therefore the maximum value for variables declared as integers (e.g., as int ) in many programming languages.

What number is Lua?

Lua has no integer type, as it does not need it. There is a widespread misconception about floating-point arithmetic errors and some people fear that even a simple increment can go weird with floating-point numbers.

How do you write infinity in Lua?

#INF (+-inf) and -1.


2 Answers

as compiled by default, the Number is a double, on most compilers that's an IEEE 64-bit floating point. that means 10bit exponent, so the maximum number is roughly 2^1024, or 5.6e300 years. that's a long time.

now, if you're incrementing it, you might be more interested in the integer range. the 52-bit mantissa means that the highest number that can be used with integer precision is 2^52, around 4.5e15. At 31'557,600 seconds/year, that's 1.427e8, almost 150 million years. still a very long uptime for any process

update 2014-12-30: Lua 5.3 (to be released any moment now) adds support for integer values, either 32 or 64 bits chosen via compile flags.

like image 136
Javier Avatar answered Sep 29 '22 02:09

Javier


Although tydok's reference to PiL 2.3 is both correct and apropos, and Javier's answer is in practice correct, I thought the discussion of numbers in Lua should be rounded out with a couple of other details.

The Lua interpreter is designed to be embedded in an application typically as a configuration and/or scripting language. When built for an application, it is common to configure some of its features to suit.

The exact numeric type to use for numbers is available for configuration. When compiling for a platform without hardware floating point where loading third-party modules is not important (especially in an embedded system or set-top box game console application) then it is reasonable to pick an integral type instead of the default double. Occasionally, switching to float is reasonable also.

However, there are applications where 64-bit integers are needed, or most numbers can be integers but occasional floating point arithmetic is required. For those cases, there is the LNUM patch to the Lua core.

LNUM changes the core so that numbers are stored as either integers or floating point, and allows several configurable selections for the precision of each.

So the bottom line answer to the question of the maximum value of a Lua number is that it depends on the configuration of the interpreter chosen at compile time, and whether you are worried about the maximum magnitude representable or the maximum integer. And even then, work has been done to make large integers play well with floating point representations.

like image 30
RBerteig Avatar answered Sep 29 '22 01:09

RBerteig