# How does TikZ parse powers? [duplicate]

I was often stumbling upon (and correcting by hand) an IMHO rather surprising behavior of TikZ: powers are parsed in an unconventional way. Look at the MWE

\documentclass[tikz,border=3.14mm]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=symb]
\draw plot[variable=\x,domain=-1:1] (\x,{\x^2});
\end{scope}
\node[anchor=south] at (symb.north) {$x\verb|^|2$};
%
\begin{scope}[xshift=2.5cm,local bounding box=pow]
\draw[dotted] plot[variable=\x,domain=-1:1] (\x,{pow(\x,2)});
\end{scope}
\node[anchor=south] at (pow.north) {$\texttt{pow}(x,2)$};
%
\begin{scope}[xshift=5cm,local bounding box=star]
\draw[dashed] plot[variable=\x,domain=-1:1] (\x,{\x*\x});
\end{scope}
\node[anchor=south] at (star.north) {$x*x$};
\end{tikzpicture}
\end{document} The left/solid plot shows x^2, and naively I would expect it to coincide with pow(x,2) and x*x, but it doesn't. Rather, for negative x, the plot also grows in the negative direction.

On the other hand, pgfplots seems to correct for this, even/also (but not only) when one uses declare function.

\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=\x^2;}]
\begin{axis}[width=4cm,title={pgfplots}]
\end{axis}
\begin{scope}[xshift=5cm,yshift=1cm,local bounding box=tikz]
\draw plot[variable=\x,domain=-1:1] (\x,{f(\x)});
\end{scope}
\node[anchor=south] at (tikz.north) {Ti\emph{k}Z};
\end{tikzpicture}
\end{document} Naively, I'd expect the plots having the same qualitative shape (modulo adjustments of length scales made by pgfplots, of course). So pgfplots and TikZ seem to interpret the same function differently.

Question: Why is that? Is there a simple way to make TikZ produce the expected output (i.e. x^2 is nonnegative)?

NOTE: @circumscribe made me aware of this earlier question. Apart from the comparison to pgfplots, this post does not add anything to the story, and in any case most likely the only viable answer is this one. So I will certainly not object to this question being closed as a duplicate of this earlier question. (I did search for earlier questions, but was under the impression they should contain parse in the title, which is why I didn't find it.)

• interestingly! with f(\x)=\x*\x works fine ... – Zarko Jan 11 '19 at 18:50
• @Zarko Sure. This is what I did in all my answers so far (where that mattered). – user121799 Jan 11 '19 at 18:51
• also work correctly if you enclose variable in parenthesis: f(\x)=(\x)^2. it seem that negative values of variable pgfplots and pure tikz treats differently – Zarko Jan 11 '19 at 18:53
• @manooooh What I meant to say is that the parsing is unfortunate for all even powers, because the result of the parsing x^n is sign(x)*pow(abs(x),n), where n is the (integer) power. For odd n this is what one might expect, for even n it may be called unexpected. – user121799 Jan 13 '19 at 3:42
• @marmot: Have you seen the answers to this question? I have no idea if it's helpful or if the patch that's mentioned in the second answer (still) works. – Circumscribe Jan 13 '19 at 22:51