[Tex/LaTex] Floating point calculations in LaTeX

calculations

I am looking for a method to do minimal floating point / integer calculations in LaTeX, not for the purpose of package writing, but for the production of actual text.

Here is an example to illustrate. Given the following input text

 The experiment included running a battery of \bind{T}{21} tests
 on \bind{S}{13} subjects, for a total of \bind{V}{T*S} expected values.
 However, since \bind{F}{37} values were defective, our successful 
 measurement rate per subject was \use{100*round((V-F)/T,2)}\%

it would be nice if was converted to:

 The experiment included running a battery of $21$ tests
 on $13$ subjects, for a total of $273$ expected values.
 However, since $37$ values were defective, our successful 
 measurement rate per subject was $86$\%.

Where \bind{C}{expression} defines a new constant C whose value is expression, and returns C, while \use{C} simply returns the value of C. Any tips?

I am aware of the spreadtab package and this question: this question, but I am looking for something more.

Best Answer

If you don't mind using luatex, then you could just let lua do the calculations. I use ConTeXt, but I believe that the luacode package in LaTeX provides similar functionality.

\startusercode
  round = global.math.round 
\stopusercode

\def\bind#1#2%
    {\usercode{#1 = #2}\use{#1}}

\def\use#1%
    {\usercode{global.context(#1)}}

\starttext
 The experiment included running a battery of \bind{T}{21.0} tests
 on \bind{S}{13} subjects, for a total of \bind{V}{T*S} expected values.
 However, since \bind{F}{37} values were defective, our successful 
 measurement rate per subject was \use{100*round((V-F)/T,2)}\%
\stoptext

I am using usercode rather luacode so that my definitions (T=21 etc) do not pollute the global namespace.

EDIT: If you want the result to be in math mode, change global.context(...) to global.context.math(...) in the definition of \use