TransWikia.com

Specify the precision in mathparse calculus

TeX - LaTeX Asked on August 13, 2021

I have defined the function f(x) = (sqrt(x)-1)/(x-1) and I need to calculate several values for f(x).
I have developed the following code, but, I cannot to specify the precision for f(x), for example, with 5 decimal places.
Anyone can help me please?

documentclass[tikz, border=.5cm]{standalone}
usepackage[portuguese]{babel}
usepackage[utf8]{inputenc}
usepackage{tkz-fct}   
usepackage{multido}
usetikzlibrary{calc,math}
usepackage{float}
usepackage{amsmath}

newcommandf[1]{ pgfmathparse{((#1)^0.5 - 1)/(#1 -1)} pgfmathresult }

begin{document}

foreach x in {2,1.5,1.2,1.19,1.189,...,1.001}{
begin{tikzpicture}[scale=0.8]
clip (-3.5,-2) rectangle (4,2);
node[above] (2) at (0,0)  {Vamos avaliar o limite $displaystylelim_{x to 1} frac{sqrt{x}-1}{x-1}$:};
node[below] (2) at (0,0)  {O valor da função em $pgfmathprintnumber{x}$ é boxed{$f{x}$}};
end{tikzpicture}
}
end{document}

One Answer

I'm afraid that the standard floating point engine in TikZ/PGF is not apt for this task.

You get much better results with the engine in expl3.

documentclass{article}
usepackage[portuguese]{babel}
usepackage[utf8]{inputenc}
usepackage{amsmath}
usepackage{xfp}

newcommandf[1]{%
  fpeval{round((sqrt(#1)-1)/(#1-1),5)}%
}

ExplSyntaxOn
NewDocumentCommand{fpforeach}{o mmm +m}
 {
  IfValueT{#1}
   {
    clist_map_inline:nn { #1 } { #5 }
   }
  fp_step_inline:nnnn { #2 } { #3 } { #4 } { #5 }
 }
ExplSyntaxOff

begin{document}

fpforeach[2,1.5,1.2]{1.19}{-0.001}{1.001}{%
  O valor da função em $#1$ é boxed{$f{#1}$}par
}

end{document}

The command f defines the function, with rounding to five decimal digits.

I added a “for each” cycle, where the optional argument is a list of “exceptional values” to be evaluated first, then you can specify the starting point, the step and the final point. The last argument is what every value in the loop is passed to, using #1 to denote the current value.

Here are the first lines of the result.

enter image description here

The last lines.

enter image description here

I'm personally against this way to present limits, because it is not a good explanation of how limits work (because one has to do backwards, not forward). Anyway, I acknowledge that's used in many books. Unfortunately. ;-)

Answered by egreg on August 13, 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