tikz-pgfplot 中 int() 函數的奇怪結果

tikz-pgfplot 中 int() 函數的奇怪結果

也許我在 tikz-pgfplot 中發現了一個新錯誤。你能證實這個奇怪的結果嗎:

\pgfmathparse{int(11/2)}

返回0

@噴射

這是我的一段程式碼,它產生了這個問題int()

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}
    \pgfmathparse{int(11/2)}
    \pgfmathresult %correctly returns 5 as JeT said, but
    
\begin{figure} % this plot is wrong as int(11/2) returns 0 for instance
    \centering
    \begin{tikzpicture}[
        scale=1.0,
        declare function={
            bit(\c,\i)=int(\c/2^\i)-2*int(\c/2^(\i+1)); %Compute the n-th binary digit of c. Both n and c must be integers
        }
        ]
        \begin{axis}[
            width=10 cm,
            height=6 cm,
            xlabel = {\large $C$},
            ylabel = {\large digit 0 of $C$},
            minor tick num=5,
            xmajorgrids,
            ymajorgrids,
            xmin = 0,
            xmax = 20,
            ymin = 0,
            ymax = 1,
            ]
            \addplot [samples at={0,...,20}, ycomb, mark=*] {bit(x,0)}; % check for debugging purpose
        \end{axis}
    \end{tikzpicture}
    \caption{Test int()}
    \label{fig:Test int()}
\end{figure}
    
\end{document}

我能弄清楚出了什麼問題。

答案1

環境中存在循環的不同方法axis

在此輸入影像描述

\documentclass{standalone}
%\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.18}

\begin{document}

% \begin{figure}
%     \centering
    \begin{tikzpicture}[
        scale=1.0,
        declare function={
            bit(\c,\i)=int(\c/2^\i)-2*int(\c/2^(\i+1)); %Compute the n-th binary digit of c. Both n and c must be integers
        }
    ]
        \begin{axis}[
            width=10 cm,
            height=6 cm,
            xlabel = {\large $C$},
            ylabel = {\large digit 0 of $C$},
            minor tick num=5,
            xmajorgrids,
            ymajorgrids,
            xtick={0,...,20},
            ytick={0,1},
            xmin = 0,
            xmax = 20,
            ymin = 0,
            ymax = 1,
            ]
            \foreach \x in {0,...,20}{% here you'll use a loop in the axis
                \pgfmathparse{bit(\x,0)}
                \edef\temp{\noexpand\addplot coordinates {(\x, \pgfmathresult)};}
                \temp
            }
        \end{axis}
    \end{tikzpicture}
%     \caption{Test int()}
%     \label{fig:Test int()}
% \end{figure}

\end{document}

相關內容