TransWikia.com

A strange TikZ numerical anomaly with ln function

TeX - LaTeX Asked by Rémi Peyre on March 3, 2021

Despite TikZ manual’s making clear that PGF’s mathematical libraries should not be expected to be extremely precise, there is a discontinuity when plotting the ln function which I find abnormally large (the discontinuity, which occurs about the argument value 0.125, is of roughly 0.03, which is quite visible on plots—even plots which are not made specifically to show the bug!). Even stranger, the bug disappears when the density of plotting values becomes high enough! See the following MWE and its result:

documentclass{minimal}
usepackage{tikz}
begin{document}
begin{tikzpicture}[x=500cm, y=50cm, thick]
foreach x in {0.120, 0.122, 0.124, 0.126, 0.128, 0.130}
 draw (x, -2.13) -- +(0, -4pt) node[below]{x};
foreach y in {-2.12, -2.10, -2.08, -2.06, -2.04, -2.02}
 draw (0.119, y) -- +(-4pt, 0) node[left]{y};
draw[green] plot[domain=0.12:0.13, samples=95] (x, {ln(x)});
draw[red] plot[domain=0.12:0.13, samples=94] (x, {ln(x)});
end{tikzpicture}
end{document}

Compilation result showing the TikZ numeric bug with plotting ln function

Has anyone any explanations for what happens…?

One Answer

I do not know from where that comes, but, with a recent (works in 3.1.8b, not in 3.1.5b) TikZ/PGF you can use the fpu version for ln:

documentclass{minimal}
usepackage{tikz}
usetikzlibrary{fpu}
begin{document}
begin{tikzpicture}[x=500cm, y=50cm, thick, /pgf/fpu/install only={ln}]
foreach x in {0.120, 0.122, 0.124, 0.126, 0.128, 0.130}
 draw (x, -2.13) -- +(0, -4pt) node[below]{x};
foreach y in {-2.12, -2.10, -2.08, -2.06, -2.04, -2.02}
 draw (0.119, y) -- +(-4pt, 0) node[left]{y};
draw[green] plot[ domain=0.12:0.13, samples=95] (x, {ln(x)});
draw[red] plot[ domain=0.12:0.13, samples=94] (x, {ln(x)});
end{tikzpicture}
end{document}

enter image description here

Answered by Rmano on March 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP