Different answers of calculations in LuaLaTeX on local computer, lua compiler and on overleaf

You have encountered the new integer datatype of Lua 5.3 which is part of LuaTeX 1.10.1 in TeX Live 2019. When Lua encounters a number that can be represented by a 64-bit integer, Lua will no longer convert it to a floating point number and lose precision but store it at full precision in a suitable integer type. However, when using integers you do not only get their advantages but also their disadvantages, one of them is overflow.

For floating point numbers, if the number becomes too large to represent it just becomes inf but integers do not behave that way due to their layout in memory. If an integer becomes too large it will wrap around and become negative. You can try that out for yourself:

$ lua5.3
Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> print(9223372036854775807 + 1)
-9223372036854775808

That is exactly what you are seeing in your code.

But there is a way around it. If you don't want to use integers, you don't have to. Simply tell Lua that you would like to use floating point numbers by appending a .0 to literals.

$ lua5.3
Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> print(9223372036854775807.0 + 1.0)
9.2233720368548e+18

Lua 5.3 as used in texlive 2019 luatex has a (64bit) integer type.

You need to decide what outcome you want, and whether to use the integer or floating point 64bit types.

The file

a=1024*1024*1024*1024*1024*1024*1024 -- 2^70
print(a)

which is trying to get the integer 2^70

produces 0 with luatex 1.10 and greater:

$ texlua in1.lua
0

but with older luatex produced an approximate result:

$ /usr/local/texlive/2018/bin/x86_64-cygwin/texlua in1.lua
1.1805916207174e+21

So you might be tempted to fix this by casting do double, eg this version with 1.0

a=1.0*1024*1024*1024*1024*1024*1024*1024 -- 2^70
print(a)

Produces 1.1805916207174e+21 on both systems.

Note however double requires some bits to store the exponent so while it will not overflow at 2^64 it can not accurately store integers above 2^56

Compare this file that calculates 2^60 then adds 1.

a=1024*1024*1024*1024*1024*1024
print(a)
b=a+1
print(b)
print (a == b)

This works in exact integer arithmetic with Lua 5.3

$ texlua in.lua
1152921504606846976
1152921504606846977
false

but works in double arithmetic, and a final result of true rather than false, in Lua 5.2.

$ /usr/local/texlive/2018/bin/x86_64-cygwin/texlua in.lua
1.1529215046068e+18
1.1529215046068e+18
true