有沒有一種簡單的方法可以提高三角函數的 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

我不知道與pgfmath;你expl3可以像這樣編譯你的表:

\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 演算法,精確度非常有限。

使用我的包計算機我得到的結果與使用 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}

三角表

相關內容