17

I would like to get the square root of a number which I have stored into a counter. I have been using the calc package for summation and multiplication operations, but I cannot find anything like

\setcounter{squarerootofmynumber}{\value{mynumber}^{1/2}}

or something like that. Do you know the way?

lockstep
  • 250,273
Doxxik
  • 251

3 Answers3

23

Here are four ways for calculating the square root of a number (with varying precision). However, the result cannot be stored in a counter unless it is an integer.

  1. The calculator package

    \documentclass{article}
    \usepackage{calculator}
    \newcounter{mycount}
    \setcounter{mycount}{7}
    \begin{document}
    \SQUAREROOT{\themycount}{\solution}%
    $\sqrt{\themycount}=\solution$
    \end{document}
    
  2. The fp package

    \documentclass{article}
    \usepackage{fp}
    \newcounter{mycount}
    \setcounter{mycount}{7}
    \begin{document}
    \FProot\solution{\themycount}{2}%
    \FPround\solution\solution{5}%
    $\sqrt{\themycount}=\solution$
    \end{document}
    
  3. The pgf package (thanks to Peter Grill for the reminder)

    \documentclass{article}
    \usepackage{pgf}
    \newcounter{mycount}
    \setcounter{mycount}{7}
    \begin{document}
    \pgfmathsetmacro{\solution}{sqrt(\themycount)}%
    $\sqrt{\themycount}=\solution$
    \end{document}
    
  4. The l3fp module of the l3kernel

    \documentclass{article}
    \usepackage{expl3}
    \ExplSyntaxOn
    \cs_new_eq:NN \calculate \fp_eval:n
    \ExplSyntaxOff
    \newcounter{mycount}
    \setcounter{mycount}{7}
    \begin{document}
    $\sqrt{\themycount}=\calculate{round(sqrt(\themycount),5)}$
    \end{document}
    

All of these examples give

enter image description here

Storing the result in a counter requires the result to be an integer. Packages 2-4 have means to round a result which would allow to set a counter afterwards. Here is an example with l3fp:

\documentclass{article}
\usepackage{expl3}
\ExplSyntaxOn
\cs_new_eq:NN \calculate \fp_eval:n
\ExplSyntaxOff
\begin{document}
\newcounter{mycount}
\setcounter{mycount}{7}
\edef\solution{\calculate{round(sqrt(\value{mycount}),0)}}
\setcounter{mycount}{\solution}\themycount
\end{document}
cgnieder
  • 66,645
17

a solution with lualatex with a good precision

\documentclass{minimal}
\newcounter{mycount} \setcounter{mycount}{7}
\begin{document}

\directlua{tex.print(math.sqrt(\themycount))}

\end{document}

enter image description here

5

Using the sqrt function which is provided with the xintexpr package you can calculate square roots with arbitrary precisions.

I have updated this answer to illustrate use of the new [P] optional argument to \xintfloatexpr (v1.1) and rounded integral square root in \xintiiexpr (v1.1a).

Also, one can use directly the underlying macros from packages xint and xintfrac. See the documentation.

\documentclass{article}
\usepackage{xintexpr}
\begin{document}

$\sqrt{17}$ with various precisions:


% Square root with 16 digits of precision:
\xintthefloatexpr sqrt(17)\relax

% If you have a LaTeX counter, use \value:
\newcounter{cnt}
\setcounter{cnt}{17}

% \xintthefloatexpr sqrt(\value{cnt})\relax

% Square root with more digits:
\xintthefloatexpr [24] sqrt(17,24)\relax
% The 24 is needed twice, once for sqrt to compute with 
%  24 digits, and once for \xintthefloatexpr not to
%  round to only 16 digits.
% The [24] requires xintexpr v1.1 or later.

% Or change globally the precision:
\xintDigits := 48;
\xintthefloatexpr sqrt(17)\relax

% There is a variant which works only with (big) integers.
% You can use choose between sqrt which truncates and sqrtr
% which rounds. The latter requires xintexpr v1.1a or later.

Integer approximation to $\sqrt{170000000000000000}$:

\xinttheiiexpr sqrt(170000000000000000)\relax{} (truncated using \verb|sqrt|)

\xinttheiiexpr sqrtr(170000000000000000)\relax{} (rounded using \verb|sqrtr|)% v1.1a or later

% This can be put in a LaTeX counter if <2^31.

A counter can not hold larger than $\number"7FFFFFFF$.

\setcounter{cnt}{\xinttheiiexpr sqrtr(170000000000000000)\relax}
The counter holds: $\thecnt\approx \sqrt{170000000000000000}$.

\setcounter{cnt}{\xinttheiiexpr sqrtr(1700000000000000000)\relax}
The counter holds $\thecnt\approx \sqrt{1700000000000000000}$.

% Rather than expressions with the function sqrt, one
% can also use directly the underlying macros provided 
% by xint and xintfrac package. See xint documentation.
\end{document}

square roots