我想得到存储在计数器中的数字的平方根。我一直在使用计算用于求和和乘法运算的包,但我找不到类似的东西
\setcounter{squarerootofmynumber}{\value{mynumber}^{1/2}}
或者类似的东西。你知道路吗?
答案1
这里有四种计算数字平方根的方法(精度不同)。但是,除非结果为整数,否则不能将其存储在计数器中。
这
calculator
包裹\documentclass{article} \usepackage{calculator} \newcounter{mycount} \setcounter{mycount}{7} \begin{document} \SQUAREROOT{\themycount}{\solution}% $\sqrt{\themycount}=\solution$ \end{document}
这
fp
包裹\documentclass{article} \usepackage{fp} \newcounter{mycount} \setcounter{mycount}{7} \begin{document} \FProot\solution{\themycount}{2}% \FPround\solution\solution{5}% $\sqrt{\themycount}=\solution$ \end{document}
这
pgf
包(感谢 Peter Grill 的提醒)\documentclass{article} \usepackage{pgf} \newcounter{mycount} \setcounter{mycount}{7} \begin{document} \pgfmathsetmacro{\solution}{sqrt(\themycount)}% $\sqrt{\themycount}=\solution$ \end{document}
模块
l3fp
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}
所有这些例子都给出了
将结果存储在计数器中要求结果为整数。包 2-4 具有对结果进行舍入的方法,以便之后设置计数器。以下是示例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}
答案2
lualatex
具有良好精度的解决方案
\documentclass{minimal}
\newcounter{mycount} \setcounter{mycount}{7}
\begin{document}
\directlua{tex.print(math.sqrt(\themycount))}
\end{document}
答案3
使用sqrt
xintexpr包您可以计算具有任意精度的平方根。
[P]
我已经更新了这个答案来说明 (v1.1)中的新可选参数和(v1.1a)\xintfloatexpr
中的四舍五入积分平方根的使用。\xintiiexpr
另外,可以直接使用包中的底层宏xint 和 xintfrac. 查看文档。
\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}