emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Derek Feichtinger <derek.feichtinger@psi.ch>
To: Eric S Fraga <e.fraga@ucl.ac.uk>
Cc: emacs-orgmode@gnu.org, Daniele Nicolodi <daniele@grinta.net>,
	Christian Moe <mail@christianmoe.com>
Subject: Re: org-tables with monetary amounts
Date: Tue, 13 Oct 2020 09:10:30 +0200	[thread overview]
Message-ID: <87h7qy38k9.fsf@psi.ch> (raw)
In-Reply-To: <87sgajrd93.fsf@ucl.ac.uk>

Hi,

On Mon, Oct 12 2020, Eric S Fraga <e.fraga@ucl.ac.uk> wrote:

> On Monday, 12 Oct 2020 at 10:22, Christian Moe wrote:
>> I think I was thinking about adding mode flags for unit computations,
>> like in the imagined example above (`$3=$2+$1;u'), similar to what has
>> been done for duration computations. 
>
> This would be very useful, actually.  I use embedded calc all the time
> with units; being able to use some of the same expressions in a table
> would be highly welcome.

The functions that one can use in calc formulas can be extended using
the 'defmath' lisp command. I paste here from my notes (not about
monetary units, but easily adapted):

+++++++++++++++++++++++++++++++
Defining a new calc function for unit conversion with defmath
#+BEGIN_SRC emacs-lisp
(defmath uconv (expr target-units &optional pure)
  (math-convert-units expr target-units pure))
#+END_SRC

#+RESULTS:
: calcFunc-uconv

| km     | ft           |
|--------+--------------|
| 2.5 km | 8202.0997 ft |
#+TBLFM: $2=uconv($1, ft)

Using the units from the table header (if the 3rd arg is given to
uconv, the output is stripped of the unit):

|  km |        ft |
|-----+-----------|
| 2.5 | 8202.0997 |
#+TBLFM: $2 = uconv($1 * @<$1, @<$2, t)

The standard calc function usimplify also works for this use
case:

|  km |        ft |
|-----+-----------|
| 2.5 | 8202.0997 |
#+TBLFM: $2 = usimplify($1 * @<$1 / @<$2)

A lisp equivalent of the above
#+begin_src elisp
(calc-eval "usimplify(2.5 km / ft)")
#+end_src

#+RESULTS:
: 8202.09973753

Let's define a function that converts to base units
#+begin_src elisp
  (defmath ustd (expr) (math-simplify-units (math-to-standard-units expr nil)))
#+end_src

#+RESULTS:
: calcFunc-ustd

| distance | time   | speed       | std unit speed   | speed in ft/s    |
|----------+--------+-------------+------------------+------------------|
| 3 km     | 2.5 hr | 1.2 km / hr | 0.33333333 m / s | 1.0936133 ft / s |
#+TBLFM: @2$3=$1/$2::@2$4=ustd($3)::@2$5=uconv($-1, ft/s)
++++++++++++++++++++++++++++++++++++


-- 
Paul Scherrer Institut
Dr. Derek Feichtinger                   Phone:   +41 56 310 47 33
Group Head HPC and Emerging Technologies  Email: derek.feichtinger@psi.ch
Building/Room No. OHSA/D17
Forschungsstrasse 111
CH-5232 Villigen PSI 


  reply	other threads:[~2020-10-13  7:18 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-22 14:57 org-tables with monetary amounts Daniele Nicolodi
2020-09-22 23:25 ` Nicholas Savage
2020-09-23  9:37 ` Russell Adams
2020-09-23 16:55 ` Eric S Fraga
2020-09-25 11:20   ` Daniele Nicolodi
2020-09-25 11:57     ` Alan Schmitt
2020-09-23 21:26 ` Nick Dokos
2020-09-24  9:17 ` Christian Moe
2020-09-24 11:47   ` Eric S Fraga
2020-10-09 16:14   ` Daniele Nicolodi
2020-10-12  8:22     ` Christian Moe
2020-10-12  9:43       ` Eric S Fraga
2020-10-13  7:10         ` Derek Feichtinger [this message]
2020-10-14  7:38           ` Christian Moe
2020-10-15 20:02       ` Daniele Nicolodi
2020-09-25  9:25 ` Neil Jerram
2020-09-25 11:35   ` Daniele Nicolodi
2020-09-26 18:38     ` Neil Jerram

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87h7qy38k9.fsf@psi.ch \
    --to=derek.feichtinger@psi.ch \
    --cc=daniele@grinta.net \
    --cc=e.fraga@ucl.ac.uk \
    --cc=emacs-orgmode@gnu.org \
    --cc=mail@christianmoe.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).