使用宏构建的表格中的数字四舍五入

使用宏构建的表格中的数字四舍五入

我想在以下代码中对生成的表中的小数位进行四舍五入:

\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}

请注意,您可以使用siunitxS来对齐点处的表达式等等,但这里此时没有必要。

显然,你也可以不用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}

在此处输入图片描述

相关内容