# On the strange calculations calling xfp, Tikz, sqrt, and ^2

As we now the square root of (-3)^2+(-4)^2 is the square root of 25 equal to 5. However we got a strange error calling xfp package and Tikz saying:

\LaTeX3 error: Invalid operation sqrt(-25) Below is the code:

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xfp}
\begin{document}

\begin{tikzpicture}

\edef\u{-3};
\edef\v{-4};
\edef\w{\fpeval{sqrt(\fpeval{\u^2}+\fpeval{\v^2})}};

\node   at (0,0) {Ans=\fpeval{\w}};

\end{tikzpicture}
\end{document}

• This is really strange that \u^2 is interpreted as a negative number such as -9.
– Aria
Commented Sep 4, 2021 at 21:08

Your \u is not a “variable”, but just a macro and TeX expands it so you get

\u^2-3^2-9

Use (\u)^2. By the way

\edef\w{\fpeval{sqrt((\u)^2+(\v)^2)}}


is much more efficient. And you don't need \fpeval{\w}, just \w is sufficient.

• Yes now makes sence. The key is to use (\u)^2 and not \u^2
– Aria
Commented Sep 4, 2021 at 21:13

Order of operations...exponentiation takes precedence over negation. Thus, one must explicitly group (via parens) the negatives, lest -3^2 be evaluated as -9.

\documentclass{standalone}
\usepackage{tikz}
\usepackage{xfp}
\begin{document}

\begin{tikzpicture}

\def\u{(-3)};
\def\v{(-4)};
\edef\w{\fpeval{sqrt(\fpeval{\u^2}+\fpeval{\v^2})}};

\node   at (0,0) {Ans=\fpeval{\w}};

\end{tikzpicture}
\end{document}


• I guess the problem is the argument of ^2 must be always in parenthesis . So we should not call \u^2 we should call (\u)^2
– Aria
Commented Sep 4, 2021 at 21:11
• @Aria Yes, that is the case. Commented Sep 4, 2021 at 21:12