2

The subtraction of the calculated height of an input via sbox (in this case = 116.43562 pt) and 17.68 pt gives an incorrect result at the 5th floating point.

Here is the full tex file

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgf,tikz} 

\newsavebox{\mybox}
\newlength{\myboxht}

\newcommand{\myfunction}[1]{%
\sbox{\mybox}{%
\begin{minipage}[b]{\linewidth}
#1
\end{minipage}%
}

\setlength{\myboxht}{\ht\mybox}%
\pgfmathparse{\myboxht-17.68pt}% 
\usebox{\mybox}

The height: \the\myboxht

Result of mathematical operation: \pgfmathresult
}

\begin{document}
\myfunction{
Test

\begin{tikzpicture}
    \draw (0,0)--(5,3.3);
\end{tikzpicture}

Test}
\end{document}

Compiling it with PDFLatex gives:

enter image description here

Is there a way to control the number of floating points?

2
  • tex is not using floating point arithmetc, it is fixed point with around 5 decimal places, so you can not expect more accuracy than you show unless you use a tikz library to use fp or Lua arithmetic Commented Dec 12, 2022 at 11:53
  • I have also compiled this file (modified) with LuaLatex and it gives the same result. Can you explain a little bit how to use "tikz library" or "Lua arithmetic"? Commented Dec 12, 2022 at 11:58

2 Answers 2

4

Tikz is using fixed point arithmetic. You could use the fp tikz library but even with no arithmetic at all you can not expect 5 decimal place accuracy.

\documentclass{article}

\begin{document}

\dimen0=98.75561pt \the\dimen0

\end{document}

enter image description here

Just assigns a value and prints it out and the 5th decimal place changes from 1 to 2. TeX lengths are always integer multiples of 1sp so you can not store every .00001pt value.

3

TeX uses fixed point arithmetic and TikZ exploits this.

Let's try in a different way. TeX computes the height of the box as 116.43562pt which is 7630725 scaled point (I asked TeX to show this). Similarly, 17.68pt is 1158676 scaled point. The subtraction is 6472049 scaled points.

Saying \dimen0=6472049sp and then \showthe\dimen0 results in TeX showing 98.75563pt because of how TeX arithmetic works. If we do with a calculator

116.43562*65536=7630724.79232
17.68*65536=1158676.48

so you see that TeX rounds up the first dimension and down the second one, which explains the difference in the fifth decimal digit.

Actually, TeX computes the height exactly as 7630725, but the shown value is the nearest value to the division by 65536 with five decimal digits. You can try:

\dimen0=116.43563pt \showthe\dimen0

to get

*\dimen0=116.43563pt \showthe\dimen0
> 116.43562pt.

Not all numbers with five decimal digits correspond to an integral number of scaled points.

The computation, as far as TeX is concerned, is exact. If you want to do floating point arithmetic, you can use \fpeval.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{pgf,tikz} 

\newsavebox{\mybox}
\newlength{\myboxht}

\newcommand{\myfunction}[1]{%
\sbox{\mybox}{%
\begin{minipage}[b]{\linewidth}
#1
\end{minipage}%
}

\setlength{\myboxht}{\ht\mybox}%
\edef\pgfmathresult{\fpeval{round(\myboxht-17.68pt,5)pt}}%
\usebox{\mybox}

The height: \the\myboxht

Result of mathematical operation: \pgfmathresult
}

\begin{document}
\myfunction{
Test

\begin{tikzpicture}
    \draw (0,0)--(5,3.3);
\end{tikzpicture}

Test}
\end{document}

enter image description here

But rounding always get in the way.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.