Emacs calculator

Emacs has quite a sophisticated scientific calculator. Like many other things in Emacs, it is both powerful and idiosyncratic.

The calc module ships with Emacs as of version 22. The calc manual has full documentation, but it takes some work to understand. This post may make it easier to read the manual.

The manual says you can start calc with the command M-#. That did not work for me, but the command M-x calc did.

Calc has two modes: RPN (reverse Polish notation) and algebraic. RPN is the default mode and uses very terse commands, often one or two letters. Most people will find the algebraic mode syntax more familiar. Commands in algebraic mode begin with a quote and use longer, more descriptive names.

For example, suppose you want to find the cosine of 5. You can do this in RPN mode by typing 5 and pressing return, then typing shift-C. To do the same calculation in algebraic mode, type 'cos(5) and return.

I’ll step through to gamma function documentation as an example of how to interpret the manual. It says

The f g (calc-gamma) [gamma] command computes the Euler gamma function. …

This means that in RPN mode, you enter the argument of the gamma function, say 5, and then type fg. (Not f g as you might expect.) You could also enter 5 and then type M-x calc-gamma. In algebraic mode, the syntax would be 'gamma(5). So the pattern in the manual is

RPN keystrokes (lisp function) [algebraic syntax].

The calc module has an amazing range of functionality — symbolic calculation, matrix operations, graphics, etc. — though I don’t imagine I’d use much of it. In my mind, the benefit of calc is being able to do a quick calculation without leaving Emacs. Although one could do sophisticated calculations in calc, I doubt I would for two reasons. First, if I have to look up how to do something in calc, I lose the benefit of not interrupting my workflow. Second, I don’t want to start a computation in calc and then discover I need something that isn’t there and then have to switch to another tool such as SciPy, Mathematica, or R.

Related posts

4 thoughts on “Emacs calculator

  1. I’ve never been able to get comfortable with Emacs’ calc mode. I just have a keyboard shortcut that opens Scheme in a terminal, and that’s little enough Emacs-interruption for me. You could probably set one up that starts Python with SciPy already imported.

  2. Emacs actually has a few calculators available, including another, simpler calculator built-in since version 21, which is fairly old at this point. It might be too simple for anyone here to bother with, but it has trig functions and some other goodies in addition to the normal four-function stuff.

    Also, you neglect to mention that the full-featured calc contains a small computer algebra system. It’s probably not as great as just hooking Emacs to Maxima (especially with imaxima) but it does work.

  3. I would also add that emacs calc has multiple precision done right. Not just bignums and rationals, but floating point arithmetic to arbitrary precision. For example, in calc mode:

    p 100

    sets the precision to 100 decimal places. Then

    2Q

    computes the square root of 2 to 100 decimal places. All the trig functions, logarithms, etc work with this arbitrary precision. Calc comes with an equation editor that IMO is more powerful than what I know from Maple or Macsyma or Maxima, it can output latex, it can do dimensional analysis (which makes it awesome for engineers), and regarding its computer algebra system: While it’s very simple, it’s based on rewrite rules, and the user can add additional ones. This means that you can teach calc whatever simplification patterns you use in your work. It’s a very powerful system. I think it’s well worth the investment of time to learn.

Comments are closed.