Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

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

share|improve this answer
    
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. –  Christian Feuersänger Apr 21 '11 at 19:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.