[Tex/LaTex] Increasing precision in \pgfmathsetmacro


I would like to use

\pgfmathprintnumber[/pgf/number format/frac]{\var}

where \var is a variable set by \pgfmathsetmacro and I'm having similar difficulties as in this and this question. I'm aware that the frac format is numerically unstable.

My problem is that the precision of \pgfmathsetmacro seems to be five digits, but for the following minimal example I'd need seven digits. Is there a trick to do it?


The following solutions are possible (I haven't decided which one is the best.. please vote!)

  • Package xintexpr and round(x,10) to have precision 10 result (by @jfbu)
    • changes just the division
  • frac shift=2 (by @Qrrbrbirlbel)
    • simple addition
    • no additional packages
  • fixed point arithmetic of tikz to calculate a more precise result (also by @Qrrbrbirlbel)
    • newcommand, but otherwise also just changes the division

Original post continued
See this example:

  • blue my first attempt
  • black with fpu
  • red: what I want.

enter image description here

\usepackage{fp} % uncommenting results in the following warning, but no change in output
%! Package pgf /pgf/number format/frac warning=true: /pgf/number format/frac of
%`3.3333e-1' = 100009 / 300030 might be large due to instabilities. Try \usepack
%age{fp} to improve accuracy.

\foreach \i in {0,1,...,\nn}{
  % first test
  \draw[blue] (\xi, -1.1 cm) -- (\xi,2pt)  node[above,align=center] 
     {\pgfmathprintnumber[fixed, precision=10]{\xival} \\ 
     \pgfmathprintnumber[/pgf/number format/frac]{\xival}};

  % second test (fpu)
  \draw (\xi,2pt) -- (\xi, -1.1 cm) node[below,align=center] 
      {\pgfmathprintnumber[fixed, precision=10]{\tmp} \\ 
      \pgfmathprintnumber[/pgf/number format/frac]{\tmp}};
  } % end foreach

% this works (two digits '3' added)
\node[red] at (1,-.5) {\pgfmathprintnumber[/pgf/number format/frac]{0.3333333}};


Best Answer

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.


\def\nu{4} \def\w{5}
\foreach \i in {0,1,...,\the\numexpr\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}
  \draw (\i,-1) node[above,align=center] 
    {{\tiny\pgfmathprintnumber[fixed, precision=10]{\xival}} \\ 
           \pgfmathprintnumber[/pgf/number format/frac]{\xival}};


enter image description here