Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
4  
Counters can only store integer values. –  egreg Dec 14 '12 at 17:36
    
As pointed out by @egreg counters can only store integers. I'm not aware of a package that helps approximating squre roots. What do you need this for? –  Marc van Dongen Dec 14 '12 at 17:39
    
There are a range of packages that let you calculate square roots (expl3's l3fp module, calculator, fp) but that won't help for storing the results in a counter for the reasons already mentioned. –  cgnieder Dec 14 '12 at 17:50
2  
With \usepackage{pgf}, you can use \pgfmathsetmacro{\squarerootofmynumber}{sqrt(\value{mynumber})}. –  Peter Grill Dec 14 '12 at 18:02
add comment

3 Answers 3

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}
share|improve this answer
    
You can \usepackage{pgfmath} if you only want the calc features, and not the whole drawing engine. Perhaps also is worth to mention that if you have tikz or beamer, then pgfmath is already loaded. –  JLDiaz Dec 14 '12 at 19:05
    
@JLDiaz I tried that with the example above but it gave ! Undefined control sequence. l.108 \pgfkeys and I was to lazy to investigate why that was... –  cgnieder Dec 14 '12 at 19:07
    
You are right. Then it does not work as advertised in the manual :( –  JLDiaz Dec 14 '12 at 19:13
2  
Nice answer. As far as pgfmath see: Is it possible to load pgfmath without loading the full pgf package?. –  Peter Grill Dec 14 '12 at 19:36
add comment

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

share|improve this answer
add comment

The xintfrac package will calculate a square root as a floating number of a given precision. You can also output it then as a fixed point number with a given number of digits after the decimal mark. Also, xintexpr has simpler syntax. Examples using LaTeX:

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

\newcounter{cnt}
\setcounter{cnt}{17}

% 24 digits of precision
$\sqrt{17}\approx{}$\xintFloatSqrt [24]{\value{cnt}}

% default is 16, let's round to a fixed point number
% with 8 digits after decimal mark

$\sqrt{17}\approx{}$\xintRound {8}{\xintFloatSqrt {\value{cnt}}}

% There is also a purely integral version which computes
% floor(sqrt(N))

$\sqrt{17000000}\approx{}$\xintiSqrt {17000000}

% One can use functional expressions:

\xintNewExpr \MySquareRoot [1]{round(sqrt(#1,24),8)}

$\sqrt{5}\approx{}$\MySquareRoot {5}

% Except the integer only \xintiSqrt, the others handle also 
% fractions or fixed point numbers or scientific numbers on input

$\sqrt{\frac{71}{27}}\approx{}$\MySquareRoot {71/27}

$\sqrt{1.234\mathrm{e}12}\approx{}$\MySquareRoot {1.234e12}

$\sqrt{3}\approx{}$\xinttheexpr round(sqrt(3,60),59)\relax

\end{document}

square roots

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.