14

In the following example, you will see that the sample at (0,0) is not drawn using x^(0.5) (blue), with sqrt it is (green). How can you explain that?

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[samples at={0,0.02,...,1.5,2}]
       \addplot[green,thick]{sqrt(x)};
       \addplot[blue,thick]{(x)^(0.5)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

9

The sqrt function of the fpu library (which is active in PGFPlots by default) checks for the case sqrt(0), but the pow function does not check for the case 0^x. The pow function uses the equation #1^#2 = exp( #2 * ln(#1) ), so it tries to calculate the logarithm of 0, which fails, so the coordinate is discarded.

The check for 0^x can be added by redefining the \pgfmathfloatpow@ macro as follows:

\makeatletter
\def\pgfmathfloatpow@#1#2{%
    \begingroup%
    \expandafter\pgfmathfloat@decompose@tok#1\relax\pgfmathfloat@a@S\pgfmathfloat@a@Mtok\pgfmathfloat@a@E
    \ifcase\pgfmathfloat@a@S\relax
        % 0 ^ #2 = 0
        \pgfmathfloatcreate{0}{0.0}{0}%
    \else
        \expandafter\pgfmathfloat@decompose@tok#2\relax\pgfmathfloat@a@S\pgfmathfloat@a@Mtok\pgfmathfloat@a@E
        \ifcase\pgfmathfloat@a@S\relax
            % #1 ^ 0 = 1
            \pgfmathfloatcreate{1}{1.0}{0}%
        \or
            % #2 > 0
            \pgfmathfloatpow@@{#1}{#2}%
        \or
            % #2 < 0
            \pgfmathfloatpow@@{#1}{#2}%
        \or
            % #2 = nan
            \edef\pgfmathresult{#2}%
        \or
            % #2 = inf
            \edef\pgfmathresult{#2}%
        \or
            % #2 = -inf
            \pgfmathfloatcreate{0}{0.0}{0}%
        \fi
    \fi
    \pgfmath@smuggleone\pgfmathresult
    \endgroup
}%
\makeatother

It might be worth filing a bug report for this.


Your example could be fixed like this:

\documentclass{standalone}
\usepackage{pgfplots}

\makeatletter
\def\pgfmathfloatpow@#1#2{%
    \begingroup%
    \expandafter\pgfmathfloat@decompose@tok#1\relax\pgfmathfloat@a@S\pgfmathfloat@a@Mtok\pgfmathfloat@a@E
    \ifcase\pgfmathfloat@a@S\relax
        % 0 ^ #2 = 0
        \pgfmathfloatcreate{0}{0.0}{0}%
    \else
        \expandafter\pgfmathfloat@decompose@tok#2\relax\pgfmathfloat@a@S\pgfmathfloat@a@Mtok\pgfmathfloat@a@E
        \ifcase\pgfmathfloat@a@S\relax
            % #1 ^ 0 = 1
            \pgfmathfloatcreate{1}{1.0}{0}%
        \or
            % #2 > 0
            \pgfmathfloatpow@@{#1}{#2}%
        \or
            % #2 < 0
            \pgfmathfloatpow@@{#1}{#2}%
        \or
            % #2 = nan
            \edef\pgfmathresult{#2}%
        \or
            % #2 = inf
            \edef\pgfmathresult{#2}%
        \or
            % #2 = -inf
            \pgfmathfloatcreate{0}{0.0}{0}%
        \fi
    \fi
    \pgfmath@smuggleone\pgfmathresult
    \endgroup
}%
\makeatother

\begin{document}
\begin{tikzpicture}
    \begin{axis}[samples at={0,0.02,...,1.5,2}]
       \addplot[green,thick]{sqrt(x)};
       \addplot[blue,thick]{x^0.5};
    \end{axis}
\end{tikzpicture}
\end{document}
11

This has already been fixed; an upgrade to PGF 3.0.0 will remove the problem.

Here is your input file with PGF 3.0.0 :

enter image description here

The associated lines in the PGF change log are

2013-08-24 Christian Feuersaenger <cfeuersaenger@[xxxxx]net>

    - fixed bug in fpu: 0^0 and 0^x both produced nan. Now we get
      0^0=1 and 0^x = 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.