How to divide in LaTeX without it rounding or truncating?

\numexpr works only on integers. If you want decimals you have to use \dimexpr (note that you have to add the pt unit to the numerator):

\documentclass{article}
\makeatletter
\def\dimeval#1{\strip@pt\dimexpr#1\relax}
\makeatother
\begin{document}
\newcommand\qoffset{14}
\newcommand\smallx{8}
\newcommand\smally{4}
\dimeval{\qoffset pt / (\qoffset - \smallx)}

\dimeval{\smally pt * (\qoffset / (\qoffset - \smallx))}
\end{document}

which results:

enter image description here

which is correct for the first one, but not as much for the second (it should be 9.33333).

The second one is wrong (depending on your point of view) because \dimexpr only does integer division and multiplication, so (replacing the values) 4pt * ( 14 / ( 14 - 8 ) ) evaluates to 4pt * ( 14 / 6 ) then to 4pt * 2.33333, and 2.33333 truncates to 2 and the result is 8. You can get a bit closer with

\dimeval{\smally pt * \dimeval{\qoffset pt / (\qoffset - \smallx)}}

but a bit wrong is still wrong.


If you want real floating point arithmetics (without having to worry about units), use \fpeval from the xfp package:

\documentclass{article}
\usepackage{xfp}
\begin{document}
\newcommand\qoffset{14}
\newcommand\smallx{8}
\newcommand\smally{4}
\fpeval{\qoffset / (\qoffset - \smallx)}

\fpeval{\smally * (\qoffset / (\qoffset - \smallx))}
\end{document}

which results

enter image description here


As Mico says, you might want rounding. If want for typesetting numbers the best option is the siunitx package:

\documentclass{article}
\usepackage{xfp}
\usepackage{siunitx}
\begin{document}
\num[round-mode=places, round-precision=5]%
  {\fpeval{4 * (14 / (14 - 8))}}
\end{document}

enter image description here

but if you want to round for further calculations, then you can use the round function directly (the syntax is round(<num>,<digits>)):

\documentclass{article}
\usepackage{xfp}
\begin{document}
\fpeval{round(  4 * (14 / (14 - 8))  ,5)}
\end{document}

enter image description here


Would you consider using LuaTeX? Then you could evaluate even more complicated expressions:

% gobble ".0" for integers and rounding function
\begingroup
\catcode`\%=12
\directlua{
   function math.round_int ( x )
     return x>=0 and math.floor(x+0.5) or math.ceil(x-0.5)
   end
   function math.round ( x , n )
     return math.round_int ( x*10^n ) / 10^n 
   end
   function gobblezero(x)
     local y = math.round ( x , 8 )
     if y == math.floor(y) then
       return string.format ( "%.0f", y )
     else
       return math.round(y, 7)
     end
   end}
\endgroup

\def\fpeval#1{\directlua{tex.print(gobblezero(#1))}}

$$ \root 6 \of {64} = \fpeval{64^(1/6)} $$

With rounding:
%
$$ 1/3 = \fpeval{math.round(1/3, 1)}... $$

\bye

enter image description here