1

I'm trying to evaluate the following expression with \pgfmathparse, but I get error messages:

\pgfmathparse{\sqrt{{5.8186/{\pi}}}}\pgfmathresult\

Any idea why?

3
  • 1
    \pi is a character, not the actual value of π. Mar 13, 2018 at 17:47
  • @PhelypeOleinik Thanks, I was suspecting it had something to do with that. Is there another way to pass the 'real' pi (and not just 3.14, please)?
    – Anakhand
    Mar 13, 2018 at 17:50
  • My answer would be pretty much the same as Heiko's, except that I didn't know that pi was a possible value, so I would use rad(180)... Don't ask :P Mar 13, 2018 at 17:54

2 Answers 2

3

The commands \sqrt and \pi are used to typeset a square root sign and the Greek letter pi, not for computing values.

You can use a different method:

\documentclass{article}
\usepackage{xfp}

\begin{document}

\fpeval{sqrt(5.8186/pi)}

\fpeval{round(sqrt(5.8186/pi),5)}

\fpeval{trunc(sqrt(5.8186/pi),5)}

\end{document}

No need to do two steps.

enter image description here

Compare with the result of the standard Unix bc program:

> echo 'sqrt(5.8186/(4*a(1)))' | bc -l
1.36092538507774348937
4

Math expressions in pgf/TikZ use a different syntax than TeX's math typesetting (sqrt instead of \sqrt, pi instead of \pi, parentheses instead of braces):

\documentclass[tikz]{standalone}

\begin{document}
  \pgfmathparse{sqrt(5.8186/pi)}%
  \pgfmathresult

  % Rounding, three fractional digits
  \pgfmathparse{round(1000*sqrt(5.8186/pi))/1000}%
  \pgfmathresult
\end{document}

Result

2
  • Thanks! BTW, know any way to set the desired decimal precision?
    – Anakhand
    Mar 13, 2018 at 17:59
  • @Anakhand See updated answer. Mar 13, 2018 at 18:08

You must log in to answer this question.

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