5

For some weird reason the following code gives me the wrong plot

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
 \addplot+[jump mark left,samples at={2.5,5,...,20}] {floor(x/2.5)};
\end{axis}
\end{tikzpicture}
\end{document}

the resulting plot

Clearly, at for instance x=15, y should equal to 6. Is this a bug?

Added: I've noticed that the floor() function is superfluous in this case - the jump mark left option works just like a floor function, which produces the correct graph. Still, this does not justify the above erroneous example.

1 Answer 1

5

I'd guess that this is a floating point rounding error (even though \pgfmathparse{floor(((5-2.5)*6)/2.5)}\pgfmathresult correctly prints 6.0). Calculations done using TeX's dimensions have poor precision. One way to fix it is to add a small number to the argument of floor:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}
 \addplot+[jump mark left,samples at={2.5,5,...,20}] {floor(x/2.5+0.0001)};
\end{axis}
\end{tikzpicture}
\end{document}

result

1
  • I agree with the solution - here is just a small footnote for the understanding: The \pgfmathparse experiment is not quite complete -- you need to call \pgfkeys{/pgf/fpu} before evaluating the expression to reproduce the behavior of pgfplots. In that case, it returns 5.9999 as result of 15/2.5. Note that the ... syntax in samples at might also introduce rounding errors. Commented Apr 21, 2011 at 19:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .