3

The following example return value 0 at division of \x by \y

\documentclass{article}
\usepackage[spanish]{babel}
\usepackage[utf8x]{inputenc}
\newcommand{\x}{31}
\newcommand{\y}{45}
\begin{document}
    I need the decimal results of following division:
    \the\numexpr\x/\y\relax.
\end{document}

How can I fix that?

  • 1
    \numexpr returns an integer number and rounds. Therefore, the result of 31/45 is 1, not 0. – Heiko Oberdiek Apr 5 '18 at 17:12
  • See the fp or xfp packages for real-number mathematics, for example. I believe pgf based packages also provide such facilities. – Steven B. Segletes Apr 5 '18 at 17:13
4
\documentclass{article}
\usepackage{xfp}

\begin{document}

\fpeval{31/45}

\fpeval{round(31/45,5)}

\end{document}

The toolbox of xfp is quite extensive.

enter image description here

If you need the comma instead of the decimal period, use siunitx:

\documentclass{article}
\usepackage{xfp,siunitx}

\sisetup{output-decimal-marker={,}}

\begin{document}

\num{\fpeval{31/45}}

\num{\fpeval{round(31/45,5)}}

\end{document}

enter image description here

4
\documentclass{article}
%\usepackage[spanish]{babel}
\usepackage[utf8x]{inputenc}
\newcommand{\x}{31}
\newcommand{\y}{45}
\makeatletter
\newcommand\MyDivide[2]{\strip@pt\dimexpr\numexpr#1*65536/#2 sp\relax}

\makeatother
\begin{document}
    I need the decimal results of following division:
    \MyDivide{\x}{\y}
\end{document}

enter image description here

oops sorry I had interchanged #1 and #2 first. So:

enter image description here

  • Thanks. And if I wanna make another operation (+, - or *) with those number. How could be? – Andrey Vinajera Apr 5 '18 at 17:49
  • @AndreyVinajera perhaps start with answers there or there – user4686 Apr 5 '18 at 18:30
  • @AnderyVinajera the canonical answer nowadays is probably to use the xfp package from LaTeX3 team, as in egreg's answer. It does computations with 16 decimal digits of precision and provides exp, log, sin, cos... My own xint computes in arbitrary precision and with fractions exactly, and has a floating-point mode which like xfp is set at 16 digits per default, but you can set it to 24 digits... caveat is that currently (April 2018) only sqrt is available as math function. xintexpr's parser has very extended syntax allowing things such as add(x(x+3)(x+5), x=10..20). – user4686 Apr 6 '18 at 6:53
2

Here's a LuaLaTeX-based solution.

enter image description here

% !TeX program = lualatex

\RequirePackage{luacode}
\begin{luacode}
function mycalc (prec,n)
   tex.sprint ( string.format ( "%."..prec.."f" , n) )
end
\end{luacode}

\documentclass{article}
\usepackage[spanish]{babel}
%% Show 5 digits of prec. by default:
\newcommand\mycalc[2][5]{\directlua{mycalc(#1,#2)}}

\begin{document}
\newcommand{\x}{31} 
\newcommand{\y}{45}

I need the decimal results of following division: \mycalc{\x/\y}.
\end{document}
  • @AndreyVinajera - Observe that the method used in this answer works for all arithmetic operations, not just division. Assuming \a, \b, and \c are suitably defined, you could write \mycalc{\a^{\b}*\c}, etc. – Mico Apr 6 '18 at 7:09
2

The xlop package 0.26 allows you to do French divisions like this:

\documentclass{article}
\usepackage{xlop}

\begin{document}
“french”  division:

\opdiv{31}{45}
\end{document}

Output:

enter image description here

It can stop as soon as the period found

\documentclass{article}
\usepackage{xlop}

\begin{document}
With inline mode:
\opdiv[maxdivstep=5,style=text]{31}{45}

Stop with period detection:
\opdiv[period,style=text]{31}{45}
\end{document}

Output:

enter image description here

Star macros allow calculations to be performed without displaying the result. In the case of division, the \opdiv*{31}{45}{q}{r} macro stores the quotient (q) and the rest (r) in two parameters.

Three macros allow a control about precision. They allow to approximate a number giving the rank of the approximation. There are \opfloor (default approximate value), \opceil (excess approximate value), and \opround (rounding)

\documentclass{article}
\usepackage{xlop}

\begin{document}

Approximation:
\opdiv*[maxdivstep=15]{31}{45}{q}{r}
 \opfloor{q}{7}{a}
 \opceil{q}{10}{b}
 \opround{q}{13}{c}

\opprint{a}

\opprint{b}

\opprint{c}
\end{document}

Output:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.