I was trying to print the first few terms of hyperfactorials, which are defined by H(0)=1 and H(n)=H(n-1)*n^n.
- When
fpeval
is used for calculation andinteval
is used for printing:
\documentclass{article}
\usepackage{tikz}
\usepackage{xfp}
\begin{document}
These are the first few terms of hyperfactorials. \\
\edef\x{1}
\foreach[remember=\x] \i in {1,2,...,13} {
\edef\x{\fpeval{\x*\i^\i}}
$\inteval{\x}$ \\
}
\end{document}
Error:
! Number too big.
Output (when ignoring error):
These are the first few terms of hyperfactorials.
1
4
108
27648
86400000
2147483647
2147483647
2147483647
2147483647
2147483647
2147483647
2147483647
2147483647
- When
fpeval
is used for calculation and printing:
\documentclass{article}
\usepackage{tikz}
\usepackage{xfp}
\begin{document}
These are the first few terms of hyperfactorials. \\
\edef\x{1}
\foreach[remember=\x] \i in {1,2,...,13} {
\edef\x{\fpeval{\x*\i^\i}}
$\fpeval{\x}$ \\
}
\end{document}
Output:
These are the first few terms of hyperfactorials.
1
4
108
27648
86400000
4031078400000
3319766398771200000
55696437941726560000000000
21577941222941860000000000000000000
215779412229418600000000000000000000000000000
61564384586635060000000000000000000000000000000000000000
548914237009501600000000000000000000000000000000000000000000000000000
166252458044258000000000000000000000000000000000000000000000000000000000000000000000
- When
inteval
is used for calculation and printing:
\documentclass{article}
\usepackage{tikz}
\usepackage{xfp}
\begin{document}
These are the first few terms of hyperfactorials. \\
\edef\x{1}
\foreach[remember=\x] \i in {1,2,...,13} {
\edef\y{\x}
\foreach[parse=true][remember=\y] \j in {1,...,\i} {
\edef\y{\inteval{\y*\i}}
}
$\inteval{\y}$ \\
\edef\x{\y}
}
\end{document}
Error:
! Arithmetic overflow.
Output (when ignoring error):
These are the first few terms of hyperfactorials.
1
1
1
1
1
1
1
1
1
1
1
1
1
Correct output:
These are the first few terms of hyperfactorials.
1
4
108
27648
86400000
4031078400000
3319766398771200000
55696437941726556979200000
21577941222941856209168026828800000
215779412229418562091680268288000000000000000
61564384586635053951550731889313964883968000000000000000
548914237009501581804104224704637116078267727827959808000000000000000
166252458044258018207674078620690924617735088270974773221032328167424000000000000000
Comparing the three cases it is clear that using \fpeval
for both calculation and printing results in the best accuracy. Why is \fpeval
more precise? Shouldn't integers have more precision and range since there are no exponent bits? Also are there any packages/commands that cover huge integers with 100% accuracy? Finally what are the exact range and precision of \fpeval
and \inteval
?