7

I am trying to make mental math worksheets with exercises such as

25% of 80 trillion is ___

It seems that pgf and/or pgfmath is not capable of doing this as the numbers are too large. The code compiles with smaller numbers.

How should I do this?

When I figure out what's going on, I'll switch to random numbers that generate exercises and solutions worksheets, but I believe the MWE below demonstrates the problem.

\documentclass{article}

\usepackage{pgf}
\usepackage{pgfmath}

\pagestyle{empty}


\begin{document}

\pgfmathsetmacro{\Product}{int(20000*50000)}

\Product

\end{document}

enter image description here

2
  • It's a hard limit in TeX. You can't exceed that limit. You could do the calculations not in TeX and then typeset the results.
    – cfr
    Dec 5, 2020 at 1:23
  • The sagetex package gives you access to a computer algebra system, SAGE, which can handle all the mathematics. Cocalc is the easiest way to get started. Search sagetex on this site for examples.
    – DJP
    Dec 5, 2020 at 1:34

2 Answers 2

7

You can use xfp or xint. For the above purpose they have the advantage that they can yield expandable expressions. pgf also works with the fpu library (and also with the fixedpointarithmetic library together with the fp package, but the latter is arguably superseded by xfp). Here is one example.

\documentclass{article}

\usepackage{xfp}

\pagestyle{empty}


\begin{document}

\fpeval{20000*50000}

\end{document}

enter image description here

2
  • Thank you! Eventually, I want to create worksheets that use random numbers to generate exercises and solutions. How well can xfp handle that? How about xint? Dec 5, 2020 at 17:59
  • @WeCanLearnAnything Random numbers are implemented in both. In xfp there are randint(m, n) and rand(), try e.g. \fpeval{rand()} or \fpeval{randint(2,7)}. The perhaps biggest drawback of xfp is the quality of its documentation. If you use texdoc xfp you get something that lets you sort of guess what's going on, but not even the function names are typeset consistently. You can look up the details with texdoc l3fp, but this is not necessarily written for those who do not know much about l3.
    – user230294
    Dec 5, 2020 at 18:08
3

DISCLAIMER: This only works with LuaTeX and only with integers.

If you want to work with arbitrary-precision integers, a pure Lua module which handles very well with otherwise difficult cases is lua-nums. You need to download bn.lua and put it in the same folder as your main file.

I've avoided parsing, but you could look at LPEG for more advanced examples. For the moment, basic operations (+, -, *, /, ^, //, %) and binary operators (&, |, >>, <<, ~, |) should work, although passing special characters to Lua is kinda a pain. There's a trade-off between cumbersome typing and speed of operations as lua-nums runs smoothly.

\documentclass{article}
%\usepackage{geometry}
%\geometry{paperwidth=100mm,paperheight=85mm,margin=2em}
\usepackage{luacode}
\begin{luacode*}
userdata = userdata or {}
--bn.lua should be in the same folder as \jobname.tex
userdata.bn = require"bn"
userdata.evaluatebn = function(s)
    return load("return ".. s:gsub("%d+", "userdata.bn(%1)"))()
end
\end{luacode*}
\newcommand{\evaluatebn}[1]{\directlua{tex.sprint(tostring(userdata.evaluatebn("#1")))}}
\begin{document}
\section{Rationale}
In Spanish and English (long scale), a trillion stands for $10^{18}$: \evaluatebn{10^18}.

%Integer division, so // and / are interchangeable
25\% of 80 trillion (long scale) is \evaluatebn{80*10^18//4}

25\% of 80 trillion (short scale) is \evaluatebn{80*10^12//4}

\section{Some other examples}
\begin{itemize}
\item $2^{70} = \evaluatebn{1 << 70}$
% \string& instead of Lua's &
\item $47542121789123 = 4\times\evaluatebn{(47542121789123 >> 2)} + \evaluatebn{(47542121789123 \string& 3)}$
\item $15! = \evaluatebn{1*2*3*4*5*6*7*8*9*10*11*12*13*14*15}$
% \csstring\% instead of Lua's %
\item $47542121789\% 4787973 = \evaluatebn{(47542121789 \csstring\% 4787973)}$
\end{itemize}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .