7

Given this org-mode document:

| 4 | ? |
#+TBLFM: $2=$1/2*2

Evaluating the formulas produces (in the second cell) the surprising result 1, instead of (4/2*2=) 4.

Apparently, multiplication has a higher precedence in org-mode formulas than division, which can (occasionally silently) produce unexpected results.

Is there a way to make org-mode formulas use common arithmetic operator precedence, as in most in-fix notations?

3 Answers 3

8

You can just use another language for calc-eval in orgmode by the following setting in your init file.

(setq org-calc-default-modes (append '(calc-language c) org-calc-default-modes))

You can also use M-x customize-option RET org-calc-default-modes. Insert an entry with key calc-language and value c.

The language c is just an example here. There are many more languages. You can even define your own language. But for that you need some skills...

See file calc-lang.el or the doc of Language Modes.

1
  • @VladimirPanteleev Thanks for the error correction. I didn't notice since that bit was at the line-break.
    – Tobias
    Jul 30, 2018 at 15:13
1

org-mode uses calc to compute formulas, and calc uses non-standard operator precedence. From the org manual, (org) Formula syntax for Calc:

A formula can be any algebraic expression understood by the Emacs ‘Calc’ package. Note that ‘calc’ has the non-standard convention that ‘/’ has lower precedence than ‘*’, so that ‘a/b*c’ is interpreted as ‘a/(b*c)’.

Which means you'll have to use brackets to impose standard operator precedence, i.e.,

| 4 | ? |
#+TBLFM: $2=($1/2)*2
4
  • Thanks for the explanation, but that doesn't answer the stated question as stated. The question is how to make org-mode use common arithmetic precedence. Simply trying to remember to use the correct syntax to avoid unexpected results any time one uses org-mode tables is not a workable solution. Jul 30, 2018 at 14:46
  • The answer to your question: "Is there a way to make org-mode use standard operator precedence?" is "no". The other built-in alternative is to use lisp formulas instead of calc formulas, but that's even further from standard operator precedence.
    – Tyler
    Jul 30, 2018 at 14:56
  • Sorry, but I'm not convinced things are as you say. Calc is implemented in Emacs Lisp, right? So, at the very least, it should be possible to patch its code through advice or such. Its manual also speaks of support for multiple languages with different precedence rules, so perhaps this could be done as a file-local variable? Jul 30, 2018 at 14:59
  • I stand corrected, see @Tobias answer
    – Tyler
    Jul 30, 2018 at 15:11
0

This behavior is controlled by the option calc-multiplication-has-precedence. Setting it to nil makes multiplication have the same precedence as division in the default mode.

Quoting its documentation:

If non-nil, multiplication has precedence over division in normal mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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