all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Christian Schlauer <cs-muelleimer-rubbish.bin@arcor.de>
To: emacs-devel@gnu.org
Subject: Calc: `*' binds more strongly than `/'
Date: Sat, 14 Apr 2007 13:57:18 +0200	[thread overview]
Message-ID: <87y7kvxj6p.fsf@arcor.de> (raw)

Hello!

This is something I just feel I have to mention before the release, as
it is very confusing, IMHO: `*' binds more strongly than `/' in M-x
calc.

The following is a table which allows you to do calculations:

| Value | Percentage |
|-------+------------|
|    17 |            |
|    56 |            |
|    42 |            |
#+TBLFM: $2=$1/65*100

The line below the table contains formulas for the table columns, I
think you can figure out what it means -- the steps to reproduce the
confusing behaviour are:

1. M-x orgtbl-mode RET

,----[ C-h f orgtbl-mode RET ]
| The `org-mode' table editor as a minor mode for use in other modes.
`----

2. Put point in an empty table cell below `Percentage' and press `C-u
   C-c C-c', which will recompute the table:

,----[ C-h k C-c C-c ]
| C-c C-c runs the command orgtbl-ctrl-c-ctrl-c
| With prefix arg, also recompute table.
`----

What you get is this:

| Value | Percentage |
|-------+------------|
|    17 |  2.6154e-3 |
|    56 |  8.6154e-3 |
|    42 |  6.4615e-3 |
#+TBLFM: $2=$1/65*100

What I expected to get is:

| Value | Percentage |
|-------+------------|
|    17 |     26.154 |
|    56 |     86.154 |
|    42 |     64.615 |
#+TBLFM: $2=($1/65)*100

Note the    ^     ^ extra parentheses.

But in order to get the expected result, I have to put the `/'
operation in parenthesis as Calc does not calculate `from left to
right' in this case.

So when one uses orgtbl-mode (or org-mode) for table calculations
(which in turn uses Calc for the calculations), one has to know that
`*' binds more strongly than `/' -- well, I didn't know and asked
Carsten Dominik, the author of org-mode. He explained that this is a
feature of Calc. He also put a sentence in the org-mode manual:

,----[ (info "(org)Formula syntax for Calc") ]
| Note that `calc' has the slightly non-standard convention that `/'
| has lower precedence than `*', so that `a/b*c' is interpreted as
| `a/(b*c)'.
`----

Later, I also found it in the Calc manual:

,----[ (info "(calc)Algebraic Tutorial") ]
|    Algebraic formulas use the operators `+', `-', `*', `/', and `^'.
| You can use parentheses to make the order of evaluation clear.  In the
| absence of parentheses, `^' is evaluated first, then `*', then `/',
| then finally `+' and `-'.  For example, the expression
| 
| [...]
| 
|    Calc's order of evaluation is the same as for most computer
| languages, except that `*' binds more strongly than `/', as the above
| example shows.  As in normal mathematical notation, the `*' symbol can
| often be omitted:  `2 a' is the same as `2*a'.
| 
|    Operators at the same level are evaluated from left to right, except
| that `^' is evaluated from right to left.  Thus, `2-3-4' is equivalent
| to `(2-3)-4' or -5, whereas `2^3^4' is equivalent to `2^(3^4)' (a very
| large integer; try it!).
`----

I believe (but I do not know) that Calc's behaviour is (too) far from
what `ordinary' people (like me ;-) would expect: I ran into this
feature in my first table and wondered what was going on. I thought
that Org-mode calculates `wrong'.

I don't know if it is possible to change the current behaviour, but I
think it would be better. My arguments:

- I learned in school that `*' and `/' bind more than `+' and `-'. I
  don't remember any `finer graduation' between the operators. Even
  the Calc manual says that this feature is an exception. Carsten
  Dominik calls it a `slightly non-standard convention' in the
  Org-mode manual.

- MATLAB works as I expect it, and I'd guess that GNU Octave does so
  too, but I don't have it installed. Gnumeric 1.7.0 works as I expect
  it, too.

- With the inclusion of Calc (and Org-mode) in Emacs 22, Org-mode's
  spreadsheet capabilities and Calc will be used by a wider audience.
  But they will easily run into this feature and might get wrong
  results (without discovering it). Org-mode mentions the feature
  directly in the beginning of the section of the manual that deals
  with spreadsheet calculations, but in Calc I had to dig into the
  manual to find out about this, especially as I was completely
  unfamiliar with Calc. What I would expect, however, is a kind of a
  warning on the first page of the manual that Calc does not work as
  `one' might expect (depending on `one').

The Calc manual mentions `Operators at the same level are evaluated
from left to right', so Calc could treat `*' and `/' as equal, it
seems. The current behaviour was surely chosen for a reason, but I
really wonder if it is (no offence, Calc seems to be great otherwise)
suitable for a `wider' audience.

Regards,

Christian Schlauer

             reply	other threads:[~2007-04-14 11:57 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-04-14 11:57 Christian Schlauer [this message]
2007-04-14 15:09 ` Calc: `*' binds more strongly than `/' Jay Belanger
2007-04-15 19:21   ` Chong Yidong
2007-04-15 20:05     ` Stefan Monnier
2007-04-17 12:47       ` Randal L. Schwartz
2007-04-17 18:03         ` Stefan Monnier
2007-04-18  8:44           ` Kim F. Storm
2007-04-18 19:27             ` Stefan Monnier
2007-04-18 20:02               ` Davis Herring
2007-04-18 20:16                 ` David Kastrup
2007-04-18 21:43                   ` Davis Herring
2007-04-19 16:10                 ` Stefan Monnier
2007-04-20 11:13                   ` Randal L. Schwartz
2007-04-20 13:40                     ` Stefan Monnier
2007-04-20 14:07                       ` Jay Belanger
2007-04-22  1:31                         ` Miles Bader
2007-04-22 10:19                           ` Christian Schlauer
2007-04-22 10:23                         ` Christian Schlauer
2007-04-22 19:47                           ` Jay Belanger
2007-04-23 10:46                           ` Jason Rumney
2007-04-23 13:03                             ` Kim F. Storm
2007-04-23 15:54                               ` Jay Belanger
2007-04-25 19:56                                 ` Christian Schlauer
2007-04-25 20:54                                   ` Jay Belanger
2007-04-26  3:35                                     ` Stefan Monnier
2007-04-26  4:06                                       ` Jay Belanger
2007-04-26 14:11                                         ` Stefan Monnier
2007-04-26 21:07                                           ` Christian Schlauer
2007-04-26 15:07                                         ` Davis Herring
2007-04-26 16:01                                           ` Stefan Monnier
2007-04-26 18:32                                             ` Luc Teirlinck
2007-04-26 21:29                                               ` Christian Schlauer
2007-04-26 23:17                                                 ` Luc Teirlinck
2007-04-26 21:07                                     ` Christian Schlauer
2007-04-27  1:27                                       ` Jay Belanger
2007-04-26 17:47                                   ` Davis Herring
2007-06-20 15:23                         ` Jay Belanger
2007-06-20 16:02                           ` Kim F. Storm
2007-06-20 17:13                             ` Jay Belanger
2007-06-20 17:29                             ` Luc Teirlinck
2007-06-20 17:54                               ` Jay Belanger
2007-06-20 19:14                                 ` Kim F. Storm
2007-06-20 19:28                                 ` Luc Teirlinck
2007-06-20 20:18                                   ` Jay Belanger
2007-04-17 18:54         ` Jay Belanger
2007-04-18 21:55           ` Christian Schlauer
2007-04-19  2:58             ` Daniel Brockman
2007-04-22 10:27               ` Christian Schlauer
2007-04-26 16:00                 ` Daniel Brockman
2007-04-19 15:16           ` Drew Adams
2007-04-19 17:05             ` David Kastrup
2007-04-19 17:18               ` Drew Adams
2007-04-19 17:33                 ` David Kastrup
2007-04-16 15:38     ` Richard Stallman
2007-04-17 12:47 ` Randal L. Schwartz
2007-04-18 21:29   ` Christian Schlauer

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

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

  git send-email \
    --in-reply-to=87y7kvxj6p.fsf@arcor.de \
    --to=cs-muelleimer-rubbish.bin@arcor.de \
    --cc=cs-usenet@arcor.de \
    --cc=emacs-devel@gnu.org \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.