15

Here is the code. It makes use of the file matrix.lua available at the following link. https://github.com/davidm/lua-matrix/blob/master/lua/matrix.lua

The following code is to be run by matrix.lua file in the same directory as that of tex file. It is to be compiled with LuaLaTeX engine.

\documentclass{article}
\begin{document}
\def\n{{{2,4,6},{8,10,12},{14,16,20}}}
\directlua{local matrix = require "matrix"
tex.sprint(matrix.latex(matrix.pow(\n,50)))}
\end{document}

When I run the command tex.sprint(matrix.latex(matrix.pow(\n,-50))). There is no problem. It runs fine and gives answer in scientific notation. So there is no problem with power -50. However with power 50, it gives absolutely wrong answer. This is definitely not issue with lua or matrix.lua. This is because when I run following commands in lua on local computer, I get correct answer.

local matrix = require "matrix"
n={{2,4,6},{8,10,12},{14,16,20}}
print(matrix.latex(matrix.pow(n,50)))

I also get correct answer when I run the code on overleaf. Basically answers on lua compiler on my local computer and overleaf compiler match. But answers produced by LuaLaTeX compiler on my computer are different from lua compiler on my computer or one that on overleaf. Where is the problem then?

  1. Is there any issue with local computer? If so, how I get correct answer with power -50. I also get correct answer in lua compiler on my computer.

  2. Why answers on overleaf compiler and my local computer are different for power 50. They match for power -50.

  3. Does this issue have anything to do with capacities of local computer such as processor, operating system, 32 bits or 64 bits, ram etc.?

Note: There is some problem in the matrix.latex function of the file matrix.lua. It prints some unnecessary characters at the beginning of each row and in the last entry of the last row of the matrix. I already fixed it. But for this question, it can be neglected.

19

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
  • Can we add a command in \directlua so that integers are auto converted into floats. – user61681 Aug 4 '19 at 8:16
  • @user61681 No, it's a Lua language feature. You could downgrade LuaTeX on your machine. – Henri Menke Aug 4 '19 at 9:05
10

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.