6

I am trying to sum up entries in a table and thought using the fp package was the way to go. The MWE below,

\AddToTotal{18,000.00}
\AddToTotal{-17,000.00}
\AddToTotal{-8,000.00}

yields:

enter image description here

as expected. However, splitting the -8,000 into two steps breaks things:

\AddToTotal{18,000.00}
\AddToTotal{-17,000.00}
\AddToTotal{-7,000.00}
\AddToTotal{-1,000.00}% <--- Why this break things?

which should yield the same result instead produces the error:

FP error: UPN stack is empty!. \FP@errmessage #1->\errmessage {FP error: #1!}

l.25 \AddToTotal{-1,000.00} % <--- Why this break things?

Should I be using a different package for this? I only need 2 digits of precision, so perhaps there are better options.

Code:

\documentclass{article}
\usepackage{xstring}
\usepackage{siunitx}

\usepackage{fp}

\newcommand*{\Total}{0.00}% Initial Value
\newcommand*{\AddToTotal}[1]{%
    \StrDel{#1}{,}[\NewAmount]%
    \FPeval\TempTotal{\Total+\NewAmount}%
    \xdef\Total{\TempTotal}%
    %% --------
    \par
    After adding $\NewAmount$, total is \num[round-mode=places,round-precision=2]{\Total}.%
}%

\begin{document}
    \AddToTotal{18,000.00}
    \AddToTotal{-17,000.00}
    \AddToTotal{-7,000.00}
    \AddToTotal{-1,000.00}% <--- Why this break things?
\end{document}
Peter Grill
  • 223,288

2 Answers2

5

The problem can be easily fixed by adding extra parentheses in your \AddToTotal command:

\FPeval\TempTotal{(\Total)+\NewAmount}%  <-- extra parentheses around \Total

Without the parentheses, a negative \Total is expanded as is, giving something like -6000+-1000 where the first expression is interpreted as a subtraction rather than a negative number. fp then produces an empty stack error because the second operand for that subtraction is missing on the internal stack.

To fix that you have to give it a hint that the negative number is a single atom by putting a pair of parentheses around it. You now correctly get:

enter image description here

That alternative version also seems to work:

\FPeval\TempTotal{0+\Total+\NewAmount}

Not sure why the parsing works differently at the start of the expression than in the middle.

siracusa
  • 13,411
1

Why not using expl3 directly? You load it anyway, because of siunitx.

\documentclass{article}
\usepackage{siunitx} % also loads xparse and expl3

\sisetup{
  round-mode=places,
  round-precision=2,
  group-separator={,},
  group-four-digits,
}


\ExplSyntaxOn

\fp_new:N \g_grill_total_fp % initialized to 0
\tl_new:N \l__grill_total_temp_tl

\NewDocumentCommand{\AddToTotal}{m}
 {
  \tl_set:Nn \l__grill_total_temp_tl { #1 }
  \tl_remove_all:Nn \l__grill_total_temp_tl { , }
  \fp_gadd:Nn \g_grill_total_fp { \l__grill_total_temp_tl }
  After~adding~\num{\tl_use:N \l__grill_total_temp_tl},~
  total~is~\num{\fp_use:N \g_grill_total_fp}.
  \par
 }
\ExplSyntaxOff

\begin{document}

\AddToTotal{18,000.00}
\AddToTotal{-17,000.00}
\AddToTotal{-7,000.00}
\AddToTotal{-1,000.00}

\end{document}

enter image description here

egreg
  • 1,121,712