Graphics, Figures & TablesTikZ: How can I fix the numbers to be error free?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

TikZ: How can I fix the numbers to be error free?

Post by topsquark »

I am working to convert a decimal to a fraction. It works for some numbers, but for others I have the following problem:

Code: Select all

\usetikzlibrary{math}
\begin{tikzpicture}
     \tikzmath {
               \num=0.4;
               for \k in {1,...,10} {
                    \a=\k*\num;
                    { \node at (0,-\k) {\a}; };
              };
     };
\end{tikzpicture}
When I multiply the number \num by \k, it isn't really multiplying \num by an integer. It's apparently not even multiplying \num by 1.0, 2.0, etc.

I'm not quite sure how I'm going to handle something like \num=0.3333333, but so far the only values of \num I can get this to work on end in .0, .5, and .25 due to the inaccuracy. Using round() or int() doesn't seem to make any difference.

Any thoughts?

-Dan

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

TikZ: How can I fix the numbers to be error free?

Post by rais »

you could try

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{math, fixedpointarithmetic}
\begin{document}
\begin{tikzpicture}[fixed point arithmetic]% or use a scope
     \tikzmath {
               \num=0.4;
               for \k in {1,...,10} {
                    \a=\k*\num;
                    { \node at (0,-\k) {\a}; };
              };
     };
\end{tikzpicture}
\end{document}
Alternatively, you could try the fpu library that doesn't require another package.

KR
Rainer
topsquark
Posts: 71
Joined: Wed Oct 05, 2022 10:30 pm

TikZ: How can I fix the numbers to be error free?

Post by topsquark »

rais wrote:you could try

Code: Select all

\documentclass{article}
\usepackage{tikz}
\usepackage{fp}
\usetikzlibrary{math, fixedpointarithmetic}
\begin{document}
\begin{tikzpicture}[fixed point arithmetic]% or use a scope
     \tikzmath {
               \num=0.4;
               for \k in {1,...,10} {
                    \a=\k*\num;
                    { \node at (0,-\k) {\a}; };
              };
     };
\end{tikzpicture}
\end{document}
Alternatively, you could try the fpu library that doesn't require another package.

KR
Rainer
Okay. I just read up on the library. I guess I had assumed that something like that was already enabled!

Thanks for the help.

-Dan
Post Reply