9

pgfmanual.pdf, VII Utilities, Repeating Things: The Foreach Statement, says:

\foreach \x in {0,0.1,...,0.5} {\x, } yields 0, 0.1, 0.20001, 0.30002, 0.40002,

and warns about rounding-errors causing this.

Is doing things in terms of expl3's \fp_step_inline:nnnn generally safer?

(If I do

\documentclass{article}
\begin{document}
\ExplSyntaxOn\fp_step_inline:nnnn {0}{0.1}{0.5}{#1,~}\ExplSyntaxOff
\end{document}

, then I get: "0, 0.1, 0.2, 0.3, 0.4, 0.5, " .)

  • 2
    PGFMath wasn't made to be precise. It was made to make it easy to draw stuff with GPF/TikZ. With line widths of 0.4pt an imprecission of less than 0.0001pt doesn't matter. – Qrrbrbirlbel Aug 18 '23 at 15:03
  • Related: Q446008 and many others. – Qrrbrbirlbel Aug 18 '23 at 15:12
  • 1
    apart from general considerations of calculation accuracy l3fp can store 0.1 exactly whereas pgf can't. – David Carlisle Aug 18 '23 at 15:23
  • Assume, e.g., a loop for drawing some grid. If some lines of the grid are not drawn due to the loop terminating prematurely due to the programmer not taking imprecission/rounding-errors into account, then such an imprecission might matter. ;-) –  Aug 18 '23 at 16:47
  • 2
    rounding errors can happen also with l3fp (or with every other floating point calculator) and programmers must always take them into account. – Ulrike Fischer Aug 18 '23 at 17:23
  • @Cattleya I had a professor of numerical calculus (ages ago) that used to say that using whatever kind of equality (==, <= etc.) with a floating point quantity was an error always... whatever the precision. – Rmano Aug 18 '23 at 17:38
  • @Rmano compile with -Wfloat-equal :-) – David Carlisle Aug 18 '23 at 18:14
  • Use an integer loop {0, ..., 5} and divide that by 10. I'm sure some of the related Qs cover this. TikZ also has the grid path operation. There are many ways to draw a grid. Some better or worse depending on the situation. – Qrrbrbirlbel Aug 18 '23 at 18:41
  • @DavidCarlisle So l3 would be even slower than pgfmath? – cfr Aug 19 '23 at 03:42
  • @cfg most likely, but implementing 16 decimal place ieee conformant arithmetic in tex macros is probably massively over engineered for stepping a loop in tenths. As Qrrbrbirlbel says, use integer arithmetic, it will be orders of magnitude faster and exact. – David Carlisle Aug 19 '23 at 08:26
  • @DavidCarlisle I didn't see this because you didn't ping me ;). I asked because I'm doing something with pgfmath, but it's a bit more involved than stepping in 10ths. I wondered if l3 might be faster, but it doesn't sound like it. I'm looping over integers, but imprecision is still a visible issue. (Or was. I think it will be OK in most cases now.) – cfr Sep 02 '23 at 06:25
  • @cfr g isn't so far from r on a keyboard :-) – David Carlisle Sep 02 '23 at 08:27
  • @DavidCaglisle Indeed. – cfr Sep 03 '23 at 02:48

1 Answers1

11

Yes, \fp_step_inline:nnnn uses decimal floating point values with 16 digits precision, so unless you need more than 16 significant digits, all values only involving numbers expressable as decimal numbers should provide precise results.

As a downside the calculations will be slower though.