6

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}
1
  • This is really strange that \u^2 is interpreted as a negative number such as -9.
    – Aria
    Sep 4, 2021 at 21:08

2 Answers 2

9

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.

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

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}

enter image description here

2
  • 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
    Sep 4, 2021 at 21:11
  • 1
    @Aria Yes, that is the case. Sep 4, 2021 at 21:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .