The 0.33333
is simply too imprecise for the high precision that /frac
and fp
offers as 100009/300030 (which is 0.3¯33330000) is nearer to 0.33333 than to 1/3.
You can work around this by giving the option /pgf/number format/frac shift=2
(initially 4
) or by using fp
also for the division.
Code
\documentclass{standalone}
\usepackage{fp,tikz}
\usetikzlibrary{fpu,fixedpointarithmetic}
\newcommand*{\pgfMathsetmacro}[2]{\pgfmathparse{#2}\let#1\pgfmathresult}
\begin{document}
\begin{tikzpicture}[x=1.5cm]
\def\nu{4} \def\w{5}
\foreach \i in {0,1,...,\the\numexpr\nu-1}{
\pgfmathsetmacro\xival{\i/(\nu-1)}
\draw (\i,0pt) node[above,align=center]
{{\tiny\pgfmathprintnumber[fixed, precision=10]{\xival}} \\
\pgfmathprintnumber[/pgf/number format/.cd, frac, frac shift=2]{\xival}};
\tikzset{fixed point arithmetic}
\pgfMathsetmacro\xival{\i/(\nu-1)}
\draw (\i,-1) node[above,align=center]
{{\tiny\pgfmathprintnumber[fixed, precision=10]{\xival}} \\
\pgfmathprintnumber[/pgf/number format/frac]{\xival}};
}
\end{tikzpicture}
\end{document}
Output
The result of the calculation can be directly assigned to a macro using \pgfmathsetmacro
.
Table cells are local groups, therefore the following definition of \allan
first performs the calculations, defines a macro for the table rows with the expanded calculation results and calls the macro to actually set the rows:
\newcommand{\allan}{
\pgfmathsetmacro\RandomA{random(10,99)}%
\pgfmathsetmacro\RandomB{random(0, int(9 - mod(\RandomA,10)))}%
\edef\next{%
& \RandomA \noexpand\\%
+ & \RandomB \noexpand\\%
}%
\next
}
Full example:
\documentclass{article}
\usepackage{geometry}
\geometry{letterpaper, portrait, margin=1.5cm, tmargin=2.5cm }
\usepackage{tabularx}
\usepackage{array}
\usepackage{siunitx}
\usepackage{tikz}
\usepackage{pgf}
\DeclareMathSizes{10.0}{17}{12}{12}
\begin{document}
\begin{flushleft}
% fixed width, right justified column
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
% print 2 numbers
%
% 42
% + 7
%
\newcommand{\allan}{
\pgfmathsetmacro\RandomA{random(10,99)}%
\pgfmathsetmacro\RandomB{random(0, int(9 - mod(\RandomA,10)))}%
\edef\next{%
& \RandomA \noexpand\\%
+ & \RandomB \noexpand\\%
}%
\next
}
% create 7x9 grid of addition problems
\foreach \n in {0,...,8}{
\foreach \n in {0,...,6}{
\begin{tabularx}{1.8cm}{>{$}R{.3cm}<{$} >{$}R{.7cm}<{$}}
\allan
\hline
\end{tabularx}
\hfill
}
\par
\vspace{1.5cm}
}
\end{flushleft}
\end{document}
Variant without tabularx
, which fills the space on the page:
\documentclass{article}
\usepackage{geometry}
\geometry{letterpaper, portrait, margin=1.5cm, tmargin=2.5cm }
\usepackage{tabularx}
\usepackage{array}
\usepackage{siunitx}
\usepackage{tikz}
\usepackage{pgf}
\DeclareMathSizes{10.0}{17}{12}{12}
\begin{document}
\begin{flushleft}
\setlength{\parskip}{0pt plus 1fill}
\setlength{\parfillskip}{0pt}
\setlength{\tabcolsep}{2\tabcolsep}% make the lines a little longer
% print 2 numbers
%
% 42
% + 7
%
\newcommand{\allan}{
\pgfmathsetmacro\RandomA{random(10,99)}%
\pgfmathsetmacro\RandomB{random(0, int(9 - mod(\RandomA,10)))}%
\edef\next{%
& \noexpand\leavevmode
\ifnum\RandomA<10 \noexpand\hphantom{0}\fi % if \RandomA can be smaller than ten
\RandomA \noexpand\\%
+ & \RandomB \noexpand\\%
}%
\next
}
% create 7x9 grid of addition problems
\foreach \n in {0,...,8}{
\foreach \n in {0,...,6}{
\begin{tabular}{>{$}l<{$} @{$\;$} >{$}r<{$}}
\allan
\hline
\vadjust{\vspace{1.1cm}}% place for result
\end{tabular}%
\hfill
}%
\par
}
\flushbottom
\newpage
\end{flushleft}
\end{document}
Hint: \pgfmathsetseed{<number>}
can be used to get reproducible results.
Best Answer
The number range in the default math engine of
pgf
is quite restricted because of the limitations of TeX's numbers. Also the library for fixed point arithmetic does not help with these exponents, because this library only covers ten digits before and after the decimal point.The example can be processed with the floating point unit library: