有没有简单的方法可以提高三角函数 pgfmath 的精度?

有没有简单的方法可以提高三角函数 pgfmath 的精度?

我想使用 建立一个三角值表pgfmath。但是,精度有点偏差。有没有办法在 内改进这一点TikZ

这与我刚刚发布的类似问题相关:有没有办法用 Expl3 在四舍五入的数字末尾填充零?

这是我当前的工作示例:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{calc,fixedpointarithmetic}

\def\mynum{0}
\def\myvoffset{0pt}

\usepackage[margin=0.5in]{geometry}

\begin{document}

Using \texttt{TikZ}

\begin{tikzpicture}[/pgf/number format/.cd,fixed,precision=4,verbatim]

  \coordinate(UL) at (0,0);

  \node at (UL) {Degrees};
  \node[anchor=west] at ($(UL.west)+(1cm,0)$) {$\sin$};
  \node[anchor=west] at ($(UL.west)+(2.75cm,0)$) {$\cos$};
  \node[anchor=west] at ($(UL.west)+(4.50cm,0)$) {$\tan$};

  \foreach \myn in {1,2,3,...,45}
  {
    \pgfmathparse{int(mod(\myn-1,5))}
    \ifnum\pgfmathresult=0\relax
      \xdef\myvoffset{\dimexpr\myvoffset+1.350\baselineskip}%%
    \else
      \xdef\myvoffset{\dimexpr\myvoffset+1.00\baselineskip}%%
    \fi

    \coordinate (DEG/\myn)   at ($(UL.west)-(0,\myvoffset)$);
    \coordinate (DEG/S/\myn) at ($(DEG/\myn)+(1cm,0)$);
    \coordinate (DEG/C/\myn) at ($(DEG/S/\myn)+(1.75cm,0)$);
    \coordinate (DEG/T/\myn) at ($(DEG/C/\myn)+(1.75cm,0)$);

    \node[anchor=east] at (DEG/\myn) {$\myn^\circ$};
    \pgfmathparse{sin(\myn)} \node[anchor=west] at (DEG/S/\myn) {\texttt{\pgfmathprintnumber{\pgfmathresult}}};
    \pgfmathparse{cos(\myn)} \node[anchor=west] at (DEG/C/\myn) {\texttt{\pgfmathprintnumber{\pgfmathresult}}};
    \pgfmathparse{tan(\myn)} \node[anchor=west] at (DEG/T/\myn) {\texttt{\pgfmathprintnumber{\pgfmathresult}}};

  }

\end{tikzpicture}

\end{document}

在此处输入图片描述

答案1

我不知道pgfmathexpl3您可以像这样编译您的表格:

\documentclass{article}
\usepackage[margin=1cm]{geometry}

\usepackage{array,siunitx}

\sisetup{
  add-decimal-zero,
  round-precision=5,
  round-mode=places,
  round-integer-to-decimal,
  group-digits=false,
  detect-all,
}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\trigtable}{ }
 {
  \__aellett_do_trig:
  \begin{tabular}{r *{3}{ >{\ttfamily}r }}
  & \multicolumn{1}{c}{$\sin$}
  & \multicolumn{1}{c}{$\cos$}
  & \multicolumn{1}{c}{$\tan$}
  \\
  \tl_use:N \l__aellett_body_tl
  \end{tabular}
 }

\tl_new:N \l__aellett_body_tl

\cs_new:Npn \aellett_compute:nn #1 #2
 {
  \num { \fp_to_decimal:n { \fp_eval:n { round ( #1(#2) , 5 ) } } }
 }

\cs_new_protected:Npn \__aellett_do_trig:
 {
  \tl_clear:N \l__aellett_body_tl
  \int_step_inline:nnnn { 1 } { 1 } { 89 }
   {
    \tl_put_right:Nn \l__aellett_body_tl { $##1^\circ$ & }
    \tl_put_right:Nx \l__aellett_body_tl
     { 
      \aellett_compute:nn { sind } { ##1 } &
      \aellett_compute:nn { cosd } { ##1 } &
      \aellett_compute:nn { tand } { ##1 } 
     }
    \int_compare:nTF { \int_mod:nn { ##1 } { 5 } == 0 }
     {
      \tl_put_right:Nn \l__aellett_body_tl { \\[1ex] }
     }
     {
      \tl_put_right:Nn \l__aellett_body_tl { \\ }
     }
   }
  % 90 degrees
  \tl_put_right:Nx \l__aellett_body_tl
   {
    $90^\circ$ &
    \aellett_compute:nn { sind } { 90 } &
    \aellett_compute:nn { cosd } { 90 } &
    --- 
   }

 }

\ExplSyntaxOff

\begin{document}
\tiny
\trigtable

\end{document}

以下是开始:

开始

下面是结尾,只是为了展示 89 度时会发生什么:

结尾

bc我使用五位数字与89 度正切的返回值进行比较:

57.28996163075942465214

答案2

使用 扩展您的选择fixed zerofill

\documentclass{article}
\pagestyle{empty}
\usepackage{tikz}
\usetikzlibrary{calc,fixedpointarithmetic}
\def\mynum{0}
\def\myvoffset{0pt}
\usepackage[margin=0.5in]{geometry}

\begin{document}
%Using \texttt{TikZ}
\begin{tikzpicture}[/pgf/number format/.cd,fixed,precision=4,verbatim, fixed zerofill]
  \coordinate(UL) at (0,0);
  \node at (UL) {Degrees};
  \node[anchor=west] at ($(UL.west)+(1cm,0)$) {$\sin$};
  \node[anchor=west] at ($(UL.west)+(2.75cm,0)$) {$\cos$};
  \node[anchor=west] at ($(UL.west)+(4.50cm,0)$) {$\tan$};
  \foreach \myn in {1,2,3,...,45}
  {
    \pgfmathparse{int(mod(\myn-1,5))}
    \ifnum\pgfmathresult=0\relax
      \xdef\myvoffset{\dimexpr\myvoffset+1.350\baselineskip}%%
    \else
      \xdef\myvoffset{\dimexpr\myvoffset+1.00\baselineskip}%%
    \fi
    \coordinate (DEG/\myn)   at ($(UL.west)-(0,\myvoffset)$);
    \coordinate (DEG/S/\myn) at ($(DEG/\myn)+(1cm,0)$);
    \coordinate (DEG/C/\myn) at ($(DEG/S/\myn)+(1.75cm,0)$);
    \coordinate (DEG/T/\myn) at ($(DEG/C/\myn)+(1.75cm,0)$);
    \node[anchor=east] at (DEG/\myn) {$\myn^\circ$};
    \pgfmathparse{sin(\myn)} \node[anchor=west] at (DEG/S/\myn) {\texttt{\pgfmathprintnumber{\pgfmathresult}}};
    \pgfmathparse{cos(\myn)} \node[anchor=west] at (DEG/C/\myn) {\texttt{\pgfmathprintnumber{\pgfmathresult}}};
    \pgfmathparse{tan(\myn)} \node[anchor=west] at (DEG/T/\myn) {\texttt{\pgfmathprintnumber{\pgfmathresult}}};
  }
\end{tikzpicture}
\end{document}

姆韦

答案3

使用标准 TeX 算法,精度非常有限。如果您需要更精确的结果,您可能需要使用一些外部程序或任何 TeX 扩展,例如 luatex。

使用我的包计算器我得到的结果与使用 Tikz 得到的结果类似:

\documentclass{article}
\usepackage{calculator}
\usepackage{ifthen}

\begin{document}
\newcounter{angle}
\newcommand{\trigfunctions}[1]{%
    #1^{\mathrm{o}} &
    \DEGREESSIN{#1}{\sine}
    \ROUND[4]{\sine}{\sine}
    \sine & 
    \DEGREESCOS{#1}{\cosine}
    \ROUND[4]{\cosine}{\cosine}
    \cosine  & 
    \DEGREESTAN{#1}{\tangent}
    \ROUND[4]{\tangent}{\tangent}
    \tangent& 
    \DEGREESCOT{#1}{\cotangent}
    \ROUND[4]{\cotangent}{\cotangent}
    \cotangent \\}

\small
\[
\begin{array}{*{5}{r}}
\multicolumn{1}{c}{\alpha} & \multicolumn{1}{c}{\sin\alpha} & 
                             \multicolumn{1}{c}{\cos \alpha} & 
                             \multicolumn{1}{c}{\tan\alpha} & 
                             \multicolumn{1}{c}{\cot\alpha} \\
\whiledo{\value{angle}<45}{\stepcounter{angle}\trigfunctions{\theangle}}
\end{array}
\]
\end{document}

触发表

相关内容