[Tex/LaTex] How to avoid the “Dimension too large” error when plotting in newer PGF versions

tikz-pgf

After using a new version (2.10) of PGF, certain parts of my old TikZ code stopped functioning properly. For instance, the plot for a power function:

\documentclass{minimal} 
\usepackage{tikz}
\begin{document}
 \begin{tikzpicture}[scale=6,domain=0:1]
  \draw[domain=0.0000125:1] plot (\x,{(\x)^(0.5)-\x});
 \end{tikzpicture}
\end{document}

I now get the "Dimension too large" error for this.

The problem has something to do with the power function: \draw[domain=0.0000125:1] plot (\x,{(\x)^(2)-\x}); would cause no problem at all under the new version. More specifically, the problem seems to lie in taking square roots.

I noticed the problem quite some time ago, but since I am able to revert back to an older version (2.00) of PGF under which the above piece of code causes no problem, I didn't bother to ask about it. Now, however, when other people run my code, they often get problems because they have newer versions of PGF.

My question is:

How can I rewrite the code such that the intended function gets plotted without any problem under the current version of PGF?

Best Answer

Apparently, this is (was) a bug in pgf 2.10 : I can reproduce it with pgf 2.10 and it works in the current pgf CVS.

As a workaround, I suggest to use sqrt(x) which seems to use a special implementation - it works in pgf 2.10 as well.

To really solve the problem, you may need to upgrade to a more recent pgf version (I believe there are still unstable pgf builds on texample.net (?) ).

If that is infeasible, you may want to explain more about your use-case such that others can comment on alternatives.

EDIT:

According to your comment, you really search for a more general solution without upgrading pgf. There is further solution. I did not mention it in the first place because I have never used it. The idea is to use the fpu library of PGF (maybe the fp library also does the job).

With fpu, the solution is as follows:

\documentclass{minimal} 
\usepackage{tikz}
\usepgflibrary{fpu}%----- this
\begin{document}
 \begin{tikzpicture}[scale=6,domain=0:1]
  \draw[domain=0.0000125:1,
     /pgf/fpu,/pgf/fpu/output format=fixed%------ this
  ] plot (\x,{(\x)^(0.8)-\x});
 \end{tikzpicture}
\end{document}

I suppose this will always work. If replaces the internal PGF math engine (but only for that specific drawing command). The solution comes with minor changes to your documents and should not change the nature of your figures at all.

In case you are generally interested in plots (perhaps with more flexible plot handlers, 3d, or large data ranges, combined with a complete axis), you may also want to take a look at \usepackage{pgfplots}. This, however, changes your figures considerably.