pgfmath gibt mir eine falsche Berechnung

pgfmath gibt mir eine falsche Berechnung

Ich versuche, einige Diagramme mit Kugelkoordinaten zu erstellen, und habe ein Problem beim Berechnen einiger Zahlen in PGFPLOTS.

Ich füge hier ein MWE hinzu:

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

Hier ist das Ergebnis:

falsche Berechnung

Ich sollte $-0,4755282581475768$ bekommen

Antwort1

sinund cosmöchten ihr Argument in Grad. Sie können jedoch auch in Radiant angegeben werden, indem sie angehängt werden r(Handbuch, Abschnitt 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}

Antwort2

Ich für meinen Teil würde lieber mitxfp-Paketdie Berechnungen für mich durchzuführen, aufgrund seiner breiten Liste an Funktionen und der Fähigkeit, große Zahlenoperationen zu handhaben, ohnesolche Einschränkungen.

Bildbeschreibung hier eingeben

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

Bildbeschreibung hier eingeben

Antwort3

Ein schmutziger und überkomplizierter Trick

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

Bildbeschreibung hier eingeben

verwandte Informationen