# What is the correct syntax to do arithmetic for length and dimensionless macros?

I want to set papersize based on PSTricks settings. The PSTricks settings may be a value without dimension and/or a value with dimension.

Honestly I have not read the TeXBook because of my laziness. I just read some examples from others but I don't know the hidden theory. For short, I have two questions as follows.

1. why does the following calculation not work? How should I do arithmetic for values with dimension and without dimension.
2. what is the best practice to reduce the rounding error for complicated length and dimensionless value calculation?
\documentclass{minimal}
\usepackage{pstricks}
\psset
{
xunit=2cm,
yunit=1cm
}

\newcommand\Left{-4}
\newcommand\Right{4}
\newcommand\Bottom{-4}
\newcommand\Top{4}

\topmargin=-72.27pt
\oddsidemargin=-72.27pt
\parindent=0pt
\special{papersize=\the\paperwidth,\the\paperheight}

\begin{document}
\begin{pspicture}(\Left,\Bottom)(\Right,\Top)
\psframe(\Left,\Bottom)(\Right,\Top)
\end{pspicture}
\end{document}

-

You are actually doing \psxunit times 4-(-4)+20 = 28.

\paperwidth=\dimexpr\psxunit*\numexpr(\Right-\Left+2*\Padding)\relax


The 2\Padding bit expands to "20".

-
+1 for the first answer, how about the second question? (I will revisit this later and give a green check mark) –  xport Jul 22 '11 at 11:23
As usual, the calculations are done in terms of scaled points, so the difference is less than the wavelength of visible light and will influence pixels only in very unfortunate circumstances. In this particular case I get \paperwidth=29834872sp both with the \dimexpr method and with the traditional \advance method. –  egreg Jul 22 '11 at 11:33
If I use 4.5 instead of 4, the compilation fails. How to fix it? –  xport Jul 22 '11 at 12:38
Inside \numexpr only integers are allowed. Just use integers and a sufficiently small base dimension. –  egreg Jul 22 '11 at 12:50

Because egreg's suggestion using \numexpr cannot be used for non-integer values, I have to use my own as follows. It works but it seems to be too complicated and needs further simplification.

\documentclass{minimal}
\usepackage{pstricks}
\psset
{
xunit=1cm,
yunit=1cm
}

\newcommand\Left{-1}
\newcommand\Right{1}
\newcommand\Bottom{-1}
\newcommand\Top{1}