[Tex/LaTex] pgfplots and calculations; without fpu ‘dimension too large’, with fpu ‘Illegal unit of measure (pt inserted)’

pgfmathpgfplotstikz-pgf

I'm trying to multiply some numbers that will end up in the legend of a plot. I can't get it to work:

  1. If I naively try \pgfmathmultiply, I get a dimension too large error, with the code in MWE 1 (see below). I understand this is because TeX doesn't count beyond 18 inch (or so). All right, I need to load fpu, as explained in section 36 of the pgfmanual (version 2.10).

  2. If I pass /pgf/fpu to an axis-environment, compilation fails with Illegal unit of measure (pt inserted) (with MWE 2). Even if the axis-environment is completely empty. According to " How do I use pgfmathdeclarefunction to create define a new pgf function?", pgfplots uses fpu internally, so I don't really understand why I can't load it.

  3. Maybe I'm using a wrong function, I thought. On page 362 of the PGF-manual, I read The installation exchanges any routines of the standard math parser with those of the FPU: \pgfmathadd will be replaced with \pgfmathfloatadd and so on. Furthermore, any number will be parsed with \pgfmathfloatparsenumber. So, if I already have the FPU, maybe I need to use \pgfmathfloatmultiply instead? I tried with MWE 3, but this time it fails to compile with: ! Package PGF Math Error: Sorry, an internal routine of the floating point unit got an ill-formatted floating point number '1000'. The unreadable part was near '1000'... Maybe \pgfmathmultiply is not meant to be used by the user, or so.

How do I multiply large numbers and show the result in my pgfplot?

Related yet different questions:

MWE 1:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title={\pgfmathmultiply{1000}{1000}}
          ]
\addplot coordinates {(0, 0) (1, 1) (2, 2)};
\end{axis}
\end{tikzpicture}
\end{document}

MWE 2

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{tikzpicture}
\begin{axis}[/pgf/fpu,
    title={\pgfmathmultiply{1000}{1000}}
          ]
\addplot coordinates {(0, 0) (1, 1) (2, 2)};
\end{axis}
\end{tikzpicture}
\end{document}

MWE 3:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title={\pgfmathfloatmultiply{1000}{1000}}
          ]
\addplot coordinates {(0, 0) (1, 1) (2, 2)};
\end{axis}
\end{tikzpicture}
\end{document}

Best Answer

PGFplots uses the fpu library internally, but not for everything. At the user level, the fpu library is not activated. You can do that by hand by setting \pgfkeys{/pgf/fpu=true} before you do your maths using the normal \pgfmathparse{...} command. After you're done, you need to switch the fpu library back off again, otherwise PGFplots will get confused, since the fpu library uses a floating point representation of numbers, not a fixed point one. You can see what that looks like by saying

\pgfkeys{/pgf/fpu=true}
\pgfmathparse{1000*1000}
\pgfmathresult
\pgfkeys{/pgf/fpu=false}

which will print 1Y1.0e6]. You probably don't want that representation in your title, so you should either set \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}, which will make the output of maths operations fixed point, or use \pgfmathprintnumber{\pgfmathresult} for printing the result, which will use a fixed point representation even for floating point input.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.5}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    title={\pgfkeys{/pgf/fpu=true}
        \pgfmathparse{1000*1000}
        \pgfmathprintnumber{\pgfmathresult}
        \pgfkeys{/pgf/fpu=false}}
          ]
\addplot coordinates {(0, 0) (1, 1) (2, 2)};
\end{axis}
\end{tikzpicture}
\end{document}
Related Question