[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
**Subject**: **Re: largest number possible?**
**From**: mniip <mniip@...>
**Date**: Fri, 13 Jun 2014 16:35:14 +0400

On 06/13/2014 04:15 PM, Mayuresh Kathe wrote:
> hello,
>
> i wrote the following snippet (my first attempt at lua code);
> -- power generator begin
> function power(n)
> local x = 2
> for i = 2, n do
> x = x * 2
> end
> return x
> end
>
> print(power(1024))
> -- power generator end
>
> i ran it with 'lua52 square.lua' which gave me an output of "inf".
>
> is there any way to find out the largest number lua can handle?
>
> thanks,
>
> ~mayuresh
>
>
By default Lua uses C's "double" for its number type. On most platforms
it is going to be a 64-bit binary IEEE754 floating point number. As
already pointed out by Luiz, in that case it is about
1.797693134862315708145274237317043567981e+308.
However in some exceptional cases Lua might be compiled with integer
arithmetic instead, and then the largest value will be of a different
nature.
Here's a snippet that should work with both overflowing (signed int,
unsigned int), and saturated (single, double), both sign-bit and 2's
complement arithmetics:
local x = 1.0
while x * 4 > x * 2 do
x = x * 2
end
if x * 4 < x * 2 then
x = x * 2
end
local d = x / 2
while x + d ~= x do
if x + d > x and x + d + d ~= x + d then
x = x + d
end
d = d / 2
end
print(x)
To test it with 5.3's integers change 1.0 to 1 and / to //