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}

関連情報