4

I have the code:

\documentclass[tikz,border=5]{article}
\usepackage{tikz}
\usepackage{xfp}
\usepackage{amsmath}
\usetikzlibrary{math}
\usepackage{fp}
\usetikzlibrary{fixedpointarithmetic}

\begin{document}

\tikzset{fixed point arithmetic} \tikzmath{ real \x; real \y; % \x=-1.3754; \y = \fpeval{round(\x*5.56,3)}; }

\tikzset{fixed point arithmetic} \tikzmath{ real \z; real \w; % \z=1.3754; \w = \fpeval{round(\z*5.56,3)}; }

\vspace{3cm}

O valor de $x$ é: \fpeval{round(\x,3)} \ O valor de $y$ é: \y \

O valor de $z$ é: \fpeval{round(\z,3)} \ O valor de $w$ é: \w \

\end{document}

whose output is the following:

enter image description here

When I operate negative numbers in tikzmath, the decimal places of the result is incontrolable. However when the calculation deal with positive real numbers, I set the decimal places with \fpeval{round(x,3)} (to three decimal places) and I can to control the precision. My question is how can I fix this? Why fixed point arithmetic is noisy with negative numbers? I would to set the decimal places in the tikzmath code space, not in the text. Many thanks!

  • Replace last line with O valor de $y$ é: \fpeval{round(\y,3)} works. No idea why – user202729 Dec 15 '21 at 00:55
  • That having said, coming from other programming languages, use round() to print number to X decimal places feel absolutely like the wrong way. – user202729 Dec 15 '21 at 00:55
  • I want to control the decimal places in the tikzmath code, not in the last LINE. – Angelo Aliano Filho Dec 15 '21 at 00:59
  • 2
    Then you already controlled it. -7.647 == -7.6470000000000. What you want is to print the number to 3 decimal places. – user202729 Dec 15 '21 at 01:01
  • https://tex.stackexchange.com/questions/350446/print-3-decimals-in-tikz-without-using-standard-form suggests pgfmathprintnumber, which works in this case. – user202729 Dec 15 '21 at 01:02
  • Not controlled in the tikzmath mode when the number is negative. When Is positive the command round works. Try! – Angelo Aliano Filho Dec 15 '21 at 01:05
  • It does work in that the resulting value is correct. I don't think there's anything in the documentation that states that the result is guaranteed to be stored in the minimal form. – user202729 Dec 15 '21 at 01:06
  • Alternatively siunitx can be used too. https://tex.stackexchange.com/questions/599426/xfp-zerofill-at-fpeval – user202729 Dec 15 '21 at 01:21
  • Not solved yet...... – Angelo Aliano Filho Dec 15 '21 at 01:25
  • I think you're misunderstanding something. Either you only care only about the value of the numbers; in that case the produced value and the expected value is exactly equal; or you care about how it looks like in text, then use the text formatting commands. – user202729 Dec 15 '21 at 01:33
  • I am concerned about the appearance and value of the result. The only thing I don't understand is because Tikz puts too many zeros when the calculation involves negative numbers and I can't get those zeros inside the tikzmath environment – Angelo Aliano Filho Dec 15 '21 at 01:37
  • If you are concerned about the appearance then use the text formatting command? ■ While the internal storage of the value is inconsistent; it's not a bug because the value is correct; nevertheless just wait a bit and see if the TikZ developers are interested in changing the behavior (perhaps some efficiency improvement or something). – user202729 Dec 15 '21 at 01:39

1 Answers1

1

Use the numprint package. the "autolanguage" option adjusts the unit and thousand separators according to the babel package option, in this case Brazilian Portuguese.

\documentclass[tikz,border=5]{article}
\usepackage[brazil]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz} \usepackage{xfp} \usepackage{amsmath} \usetikzlibrary{math} \usepackage{fp} \usetikzlibrary{fixedpointarithmetic} \usepackage[autolanguage]{numprint}

\begin{document}

\tikzset{fixed point arithmetic} \tikzmath{ real \x; real \y; % \x=-1.3754; \y = \fpeval{round(\x*5.56,3)}; }

\tikzset{fixed point arithmetic} \tikzmath{ real \z; real \w; % \z=1.3754; \w = \fpeval{round(\z*5.56,3)}; }

\vspace{3cm}

\nprounddigits{3} O valor de $x$ é: \numprint{\x} \ O valor de $y$ é: \numprint{\y} \

\nprounddigits{4} O valor de $z$ é: \numprint{\z} \ O valor de $w$ é: \numprint{\w} \

\end{document}

enter image description here

NCrown
  • 353