How to save integral float number in Lua 5.3

print(2^62)
print(2^63)
print(2^64)

In Lua 5.2, all numbers are doubled. The output of the above code:

4.6116860184274e+18
9.2233720368548e+18
1.844674407371e+19

Lua 5.3 supports integers and automatically converts between an integer and a float representation. The same code outputs:

4611686018427387904
-9223372036854775808
0

I want to get the result of a float. 2.0^64works, but what if it's not a literal:

local n = io.read("*n")  --user input 2
print(n^64)

One possible solution is to divide the number by 1: (n/1)^64because in the section the /operands are always converted to float, but I'm looking for a more elegant solution.

Tested on Lua 5.3.0 (work2) .

+4
source share
1 answer

io.read("*n")always returns a float. So there are no surprises.

float, 0.0 .

+4

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


All Articles