# Decimal places in fixed point arithmetic with negative numbers

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:

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 Dec 15, 2021 at 0:55
• That having said, coming from other programming languages, use round() to print number to X decimal places feel absolutely like the wrong way. Dec 15, 2021 at 0:55
• I want to control the decimal places in the tikzmath code, not in the last LINE. Dec 15, 2021 at 0:59
• Then you already controlled it. -7.647 == -7.6470000000000. What you want is to print the number to 3 decimal places. Dec 15, 2021 at 1:01
• tex.stackexchange.com/questions/350446/… suggests pgfmathprintnumber, which works in this case. Dec 15, 2021 at 1:02

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}