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.

How come that in the code below, \foreach is unable to give me round numbers?

\documentclass{standalone}
\usepackage{tikz}
\usepackage{pgfplots}

\usetikzlibrary{positioning, calc}

\begin{document}
    \begin{tikzpicture}
\foreach\x in {0.1,0.2,...,0.9}\node at (0,{-2*\x}){\tiny\x};
    \end{tikzpicture}
\end{document}
share|improve this question
3  
You should use \pgfmathprintnumber{\x}. I think it is a standard issue of representing floating point numbers in computers, especially when one is using something that was not intended to do mathematical computations. –  Peter Grill Aug 20 at 21:05

2 Answers 2

up vote 7 down vote accepted

The more technical explanation is that your step size - the decimal number 0.1 - has no exact binary representation. In binary it has the infinite representation 0.0001100110011..., similar to 1/3 == 0.3333... in decimal.

So if you represent 0.1 using a finite number of bits, there is always an error. And that is what you are seeing here. As percusse said, this error accumulates when pgf adds the inaccurate step value multiple times.

However this is not always the case: If you choose a step size that can be represented exactly in binary, such as 1/8 or 0.125 (0.001 in binary), there is no error:

\foreach\x in {0,0.125,...,1}\node at (0,{-2*\x}){\tiny\x};

Loop with step size 0.125

You can reduce (but not eliminate) the error by using step size 1 and then scaling it down:

\foreach\x [evaluate=\x as \y using \x/10] in {1,2,...,9}\node at (0,{-2*\y}){\tiny\y};

Loop with step size 1, divided by 10

share|improve this answer

It's because whenever you use the ... notation, TikZ performs calculations such as what is the second number 0.2 and what is the first one; 0.1 so what is the step size...

Then it does 0.2-0.1 operation to obtain the step size and TeX precision contaminates this result. After that TikZ starts adding the contaminated 0.1 to 0.2 (not 0.1 because these are given). You get noisy 0.3,0.4,... so on.

If you write down 0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9 explicitly in the list, you can see that there is no problem printing them properly because no arithmetic is performed on the numbers and they are used as is.

share|improve this answer

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.