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

sin引数を度単位で指定します。ただし、 (マニュアル、セクション 94.3.4)cosを追加することでラジアン単位で指定することもできます。r

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

ここに画像の説明を入力してください

関連情報