Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweaked foreach loops in example diagrams to avoid a floating-point rounding error. #1050

Merged
merged 2 commits into from
Sep 25, 2021

Conversation

3geek14
Copy link
Contributor

@3geek14 3geek14 commented Sep 18, 2021

…-point rounding error.

Signed-off-by: 3geek14 [email protected]

Motivation for this change

An example in The Pic Syntax uses {0, 0.1, ..., 1} for a foreach loop. Due to floating-point arithmetic, this leaves out the value 1. Rather than changing it to 1.01 or adding , 1 to the list or using integers and dividing by 10 later…I changed the step to 0.25 to avoid the rounding error while maintaining the simplicity of the example.

Checklist

Please check the boxes below and signoff your commits to explicitly
state your agreement to the Developer Certificate of Origin:

  • [ ✓ ] Code changes are licensed under GPLv2 + LPPLv1.3c
  • [ ✓ ] Documentation changes are licensed under FDLv1.2

@muzimuzhi
Copy link
Member

Nice catch. I'm thinking if we should mention the rounding errors and their workarounds when foreach is first introduced in tutorial, more specific sec. 2.20 "Repeating Things: For-Loops", or just lead readers to sec. 88 "Repeating Things: The Foreach Statement", paragraph "The dots notation".

@3geek14
Copy link
Contributor Author

3geek14 commented Sep 19, 2021

I'm not sure there's a good way to fit it into Karl's tutorial without it feeling out of place. I agree it's an important thing to remind users of, and I'm pretty certain I've made this exact error before. I'll try to spend more time thinking about this, and I'll make an issue or another PR if I come up with something I like.

* Changed to 1/8 steps to be more similar to original 1/10 steps

Signed-off-by: 3geek14 <[email protected]>
@3geek14
Copy link
Contributor Author

3geek14 commented Sep 19, 2021

There were a few other places where the same rounding error came up. I did a search for 0.1, to find them all. (Thankfully, not too many coordinates have that, so it was reasonable to search.) I also changed to steps of 0.125 rather than 0.25, as it's visually more similar to the original, keeping a relatively dense step, while still (I think) easily interpreted by readers as a simple 1/n.

I also searched for 0.2,, 0.3,, and so on (excluding 0.5,) to see if there were similar errors. The only other one I found is on the cover:

\foreach \i in {0.5,0.6,...,2}
  \fill
    [white,opacity=\i/2,
     decoration=Koch snowflake,
     shift=(horizon),shift={(rand*11,rnd*7)},
     scale=\i,double copy shadow={
       opacity=0.2,shadow xshift=0pt,
       shadow yshift=3*\i pt,fill=white,draw=none}]
    decorate {
      decorate {
        decorate {
          (0,0)- ++(60:1) -- ++(-60:1) -- cycle
        } } };

For the cover, I think the visual design (for both picture and code) matters a lot more than it does in the examples I was editing, so I left it alone. I think my suggestion would be to use {5,6,...,20}, and then to replace \i/2, \i, and 3*\i with \i/20, \i/10, and 0.3*\i.

Of course, it's possible that it's intentional to have only 15 (rather than 16) snowflakes and for none of them to have opacity 1 (the highest currently around 0.95). In this case, I think it would be better to use {0.5,0.6,...,1.95} to make it look more intentional.

@3geek14 3geek14 changed the title Tweaked an example diagram in 18.2 The Pic Syntax to avoid a floating… Tweaked foreach loops in example diagrams to avoid a floating-point rounding error. Sep 19, 2021
3geek14 added a commit to 3geek14/pgf that referenced this pull request Sep 20, 2021
…t floating-point arithmetic to the first introduction of `foreach` in Karl's tutorial.

Signed-off-by: 3geek14 <[email protected]>
3geek14 added a commit to 3geek14/pgf that referenced this pull request Sep 20, 2021
…t floating-point arithmetic to the first introduction of `foreach` in Karl's tutorial.

Signed-off-by: 3geek14 <[email protected]>
3geek14 added a commit to 3geek14/pgf that referenced this pull request Sep 20, 2021
Per a suggestion from @muzimuzhi, add a warning about floating-point arithmetic
to the first introduction of `foreach` in Karl's tutorial.

Signed-off-by: 3geek14 <[email protected]>
3geek14 added a commit to 3geek14/pgf that referenced this pull request Sep 20, 2021
Per a suggestion from @muzimuzhi on pgf-tikz#1050, add a warning about floating-point arithmetic
to the first introduction of `foreach` in Karl's tutorial.
3geek14 added a commit to 3geek14/pgf that referenced this pull request Sep 20, 2021
Per a suggestion from @muzimuzhi on pgf-tikz#1050, add a warning about floating-point arithmetic
to the first introduction of `foreach` in Karl's tutorial.

Signed-off-by: 3geek14 <[email protected]>
@muzimuzhi
Copy link
Member

At the beginning I thought maybe the two examples not related to animations could be adopted to show workarounds for rounding errors, but now I decided to keep drawing examples simple and perhaps extend the doc for \foreach to discuss more about the workaround, later or in your #1053.

Hence approved and I'm merging this.

@muzimuzhi muzimuzhi merged commit a18a8d6 into pgf-tikz:master Sep 25, 2021
@3geek14 3geek14 deleted the rounding-error-pics branch January 1, 2022 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants