# Calculating averages and more

I am translating a Word document into LaTeX as a learning experience. Now there is this table, that already has hard-coded values. However, I would like to have it generic, so that changing a single number inside the .tex file changes the number of rows of the column. All values are calculated based on the key of the row, by the following algorithm.

Given a real number, calculate a number a dice (6-sided) and an integer modifier, which is no larger than +- 3. The expected value of the resulting modified dice throw should equal the input number with error no higher that 0.5. Make the algorithm deterministic.

Something in the lines of:

[dice, mod] foo(expected)
dice = floor(expected / 3.5)
mod = floor(expected - dice * 3.5)


But an algorithm, that balances the use of positive and negative modifiers would be better. Also, gracefully handling values below 3.5 would be great (always including at least one die).

As an example, 12 would output 3 dice +1 modifier for a total expected value of 11.5.

The source table is this, but it needn't be followed precisely.

• Please, someone more experienced edit the tags of the question. I browsed the first 3 pages of most popular tags, but found nothing. – Vorac Feb 24 '14 at 15:51
• I would suggest the calculator package in combination with forloop package. It should be possible to calculate averages then. Can you post an image file of that mentioned table? – user31729 Feb 24 '14 at 15:55
• @Christian, sure, later tonight. In short, the row number is the row ID as well. We have three columns: *1, *2/3 and *1/2. Each cell should contain the row ID, multiplied by the column ... um heading. The number of rows is currently 30, but I would like to be flexible! – Vorac Feb 24 '14 at 16:00
• You mean, that the number of rows should be flexible? The multiplication is not really the problem, but the 'dynamical' number of rows. This can be done from within LaTeX, but I suppose, it is no joy to do so ;-) – user31729 Feb 24 '14 at 16:21
• @Christian, I am yet to learn constructing tables, when a little free time pops up. That sound fairly easy and well-documented. I was thinking doing math would be awkward, hence this question on feasibility. So constructing a table, that has a number of rows, defined somewhere else, is a problem? – Vorac Feb 24 '14 at 16:24

Is this what you are aiming at?

\documentclass[12pt]{article}
\usepackage{xintfrac}
\usepackage{xintexpr}

\begin{document}\thispagestyle{empty}

% dice = floor(expected / 3.5)
% mod = floor(expected - dice * 3.5)

\def\rows {12}

\begin{tabular}{ccc}
\hline
expected & dice & mod \\
\hline
\xintFor* #1 in {\xintSeq {1}{\rows}}
\do
{ #1 & \xinttheexpr floor(#1 / 3.5)\relax
& \xinttheexpr floor(#1 - floor(#1/3.5)*3.5)\relax \\ }
\hline
\end{tabular}

% \begin{tabular}{ccc}
%   $*1$&$*2/3$&$*1/2$\\
%   \xintFor* #1 in {\xintSeq {1}{30}}
%   \do
%   {$#1$&$\xintRound {4}{\xintMul {#1}{2/3}}$&$\xintRound {2}{\xintMul {#1}{1/2}}$\\}
% \end{tabular}

\end{document}


I am not sure to have well understood. Here is the table from my first answer (commented out code above).

\documentclass[12pt]{article}
\usepackage{xintfrac}

\begin{document}\thispagestyle{empty}

\begin{tabular}{ccc}
$*1$&$*2/3$&$*1/2$\\
\xintFor* #1 in {\xintSeq {1}{30}}
\do
{$#1$&$\xintRound {4}{\xintMul {#1}{2/3}}$&$\xintRound {2}{\xintMul {#1}{1/2}}$\\}
\end{tabular}

\end{document}


If the number of rows is too big to fit on a page, using a longtable is possible, or the TeX primitive \halign.

• Hi. I added an example! – Vorac Feb 24 '14 at 17:08
• there is no problem to implement your formula using xintfrac or more conveniently xintexpr: for example \xinttheexpr floor(#1/3.5)\relax, but could you please add an image of a sample table, I have some difficulty figuring out how it is supposed to be. You can use \xintFor* with explicit sequence {{1.2}{3.7}{10.9}} rather than an arithmetic one. Or without the star \xintFor accepts a comma separated list {1.2, 3.7, 10.9}. – user4686 Feb 24 '14 at 17:17
• Great! Now to understand it :D – Vorac Feb 24 '14 at 18:56
• I was going to say the same vz the table image you have now included ;-) – user4686 Feb 24 '14 at 19:20
• how does As an example, 12 would output 3 dice +1 modifier for a total expected value of 11.5. relate to row number 12 in the image you uploaded? I still don't get it. – user4686 Feb 24 '14 at 19:23