[Tex/LaTex] How to solve the 10.09999 rounding problem with pgfmath

pgfmath

I'm trying to create a technical drawing of a magnetic write head with dimensions on it. I'm using some macros which help a lot from here: Dimensioning a technical drawing.

\documentclass{beamer}

\usepackage{tikz}
\usetikzlibrary{positioning,arrows,shapes.arrows,shapes.geometric,calc,decorations.markings}
\usepackage{color}

\pgfarrowsdeclarecombine{|<}{>|}{|}{|}{latex}{latex}
\def\Dimline[#1][#2][#3]{
    \begin{scope}[>=latex] % redef arrow for dimension lines
        \draw let \p1=#1, \p2=#2, \n0={round(veclen(\x2-\x1,\y2-\y1))} in [|<->|,
        decoration={markings, % switch on markings
                mark=at position .5 with {\node[#3,scale=0.6] at (0,0) {\DimScale{\n0}};},
        },
        postaction=decorate] #1 -- #2;
    \end{scope}
}
%% The following macro is used to scale a dimension from points to the
%% display scale.  The following code divides the number of points by
%% 28.4 to roughly get the width in centimeters (rounding to the
%% nearest millimeter):
\def\DimScale#1{\pgfmathparse{round(#1/28.4*100)/10}\pgfmathresult nm}

\newcommand{\headDesignOne}[3]{
    \draw[step=1cm,gray!50,very thin] (-10.5,-10.5) grid (10.5,10.5);
    \draw[->,thin](-10,0)--(10,0);\draw[->,thin](0,-10)--(0,10);
    \coordinate (yOffset) at (0,0);
    \coordinate (O) at (0,0);
    \draw[very thin,blue!30] (yOffset) circle (#3);

    % coordinates of the main pole
    \draw[fill=gray, fill opacity=0.5]
    ($(270-#1:#3)+(yOffset)$) coordinate (A) -- 
    ($(90+#2:#3)+(yOffset)$) coordinate (B) -- 
    ($(90-#2:#3)+(yOffset)$) coordinate (C) --
    ($(270+#1:#3)+(yOffset)$) coordinate (D)-- cycle;
    \Dimline[($(B)+(0,-0.5)$)][($(C)+(0,-0.5)$)][below];
    \Dimline[($(A)+(0,0.5)$)][($(D)+(0,0.5)$)][above];
    \Dimline[($(D)+(3.5,0)$)][($(D|-C)+(3.5,0)$)][right];\draw(D)--+(3.5,0);\draw(D|-C)--+(3.5,0);
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Begin document           %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\begin{frame}{Conventional writer design 1}
  \begin{tikzpicture}[scale=0.4]
    \path[use as bounding box](-10,-10) rectangle (10,10);     % Set the BB
    \headDesignOne{20}{40}{5}
  \end{tikzpicture}
\end{frame}
\end{document}

Unfortunately I can't paste the result as I'm new, but when I typeset the above, one of the dimensions that comes out is 85.59999

output of code

I have 2 questions:

  1. How do I make the 85.59999 to be 85.6?
  2. How do i take the 85.6 or the 64.4 that was computed in the \Dimline function and re-use that number? For example I want to put that number in a table listing some of the key parameters for this design.

Best Answer

The problem is the inaccurate division step after the rounding operation, which is necessary because you can't specify the number of decimal digits for the rounding operation. A solution would be to use \pgfmathprintnumber[precision=1]{\pgfmathresult} to round and output the number. If you want to use the rounded number elsewhere, you can use \pgfmathprintnumberto{\pgfmathresult}{\roundednumber} to store the rounded number to a macro.

Changing the scaling function to

\def\DimScale#1{\pgfmathparse{#1/2.84}\pgfmathprintnumberto[precision=1]{\pgfmathresult}{\roundednumber} \roundednumber nm}

yields

number rounded with pgfprintnumberto

Related Question