unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Calc: `*' binds more strongly than `/'
@ 2007-04-14 11:57 Christian Schlauer
  2007-04-14 15:09 ` Jay Belanger
  2007-04-17 12:47 ` Randal L. Schwartz
  0 siblings, 2 replies; 56+ messages in thread
From: Christian Schlauer @ 2007-04-14 11:57 UTC (permalink / raw)
  To: emacs-devel

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

^ permalink raw reply	[flat|nested] 56+ messages in thread

end of thread, other threads:[~2007-06-20 20:18 UTC | newest]

Thread overview: 56+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-14 11:57 Calc: `*' binds more strongly than `/' Christian Schlauer
2007-04-14 15:09 ` 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

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.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).