# How can I use math.log10 function with the directlua?

I would like to know how I can use math.log10 function with the directlua, because I always get none from directlua.

Following a minimal example:

%Engine: LuaLaTeX;
%Encoding: UTF-8;

\documentclass[11pt,A4paper]{article}

\begin{document}
{\centering Function $\log$ - Example\par}

\noindent\directlua{tex.print(math.log(math.exp(1)))} is the natural logarithm
of $e$.\\
\directlua{tex.print(math.log10(100))} is the base-10 logarithm of 100.\\
\textbf{What is the mistake in the last sentence?}

\end{document}

• Is there a math.log10 function in Lua to begin with? – egreg Mar 2 '19 at 22:37
• What LuaTeX version are you using? This is 'works for me' in TL'18 and TL'19 pre-test. – Joseph Wright Mar 2 '19 at 22:40
• LuaTeX: 1.07.0 MiKTeX: 2.9.6730 64-bit – VAS Mar 2 '19 at 22:45
• @egreg , in Lua 5.1.5 this function is avaliable. – VAS Mar 2 '19 at 22:46
• In my miktex is works fine, but I have newer luatex there: This is LuaTeX, Version 1.09.2 (MiKTeX 2.9.6960 64-bit) – Ulrike Fischer Mar 2 '19 at 22:46

math.log(x[, base])
Returns the logarithm of x in the given base. The default for base is e (so that the function returns the natural logarithm of x).

It seems that math.log10 exists, albeit undocumented in the official online reference manual. However, it has not existed in some revisions of Lua 5.2.

\documentclass[11pt,a4paper]{article}

\begin{document}

\directlua{tex.print(math.log(math.exp(1)))} is the natural logarithm
of $e$.

\directlua{tex.print(math.log(100,10))} is the base-10 logarithm of 100.

\end{document}


I get no error if I use math.log10(100) with TeX Live 2012 or 2018 (or 2019/pretest), but an error is raised with TeX Live 2013, 2014, 2015, 2016 and 2017.

With TeX Live 2019/pretest, LuaTeX uses Lua 5.3 that adds one decimal digit in order to make clear this is a floating point number rather than an integer.

I compared the online manuals for Lua:

• version 5.1 has math.log10

math.log (x)
Returns the natural logarithm of x.

math.log10 (x)
Returns the base-10 logarithm of x.

• version 5.2 hasn't it

math.log (x [, base])
Returns the logarithm of x in the given base. The default for base is e (so that the function returns the natural logarithm of x).

I guess that, at some point in time, math.log10 has been reinserted for back compatibility also in some revision of version 5.2 and has been kept in 5.3. LuaTeX has used Lua 5.2 since 2013, which explains the mystery.

Use the official function.

As an extra, courtesy of Paulo Cereda, you can conditionally define math.log10:

\documentclass[11pt,a4paper]{article}

\directlua{math.log10 = math.log10 or function(x) return math.log(x, 10) end}

\begin{document}

\directlua{tex.print(math.log(math.exp(1)))} is the natural logarithm
of $e$.

\directlua{tex.print(math.log10(100))} is the base-10 logarithm of 100.

\end{document}


I tested it on every TeX Live version I have and it works. According to Paulo

If math.log10 is defined, it will return function (in the conditional, everything but nil resolves to true) and thus the logical operation ends due to short circuit. Otherwise, the function does not exist (it will return nil) and we redefine it (second part of the logical operation).

• math.log10 works fine for me. – Ulrike Fischer Mar 2 '19 at 22:42
• @UlrikeFischer I added some information about the matter. – egreg Mar 2 '19 at 22:47
• Lua added an integer type in 5.3. Previously all numbers were double precision floating point. To distinguish between int and double in the output, Lua 5.3 adds at least one decimal place for double. – Henri Menke Mar 2 '19 at 22:50