This chapter provides a general overview of Calc, the GNU Emacs Calculator: What it is, how to start it and how to exit from it, and what are the various ways that it can be used.
Calc is an advanced calculator and mathematical tool that runs as part of the GNU Emacs environment. Very roughly based on the HP-28/48 series of calculators, its many features include:
Calc tries to include a little something for everyone; as a result it is large and might be intimidating to the first-time user. If you plan to use Calc only as a traditional desk calculator, all you really need to read is the "Getting Started" chapter of this manual and possibly the first few sections of the tutorial. As you become more comfortable with the program you can learn its additional features. In terms of efficiency, scope and depth, Calc cannot replace a powerful tool like Mathematica. But Calc has the advantages of convenience, portability, and availability of the source code. And, of course, it's free!
This document serves as a complete description of the GNU Emacs Calculator. It works both as an introduction for novices, and as a reference for experienced users. While it helps to have some experience with GNU Emacs in order to get the most out of Calc, this manual ought to be readable even if you don't know or use Emacs regularly.
The manual is divided into three major parts: the "Getting Started" chapter you are reading now, the Calc tutorial (chapter 2), and the Calc reference manual (the remaining chapters and appendices).
If you are in a hurry to use Calc, there is a brief "demonstration" below which illustrates the major features of Calc in just a couple of pages. If you don't have time to go through the full tutorial, this will show you everything you need to know to begin. See section A Demonstration of Calc.
The tutorial chapter walks you through the various parts of Calc with lots of hands-on examples and explanations. If you are new to Calc and you have some time, try going through at least the beginning of the tutorial. The tutorial includes about 70 exercises with answers. These exercises give you some guided practice with Calc, as well as pointing out some interesting and unusual ways to use its features.
The reference section discusses Calc in complete depth. You can read the reference from start to finish if you want to learn every aspect of Calc. Or, you can look in the table of contents or the Concept Index to find the parts of the manual that discuss the things you need to know.
Every Calc keyboard command is listed in the Calc Summary, and also in the Key Index. Algebraic functions, M-x commands, and variables also have their own indices. @c{Each} In the printed manual, each paragraph that is referenced in the Key or Function Index is marked in the margin with its index entry.
You can access this manual on-line at any time within Calc by pressing the h i key sequence. Outside of the Calc window, you can press M-# i to read the manual on-line. Also, you can jump directly to the Tutorial by pressing h t or M-# t, or to the Summary by pressing h s or M-# s. Within Calc, you can also go to the part of the manual describing any Calc key, function, or variable using h k, h f, or h v, respectively. See section Help Commands.
Printed copies of this manual are also available from the Free Software Foundation.
This section describes the various notations that are used throughout the Calc manual.
In keystroke sequences, uppercase letters mean you must hold down the shift key while typing the letter. Keys pressed with Control held down are shown as C-x. Keys pressed with Meta held down are shown as M-x. Other notations are RET for the Return key, SPC for the space bar, TAB for the Tab key, DEL for the Delete key, and LFD for the Line-Feed key.
(If you don't have the LFD or TAB keys on your keyboard, the C-j and C-i keys are equivalent to them, respectively. If you don't have a Meta key, look for Alt or Extend Char. You can also press ESC or C-[ first to get the same effect, so that M-x, ESC x, and C-[ x are all equivalent.)
Sometimes the RET key is not shown when it is "obvious" that you must press RET to proceed. For example, the RET is usually omitted in key sequences like M-x calc-keypad RET.
Commands are generally shown like this: p (calc-precision
)
or M-# k (calc-keypad
). This means that the command is
normally used by pressing the p key or M-# k key sequence,
but it also has the full-name equivalent shown, e.g., M-x calc-precision.
Commands that correspond to functions in algebraic notation
are written: C (calc-cos
) [cos
]. This means
the C key is equivalent to M-x calc-cos, and that
the corresponding function in an algebraic-style formula would
be `cos(x)'.
A few commands don't have key equivalents: calc-sincos
[sincos
].
This section will show some typical small problems being solved with Calc. The focus is more on demonstration than explanation, but everything you see here will be covered more thoroughly in the Tutorial.
To begin, start Emacs if necessary (usually the command emacs
does this), and type M-# c (or ESC # c) to start the
Calculator. (See section Starting Calc, if this doesn't work for you.)
Be sure to type all the sample input exactly, especially noting the difference between lower-case and upper-case letters. Remember, RET, TAB, DEL, and SPC are the Return, Tab, Delete, and Space keys.
RPN calculation. In RPN, you type the input number(s) first, then the command to operate on the numbers.
Type 2 RET 3 + Q to compute @c{$\sqrt{2+3} = 2.2360679775$} the square root of 2+3, which is 2.2360679775.
Type P 2 ^ to compute @c{$\pi^2 = 9.86960440109$} the value of `pi' squared, 9.86960440109.
Type TAB to exchange the order of these two results.
Type - I H S to subtract these results and compute the Inverse Hyperbolic sine of the difference, 2.72996136574.
Type DEL to erase this result.
Algebraic calculation. You can also enter calculations using conventional "algebraic" notation. To enter an algebraic formula, use the apostrophe key.
Type ' sqrt(2+3) RET to compute @c{$\sqrt{2+3}$} the square root of 2+3.
Type ' pi^2 RET to enter @c{$\pi^2$} `pi' squared. To evaluate this symbolic formula as a number, type =.
Type ' arcsinh($ - $$) RET to subtract the second-most-recent result from the most-recent and compute the Inverse Hyperbolic sine.
Keypad mode. If you are using the X window system, press M-# k to get Keypad mode. (If you don't use X, skip to the next section.)
Click on the 2, ENTER, 3, +, and SQRT "buttons" using your left mouse button.
Click on PI, 2, and y^x.
Click on INV, then ENTER to swap the two results.
Click on -, INV, HYP, and SIN.
Click on <- to erase the result, then click OFF to turn the Keypad Calculator off.
Grabbing data. Type M-# x if necessary to exit Calc. Now select the following numbers as an Emacs region: "Mark" the front of the list by typing control-SPC or control-@ there, then move to the other end of the list. (Either get this list from the on-line copy of this manual, accessed by M-# i, or just type these numbers into a scratch file.) Now type M-# g to "grab" these numbers into Calc.
1.23 1.97 1.6 2 1.19 1.08
The result `[1.23, 1.97, 1.6, 2, 1.19, 1.08]' is a Calc "vector." Type V R + to compute the sum of these numbers.
Type U to Undo this command, then type V R * to compute the product of the numbers.
You can also grab data as a rectangular matrix. Place the cursor on the upper-leftmost `1' and set the mark, then move to just after the lower-right `8' and press M-# r.
Type v t to transpose this @c{$3\times2$} 3x2 matrix into a @c{$2\times3$} 2x3 matrix. Type v u to unpack the rows into two separate vectors. Now type V R + TAB V R + to compute the sums of the two original columns. (There is also a special grab-and-sum-columns command, M-# :.)
Units conversion. Units are entered algebraically. Type ' 43 mi/hr RET to enter the quantity 43 miles-per-hour. Type u c km/hr RET. Type u c m/s RET.
Date arithmetic. Type t N to get the current date and time. Type 90 + to find the date 90 days from now. Type ' <25 dec 87> RET to enter a date, then - 7 / to see how many weeks have passed since then.
Algebra. Algebraic entries can also include formulas or equations involving variables. Type ' [x + y = a, x y = 1] RET to enter a pair of equations involving three variables. (Note the leading apostrophe in this example; also, note that the space between `x y' is required.) Type a S x,y RET to solve these equations for the variables x and y.
Type d B to view the solutions in more readable notation. Type d C to view them in C language notation, and d T to view them in the notation for the TeX typesetting system. Type d N to return to normal notation.
Type 7.5, then s l a RET to let a = 7.5 in these formulas. (That's a letter l, not a numeral 1.)
Help functions. You can read about any command in the on-line
manual. Type M-# c to return to Calc after each of these
commands: h k t N to read about the t N command,
h f sqrt RET to read about the sqrt
function, and
h s to read the Calc summary.
Press DEL repeatedly to remove any leftover results from the stack. To exit from Calc, press q or M-# c again.
Calc has several user interfaces that are specialized for different kinds of tasks. As well as Calc's standard interface, there are Quick Mode, Keypad Mode, and Embedded Mode.
Calc must be installed before it can be used. See section Installation, for instructions on setting up and installing Calc. We will assume you or someone on your system has already installed Calc as described there.
On most systems, you can type M-# to start the Calculator. The notation M-# is short for Meta-#. On most keyboards this means holding down the Meta (or Alt) and Shift keys while typing 3.
Once again, if you don't have a Meta key on your keyboard you can type ESC first, then #, to accomplish the same thing. If you don't even have an ESC key, you can fake it by holding down Control or CTRL while typing a left square bracket (that's C-[ in Emacs notation).
M-# is a prefix key; when you press it, Emacs waits for you to press a second key to complete the command. In this case, you will follow M-# with a letter (upper- or lower-case, it doesn't matter for M-#) that says which Calc interface you want to use.
To get Calc's standard interface, type M-# c. To get Keypad Mode, type M-# k. Type M-# ? to get a brief list of the available options, and type a second ? to get a complete list.
To ease typing, M-# M-# (or M-# # if that's easier) also works to start Calc. It starts the same interface (either M-# c or M-# k) that you last used, selecting the M-# c interface by default. (If your installation has a special function key set up to act like M-#, hitting that function key twice is just like hitting M-# M-#.)
If M-# doesn't work for you, you can always type explicit commands like M-x calc (for the standard user interface) or M-x calc-keypad (for Keypad Mode). First type M-x (that's Meta with the letter x), then, at the prompt, type the full command (like calc-keypad) and press Return.
If you type M-x calc and Emacs still doesn't recognize the command (it will say `[No match]' when you try to press RET), then Calc has not been properly installed.
The same commands (like M-# c or M-# M-#) that start the Calculator also turn it off if it is already on.
Calc's standard interface acts like a traditional RPN calculator, operated by the normal Emacs keyboard. When you type M-# c to start the Calculator, the Emacs screen splits into two windows with the file you were editing on top and Calc on the bottom.
@advance@hsize20pt
... --**-Emacs: myfile (Fundamental)----All---------------------- --- Emacs Calculator Mode -- |Emacs Calc Mode v2.00... 2: 17.3 | 17.3 1: -5 | 3 . | 2 | 4 | * 8 | ->-5 | --%%-Calc: 12 Deg (Calculator)----All----- --%%-Emacs: *Calc Trail*
In this figure, the mode-line for `myfile' has moved up and the "Calculator" window has appeared below it. As you can see, Calc actually makes two windows side-by-side. The lefthand one is called the stack window and the righthand one is called the trail window. The stack holds the numbers involved in the calculation you are currently performing. The trail holds a complete record of all calculations you have done. In a desk calculator with a printer, the trail corresponds to the paper tape that records what you do.
In this case, the trail shows that four numbers (17.3, 3, 2, and 4) were first entered into the Calculator, then the 2 and 4 were multiplied to get 8, then the 3 and 8 were subtracted to get -5. (The `>' symbol shows that this was the most recent calculation.) The net result is the two numbers 17.3 and -5 sitting on the stack.
Most Calculator commands deal explicitly with the stack only, but there is a set of commands that allow you to search back through the trail and retrieve any previous result.
Calc commands use the digits, letters, and punctuation keys. Shifted (i.e., upper-case) letters are different from lowercase letters. Some letters are prefix keys that begin two-letter commands. For example, e means "enter exponent" and shifted E means e^x. With the d ("display modes") prefix the letter "e" takes on very different meanings: d e means "engineering notation" and d E means "eqn language mode."
There is nothing stopping you from switching out of the Calc
window and back into your editing window, say by using the Emacs
C-x o (other-window
) command. When the cursor is
inside a regular window, Emacs acts just like normal. When the
cursor is in the Calc stack or trail windows, keys are interpreted
as Calc commands.
When you quit by pressing M-# c a second time, the Calculator windows go away but the actual Stack and Trail are not gone, just hidden. When you press M-# c once again you will get the same stack and trail contents you had when you last used the Calculator.
The Calculator does not remember its state between Emacs sessions. Thus if you quit Emacs and start it again, M-# c will give you a fresh stack and trail. There is a command (m m) that lets you save your favorite mode settings between sessions, though. One of the things it saves is which user interface (standard or Keypad) you last used; otherwise, a freshly started Emacs will always treat M-# M-# the same as M-# c.
The q key is another equivalent way to turn the Calculator off.
If you type M-# b first and then M-# c, you get a
full-screen version of Calc (full-calc
) in which the stack and
trail windows are still side-by-side but are now as tall as the whole
Emacs screen. When you press q or M-# c again to quit,
the file you were editing before reappears. The M-# b key
switches back and forth between "big" full-screen mode and the
normal partial-screen mode.
Finally, M-# o (calc-other-window
) is like M-# c
except that the Calc window is not selected. The buffer you were
editing before remains selected instead. M-# o is a handy
way to switch out of Calc momentarily to edit your file; type
M-# c to switch back into Calc when you are done.
Quick Mode is a quick way to use Calc when you don't need the
full complexity of the stack and trail. To use it, type M-# q
(quick-calc
) in any regular editing buffer.
Quick Mode is very simple: It prompts you to type any formula in standard algebraic notation (like `4 - 2/3') and then displays the result at the bottom of the Emacs screen (3.33333333333 in this case). You are then back in the same editing buffer you were in before, ready to continue editing or to type M-# q again to do another quick calculation. The result of the calculation will also be in the Emacs "kill ring" so that a C-y command at this point will yank the result into your editing buffer.
Calc mode settings affect Quick Mode, too, though you will have to go into regular Calc (with M-# c) to change the mode settings.
See section "Quick Calculator" Mode, for further information.
Keypad Mode is a mouse-based interface to the Calculator. It is designed for use with the X window system. If you don't have X, you will have to operate keypad mode with your arrow keys (which is probably more trouble than it's worth). Keypad mode is currently not supported under Emacs 19.
Type M-# k to turn Keypad Mode on or off. Once again you get two new windows, this time on the righthand side of the screen instead of at the bottom. The upper window is the familiar Calc Stack; the lower window is a picture of a typical calculator keypad.
|--- Emacs Calculator Mode --- |2: 17.3 |1: -5 | . |--%%-Calc: 12 Deg (Calcul |----+-----Calc 2.00-----+----1 |FLR |CEIL|RND |TRNC|CLN2|FLT | |----+----+----+----+----+----| | LN |EXP | |ABS |IDIV|MOD | |----+----+----+----+----+----| |SIN |COS |TAN |SQRT|y^x |1/x | |----+----+----+----+----+----| | ENTER |+/- |EEX |UNDO| <- | |-----+---+-+--+--+-+---++----| | INV | 7 | 8 | 9 | / | |-----+-----+-----+-----+-----| | HYP | 4 | 5 | 6 | * | |-----+-----+-----+-----+-----| |EXEC | 1 | 2 | 3 | - | |-----+-----+-----+-----+-----| | OFF | 0 | . | PI | + | |-----+-----+-----+-----+-----+
@begingroup @ifdim@hsize=5in @advance@hsize-2.2in @else @advance@hsize-3.05in @advance@vsize.1in @fi
Keypad Mode is much easier for beginners to learn, because there is no need to memorize lots of obscure key sequences. But not all commands in regular Calc are available on the Keypad. You can always switch the cursor into the Calc stack window to use standard Calc commands if you need. Serious Calc users, though, often find they prefer the standard interface over Keypad Mode.
To operate the Calculator, just click on the "buttons" of the keypad using your left mouse button. To enter the two numbers shown here you would click 1 7 . 3 ENTER 5 +/- ENTER; to add them together you would then click + (to get 12.3 on the stack).
If you click the right mouse button, the top three rows of the keypad change to show other sets of commands, such as advanced math functions, vector operations, and operations on binary numbers.
@endgroup Because Keypad Mode doesn't use the regular keyboard, Calc leaves the cursor in your original editing buffer. You can type in this buffer in the usual way while also clicking on the Calculator keypad. One advantage of Keypad Mode is that you don't need an explicit command to switch between editing and calculating.
If you press M-# b first, you get a full-screen Keypad Mode
(full-calc-keypad
) with three windows: The keypad in the lower
left, the stack in the lower right, and the trail on top.
See section "Keypad" Mode, for further information.
If you are not in Emacs at the moment but you wish to use Calc, you must start Emacs first. If all you want is to run Calc, you can give the commands:
emacs -f full-calc
or
emacs -f full-calc-keypad
which run a full-screen Calculator (as if by M-# b M-# c) or a full-screen X-based Calculator (as if by M-# b M-# k). In standalone operation, quitting the Calculator (by pressing q or clicking on the keypad EXIT button) quits Emacs itself.
Embedded Mode is a way to use Calc directly from inside an editing buffer. Suppose you have a formula written as part of a document like this:
The derivative of ln(ln(x)) is
and you wish to have Calc compute and format the derivative for you and store this derivative in the buffer automatically. To do this with Embedded Mode, first copy the formula down to where you want the result to be:
The derivative of ln(ln(x)) is ln(ln(x))
Now, move the cursor onto this new formula and press M-# e. Calc will read the formula (using the surrounding blank lines to tell how much text to read), then push this formula (invisibly) onto the Calc stack. The cursor will stay on the formula in the editing buffer, but the buffer's mode line will change to look like the Calc mode line (with mode indicators like `12 Deg' and so on). Even though you are still in your editing buffer, the keyboard now acts like the Calc keyboard, and any new result you get is copied from the stack back into the buffer. To take the derivative, you would type a d x RET.
The derivative of ln(ln(x)) is 1 / ln(x) x
To make this look nicer, you might want to press d = to center the formula, and even d B to use "big" display mode.
The derivative of ln(ln(x)) is % [calc-mode: justify: center] % [calc-mode: language: big] 1 ------- ln(x) x
Calc has added annotations to the file to help it remember the modes that were used for this formula. They are formatted like comments in the TeX typesetting language, just in case you are using TeX. (In this example TeX is not being used, so you might want to move these comments up to the top of the file or otherwise put them out of the way.)
As an extra flourish, we can add an equation number using a righthand label: Type d } (1) RET.
% [calc-mode: justify: center] % [calc-mode: language: big] % [calc-mode: right-label: " (1)"] 1 ------- (1) ln(x) x
To leave Embedded Mode, type M-# e again. The mode line and keyboard will revert to the way they were before. (If you have actually been trying this as you read along, you'll want to press M-# 0 [with the digit zero] now to reset the modes you changed.)
The related command M-# w operates on a single word, which generally means a single number, inside text. It uses any non-numeric characters rather than blank lines to delimit the formula it reads. Here's an example of its use:
A slope of one-third corresponds to an angle of 1 degrees.
Place the cursor on the `1', then type M-# w to enable Embedded Mode on that number. Now type 3 / (to get one-third), and I T (the Inverse Tangent converts a slope into an angle), then M-# w again to exit Embedded mode.
A slope of one-third corresponds to an angle of 18.4349488229 degrees.
See section Embedded Mode, for full details.
Two more Calc-related commands are M-# g and M-# r, which "grab" data from a selected region of a buffer into the Calculator. The region is defined in the usual Emacs way, by a "mark" placed at one end of the region, and the Emacs cursor or "point" placed at the other.
The M-# g command reads the region in the usual left-to-right, top-to-bottom order. The result is packaged into a Calc vector of numbers and placed on the stack. Calc (in its standard user interface) is then started. Type v u if you want to unpack this vector into separate numbers on the stack. Also, C-u M-# g interprets the region as a single number or formula.
The M-# r command reads a rectangle, with the point and mark defining opposite corners of the rectangle. The result is a matrix of numbers on the Calculator stack.
Complementary to these is M-# y, which "yanks" the value at the top of the Calc stack back into an editing buffer. If you type M-# y while in such a buffer, the value is yanked at the current position. If you type M-# y while in the Calc buffer, Calc makes an educated guess as to which editing buffer you want to use. The Calc window does not have to be visible in order to use this command, as long as there is something on the Calc stack.
Here, for reference, is the complete list of M-# commands. The shift, control, and meta keys are ignored for the keystroke following M-#.
Commands for turning Calc on and off:
Commands for moving data into and out of the Calculator:
Commands for use with Embedded Mode:
calc-edit
) the formula at the current point.
Miscellaneous commands:
Calc was originally started as a two-week project to occupy a lull
in the author's schedule. Basically, a friend asked if I remembered
the value of @c{$2^{32}$}
2^32. I didn't offhand, but I said, "that's
easy, just call up an xcalc
." Xcalc
duly reported
that the answer to our question was `4.294967e+09'---with no way to
see the full ten digits even though we knew they were there in the
program's memory! I was so annoyed, I vowed to write a calculator
of my own, once and for all.
I chose Emacs Lisp, a) because I had always been curious about it and b) because, being only a text editor extension language after all, Emacs Lisp would surely reach its limits long before the project got too far out of hand.
To make a long story short, Emacs Lisp turned out to be a distressingly solid implementation of Lisp, and the humble task of calculating turned out to be more open-ended than one might have expected.
Emacs Lisp doesn't have built-in floating point math, so it had to be simulated in software. In fact, Emacs integers will only comfortably fit six decimal digits or so--not enough for a decent calculator. So I had to write my own high-precision integer code as well, and once I had this I figured that arbitrary-size integers were just as easy as large integers. Arbitrary floating-point precision was the logical next step. Also, since the large integer arithmetic was there anyway it seemed only fair to give the user direct access to it, which in turn made it practical to support fractions as well as floats. All these features inspired me to look around for other data types that might be worth having.
Around this time, my friend Rick Koshi showed me his nifty new HP-28 calculator. It allowed the user to manipulate formulas as well as numerical quantities, and it could also operate on matrices. I decided that these would be good for Calc to have, too. And once things had gone this far, I figured I might as well take a look at serious algebra systems like Mathematica, Macsyma, and Maple for further ideas. Since these systems did far more than I could ever hope to implement, I decided to focus on rewrite rules and other programming features so that users could implement what they needed for themselves.
Rick complained that matrices were hard to read, so I put in code to format them in a 2D style. Once these routines were in place, Big mode was obligatory. Gee, what other language modes would be useful?
Scott Hemphill and Allen Knutson, two friends with a strong mathematical bent, contributed ideas and algorithms for a number of Calc features including modulo forms, primality testing, and float-to-fraction conversion.
Units were added at the eager insistence of Mass Sivilotti. Later, Ulrich Mueller at CERN and Przemek Klosowski at NIST provided invaluable expert assistance with the units table. As far as I can remember, the idea of using algebraic formulas and variables to represent units dates back to an ancient article in Byte magazine about muMath, an early algebra system for microcomputers.
Many people have contributed to Calc by reporting bugs and suggesting
features, large and small. A few deserve special mention: Tim Peters,
who helped develop the ideas that led to the selection commands, rewrite
rules, and many other algebra features; @c{Fran\c cois}
Francois Pinard, who contributed
an early prototype of the Calc Summary appendix as well as providing
valuable suggestions in many other areas of Calc; Carl Witty, whose eagle
eyes discovered many typographical and factual errors in the Calc manual;
Tim Kay, who drove the development of Embedded mode; Ove Ewerlid, who
made many suggestions relating to the algebra commands and contributed
some code for polynomial operations; Randal Schwartz, who suggested the
calc-eval
function; Robert J. Chassell, who suggested the Calc
Tutorial and exercises; and Juha Sarlin, who first worked out how to split
Calc into quickly-loading parts. Bob Weiner helped immensely with the
Lucid Emacs port.
Among the books used in the development of Calc were Knuth's Art of Computer Programming (especially volume II, Seminumerical Algorithms); Numerical Recipes by Press, Flannery, Teukolsky, and Vetterling; Bevington's Data Reduction and Error Analysis for the Physical Sciences; Concrete Mathematics by Graham, Knuth, and Patashnik; Steele's Common Lisp, the Language; the CRC Standard Math Tables (William H. Beyer, ed.); and Abramowitz and Stegun's venerable Handbook of Mathematical Functions. I consulted the user's manuals for the HP-28 and HP-48 calculators, as well as for the programs Mathematica, SMP, Macsyma, Maple, MathCAD, Gnuplot, and others. Also, of course, Calc could not have been written without the excellent GNU Emacs Lisp Reference Manual, by Bil Lewis and Dan LaLiberte.
Final thanks go to Richard Stallman, without whose fine implementations of the Emacs editor, language, and environment, Calc would have been finished in two weeks.