pgfmath 給了我一個錯誤的計算

pgfmath 給了我一個錯誤的計算

我正在嘗試用球坐標繪製一些圖,但在計算 PGFPLOTS 中的一些數字時遇到問題。

我在這裡添加一個MWE:

\documentclass[12pt]{standalone}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}


\begin{document}

 % compute angles
    \pgfmathsetmacro\a{-pi/5}
    \pgfmathsetmacro\b{pi/10}
    \pgfmathsetmacro\phil{\a-\b}
    \pgfmathsetmacro\phih{\a+\b}
    \pgfmathsetmacro\thetal{\a-\b}
    \pgfmathsetmacro\thetah{\a+\b}

    \pgfmathsetmacro\s{cos(\thetal)*sin(\phil)}
    % this should be about -0.476
    \pgfmathprintnumber{\s}


\end{document}

結果如下:

錯誤的計算

我應該得到 $-0.4755282581475768$

答案1

sincos希望他們的論點有程度。儘管它們也可以透過附加以弧度給出r(手冊,第 94.3.4 節)。

\documentclass[12pt]{standalone}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}


\begin{document}

 % compute angles
 %   \pgfmathsetmacro\a{-180/5}
 %   \pgfmathsetmacro\b{180/10}
    \pgfmathsetmacro\a{-pi/5 r}
    \pgfmathsetmacro\b{pi/10 r}
    \pgfmathsetmacro\phil{\a-\b}
    \pgfmathsetmacro\phih{\a+\b}
    \pgfmathsetmacro\thetal{\a-\b}
    \pgfmathsetmacro\thetah{\a+\b}

    \pgfmathsetmacro\s{cos(\thetal)*sin(\phil)}
    % this should be about -0.476
    \pgfmathprintnumber{\s}


\end{document}

答案2

對我來說,我寧願選擇xfp 包為我進行計算,因為它具有廣泛的函數列表並且能夠處理大數運算,而無需這樣的限制

在此輸入影像描述

\documentclass[12pt,border=5mm]{standalone}
\usepackage{amsmath}
\usepackage{xfp,siunitx}


\begin{document}
 % compute angles
    \edef\a{-pi/5}
    \edef\b{pi/10}
    \edef\phil{\a-\b}
    \edef\phih{\a+\b}
    \edef\thetal{\a-\b}
    \edef\thetah{\a+\b}

    \edef\s{\fpeval{cos(\thetal)*sin(\phil)}}
    % this should be about -0.476
    \num{\s}
\end{document}

在此輸入影像描述

答案3

骯髒且過於複雜的把戲

% xop.tex
%   run 
% pdflatex xop.tex
% asy xop-*.asy
% pdflatex xop.tex

\documentclass[12pt]{standalone}
\usepackage[pdftex]{graphicx}
\usepackage{amsmath}
\usepackage{tikz,pgfplots}
\usepackage[inline]{asymptote}

\begin{document}

\gdef\setasy#1{\gdef\s{#1}}
\pgfmathsetmacro\s{0}
\begin{asy}
  // compute angles
  real a=-pi/5;
  real b=pi/10;
  real phil=a-b;
  real phih=a+b;
  real thetal=a-b;
  real thetah=a+b;
  real s=cos(thetal)*sin(phil);
  tex("\setasy{"+string(s)+"}");
\end{asy}

\pgfmathprintnumber{\s} (\s)

\end{document}

在此輸入影像描述

相關內容