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}

관련 정보