![使用宏构建的表格中的数字四舍五入](https://linux22.com/image/423289/%E4%BD%BF%E7%94%A8%E5%AE%8F%E6%9E%84%E5%BB%BA%E7%9A%84%E8%A1%A8%E6%A0%BC%E4%B8%AD%E7%9A%84%E6%95%B0%E5%AD%97%E5%9B%9B%E8%88%8D%E4%BA%94%E5%85%A5.png)
我想在以下代码中对生成的表中的小数位进行四舍五入:
\documentclass[varwidth,border=5]{standalone}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{fp}
\usepackage{xfp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{math}
\usepackage{booktabs}
\begin{document}
\tikzset{fixed point arithmetic}
\tikzmath{
function printfib(\i,\f){print {$f_{\i} = \f$\newline};};
function sequencia(\n) {
real \a, \x, \e;
real \S;
\a = 2;
\x = 0;
for \i in {1,...,\n}{
\x = 0.5*(6 + \x);
\e = abs(6-\x);
};
return {\x};
};
int \i;
real \seq;
real \erro;
for \i in {1,...,20}{
\seq{\i}=sequencia(\i);
\erro{\i}=abs(6-sequencia(\i));
};
}
\def\tmp{$n$ & $f(n)$ & $|erro|$\\ \midrule}%
\edef\iloop{0}%
\loop
\edef\iloop{\the\numexpr\iloop+1}%
\xappto\tmp{$\iloop$ & $\seq{\iloop}$ & $\erro{\iloop}$} %
\gappto\tmp{\\}%
\ifnum\iloop<20\repeat
\begin{tabular}{c|c|c}
\toprule
\tmp
\bottomrule
\end{tabular}
\end{document}
其输出如下:
有没有办法生成小数点位数较少的表格?
太感谢了!
答案1
由于您正在加载xfp
,因此您可以将其用于此。
\documentclass[varwidth,border=5]{standalone}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{fp}
\usepackage{xfp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{math}
\usepackage{booktabs}
\begin{document}
\tikzset{fixed point arithmetic}
\tikzmath{
function printfib(\i,\f){print {$f_{\i} = \f$\newline};};
function sequencia(\n) {
real \a, \x, \e;
real \S;
\a = 2;
\x = 0;
for \i in {1,...,\n}{
\x = 0.5*(6 + \x);
\e = abs(6-\x);
};
return {\x};
};
int \i;
real \seq;
real \erro;
for \i in {1,...,20}{
\seq{\i}=sequencia(\i);
\erro{\i}=abs(6-sequencia(\i));
};
}
\def\tmp{$n$ & \multicolumn{1}{|c}{$f(n)$} & \multicolumn{1}{|c}{$|erro|$}\\ \midrule}%
\edef\iloop{0}%
\loop
\edef\iloop{\the\numexpr\iloop+1}%
\xappto\tmp{$\iloop$ & $\fpeval{round(\seq{\iloop},6)}$ & $\fpeval{round(\erro{\iloop},6)}$} %
\gappto\tmp{\\}%
\ifnum\iloop<20\repeat
\begin{tabular}{c|l|l}
\toprule
\tmp
\bottomrule
\end{tabular}
\end{document}
请注意,您可以使用siunitx
列S
来对齐点处的表达式等等,但这里此时没有必要。
显然,你也可以不用xfp
。在某种程度上,这更优雅或更方便,因为你可以用 pgf 键控制一切:
\pgfkeys{/pgf/number format/.cd,fixed,precision=5}
也因为有更多更好的文档选项。它也不太优雅,因为你必须添加一个\noexpand
。所以,几乎总是,这是一个品味问题。
\documentclass[varwidth,border=5]{standalone}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}
\usetikzlibrary{math}
\usepackage{booktabs}
\begin{document}
\tikzset{fixed point arithmetic}
\tikzmath{
function printfib(\i,\f){print {$f_{\i} = \f$\newline};};
function sequencia(\n) {
real \a, \x, \e;
real \S;
\a = 2;
\x = 0;
for \i in {1,...,\n}{
\x = 0.5*(6 + \x);
\e = abs(6-\x);
};
return {\x};
};
int \i;
real \seq;
real \erro;
for \i in {1,...,20}{
\seq{\i}=sequencia(\i);
\erro{\i}=abs(6-sequencia(\i));
};
}
\pgfkeys{/pgf/number format/.cd,fixed,precision=5}%
\def\tmp{$n$ & \multicolumn{1}{|c}{$f(n)$} & \multicolumn{1}{|c}{$|erro|$}\\ \midrule}%
\edef\iloop{0}%
\loop
\edef\iloop{\the\numexpr\iloop+1}%
\xappto\tmp{$\iloop$ & $\noexpand\pgfmathprintnumber{\seq{\iloop}}$ &
$\noexpand\pgfmathprintnumber{\erro{\iloop}}$} %
\gappto\tmp{\\}%
\ifnum\iloop<20\repeat
\begin{tabular}{c|l|l}
\toprule
\tmp
\bottomrule
\end{tabular}
\end{document}