# Org table: Calculate units

Are Org tables able to calcultate units?

Say I have a table

``````| test     | Voltage  |  DC | Current    | Power       |
|----------+----------+-----+------------+-------------|
| DC-motor | 12 V     | 10% | 15.6mA     | 187.2 V mA  |
|          |          | 20% | 23.6mA     | 283.2 V mA  |
|          |          | 30% | 34.3mA     | 411.6 V mA  |
|          |          | 40% | 43.3mA     | 519.6 V mA  |
|          |          | 50% | 54.6mA     | 655.2 V mA  |
|          |          | 60% | 66.6mA     | 799.2 V mA  |
|          |          | 70% | 83.1mA     | 997.2 V mA  |
|          |          | 80% | 95.3mA     | 1143.6 V mA |
|          |          | 90% | 110.1mA    | 1321.2 V mA |
| solenoid | 12 V     |  DC | 1.061 A    | 12.732 V A  |
|----------+----------+-----+------------+-------------|
#+TBLFM: \$5=12V*\$4
``````

And I want to calculate the `Power` column in watts. I know there's a calc-function called `(calc-simplify-units)` but how would I apply such a function to the column function?

``````| test     | Voltage  |  DC | Current    | Power     |
|----------+----------+-----+------------+-----------|
| DC-motor | 12 V     | 10% | 15.6mA     | 187.2 mW  |
|          |          | 20% | 23.6mA     | 283.2 mW  |
|          |          | 30% | 34.3mA     | 411.6 mW  |
|          |          | 40% | 43.3mA     | 519.6 mW  |
|          |          | 50% | 54.6mA     | 655.2 mW  |
|          |          | 60% | 66.6mA     | 799.2 mW  |
|          |          | 70% | 83.1mA     | 997.2 mW  |
|          |          | 80% | 95.3mA     | 1143.6 mW |
|          |          | 90% | 110.1mA    | 1321.2 mW |
| solenoid | 12 V     |  DC | 1.061 A    | 12.732 W  |
|----------+----------+-----+------------+-----------|
``````
• Org formulas are essentially Calc code (unless you use `;L` to mean it's ELisp code). I'm sure that what you are asking for is possible, but I would need to research it further to give you the answer. – wvxvw Nov 26 '18 at 13:48

This question is almost a duplicate to the question about the usage of a calc table for converting units.

The only difference is that you do not only want to convert values with units in a column but you want the result of an operation with standard units.

You can write the operation into the first inner `calc-eval` of the linked answer.

``````| test     | Voltage |  DC | Current | Power    |
|----------+---------+-----+---------+----------|
| DC-motor | 12 V    | 10% | 15.6 mA | 0.1872 W |
|          |         | 20% | 23.6 mA | 0.2832 W |
|          |         | 30% | 34.3 mA | 0.4116 W |
|          |         | 40% | 43.3mA  | 0.5196 W |
|          |         | 50% | 54.6mA  | 0.6552 W |
|          |         | 60% | 66.6mA  | 0.7992 W |
|          |         | 70% | 83.1mA  | 0.9972 W |
|          |         | 80% | 95.3mA  | 1.1436 W |
|          |         | 90% | 110.1mA | 1.3212 W |
| solenoid | 12 V    |  DC | 1.061 A | 12.732 W |
|----------+---------+-----+---------+----------|
#+TBLFM: \$5='(calc-eval (math-convert-units (calc-eval (concat "12 V * " \$4) 'raw) (calc-eval "W" 'raw)))
``````

Note that this works with the setting M-x `calc-normal-language`.

There is a slightly simpler but a bit hacky solution.

You can use `usimplify` with a quasi-zero as first operand that has the right compatible unit.

Note that `0 W` as first operand does not work. The 0 is optimized out.

``````| test     | Voltage |  DC | Current | Power    |
|----------+---------+-----+---------+----------|
| DC-motor | 12 V    | 10% | 15.6 mA | 0.1872 W |
|          |         | 20% | 23.6 mA | 0.2832 W |
|          |         | 30% | 34.3 mA | 0.4116 W |
|          |         | 40% | 43.3mA  | 0.5196 W |
|          |         | 50% | 54.6mA  | 0.6552 W |
|          |         | 60% | 66.6mA  | 0.7992 W |
|          |         | 70% | 83.1mA  | 0.9972 W |
|          |         | 80% | 95.3mA  | 1.1436 W |
|          |         | 90% | 110.1mA | 1.3212 W |
| solenoid | 12 V    |  DC | 1.061 A | 12.732 W |
|----------+---------+-----+---------+----------|
#+TBLFM: \$5=usimplify(1e-50 W + 12 V * \$4)
``````

Furthermore, it is really easy to write a calc function `uconvert` that converts units.

Just copy the following code into your init file.

``````(defmath uconvert (v u)
"Convert value V to compatible unit U."
(math-convert-units v u))
``````

After restarting Emacs you can use `uconvert` as demonstrated in the following org table:

``````| test     | Voltage |  DC | Current | Power    |
|----------+---------+-----+---------+----------|
| DC-motor | 12 V    | 10% | 15.6 mA | 0.1872 W |
|          |         | 20% | 23.6 mA | 0.2832 W |
|          |         | 30% | 34.3 mA | 0.4116 W |
|          |         | 40% | 43.3mA  | 0.5196 W |
|          |         | 50% | 54.6mA  | 0.6552 W |
|          |         | 60% | 66.6mA  | 0.7992 W |
|          |         | 70% | 83.1mA  | 0.9972 W |
|          |         | 80% | 95.3mA  | 1.1436 W |
|          |         | 90% | 110.1mA | 1.3212 W |
| solenoid | 12 V    |  DC | 1.061 A | 12.732 W |
|----------+---------+-----+---------+----------|
#+TBLFM: \$5=uconvert(12V*\$4,W)
``````
• I didn't know about `defmath`. That's a beautiful solution, Thanks! – Frede Nov 26 '18 at 17:35