pgfmath 中的精度问题

pgfmath 中的精度问题

当我计算两个条形图之间的差异时,差异仅显示固定值。我想要精确的值。

\documentclass{article}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\usepackage{pgf}
\usepackage[margin=20mm]{geometry}
\pgfplotsset{compat=1.13}
\newcommand{\PgfmathtruncatemacroFPU}[2]{\begingroup%
%\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathtruncatemacro{#1}{#2}%
\pgfmathsmuggle#1\endgroup}
\begin{document}
\begin{center}
    \begin{tikzpicture}
    \pgfplotstableread[header=true]{
        dir   L    R
        Jan 66.2 121
        Feb 42.9 55.7
        Mar  45.1 73.2
        Apr  51.7 75.9
        May  65.4 88
        Jun 40.4 80.7
        Jul 65.5 86.9
        Aug  70.7 99.1
         Sep  59.2 91.9
        Oct 84 110
        Nov 80.5 117
        Dec  77.3 90
    }\honeydat
    \pgfplotstablegetrowsof{\honeydat}
    \pgfmathtruncatemacro{\numrows}{\pgfplotsretval-1}
        \begin{axis}[width=18cm,ybar,ylabel=Cost (Dollars),
            axis x line*=bottom, % show bottom x-axis and dont remove tick marks
            %axis y line=none,
             %yticklabels={cents},
        %y tick label style={text width=1.3cm,align=right}
            scaled y ticks = true,
            enlarge x limits={abs=0.5cm},
            bar width=13pt,
            xtick={0,...,11},
            xticklabels={Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec},
            ymax=140,
            ytick={0,20,...,140},
            nodes near coords,
            nodes near coords style={
                anchor=south,
            },
            every node near coord/.append style={
                yshift=-13pt,
                font=\tiny
            },
        ]
        \addplot[draw=black,fill=gray!20] table[x expr=\coordindex,y=L] \honeydat;
         \addlegendentry{FLAME}
        \addplot[draw=black,fill=gray!70] table[x expr=\coordindex,y=R] \honeydat;
        \addlegendentry{Traditional}
        \pgfplotsinvokeforeach{0,...,\numrows}{%
         \pgfplotstablegetelem{#1}{L}\of{\honeydat}%
         \edef\myL{\pgfplotsretval}%
         \pgfplotstablegetelem{#1}{R}\of{\honeydat}%
         \edef\myR{\pgfplotsretval}%
         \PgfmathtruncatemacroFPU{\mysign}{sign(\myR-\myL)}
         \ifnum\mysign>0
          \PgfmathtruncatemacroFPU{\mydiff}{\myR-\myL}
          \edef\temp{\noexpand\path[fill=orange!20] (axis cs:#1,\myR)
            rectangle ([xshift=-13pt]axis cs:#1,\myL);
            \noexpand\draw[densely dashed,orange!80!black] 
            (axis cs:#1,\myR) -- ++ (-13pt,0pt) 
                node[pos=0.5,above]{\noexpand\pgfmathprintnumber[fixed,precision=2]{\mydiff}}
              -- ([xshift=-13pt]axis cs:#1,\myL);          
            }
         \else
          \PgfmathtruncatemacroFPU{\mydiff}{\myL-\myR}
          \edef\temp{\noexpand\path[fill=green!20] (axis cs:#1,\myL)
            rectangle ([xshift=13pt]axis cs:#1,\myR);
            \noexpand\draw[densely dashed,orange] 
            (axis cs:#1,\myL) -- ++ (10pt,0pt) 
                node[pos=0.5,above]{\noexpand\pgfmathprintnumber{\mydiff}}
              -- ([xshift=13pt]axis cs:#1,\myR);           
            }
         \fi
         \temp}
        \end{axis}
    \end{tikzpicture}
\end{center}
\end{document}

答案1

这是因为您截断了结果。

替换\PgfmathtruncatemacroFPU{\mydiff}{\myR-\myL}\pgfmathsetmacro{\mydiff}{\myR-\myL}。同样如此\PgfmathtruncatemacroFPU{\mydiff}{\myL-\myR}

在此处输入图片描述

相关内容