This is Info file calc.info, produced by Makeinfo-1.55 from the input file calc.texinfo. This file documents Calc, the GNU Emacs calculator. Copyright (C) 1990, 1991 Free Software Foundation, Inc. Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies. Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided also that the section entitled "GNU General Public License" is included exactly as in the original, and provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one. Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions, except that the section entitled "GNU General Public License" may be included in a translation approved by the author instead of in the original English. File: calc.info, Node: History and Acknowledgements, Prev: Using Calc, Up: Getting Started History and Acknowledgements ============================ 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 `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; 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. File: calc.info, Node: Interactive Tutorial, Up: Top Tutorial ******** Some brief instructions on using the Emacs Info system for this tutorial: Press the space bar and Delete keys to go forward and backward in a section by screenfuls (or use the regular Emacs scrolling commands for this). Press `n' or `p' to go to the Next or Previous section. If the section has a "menu", press a digit key like `1' or `2' to go to a sub-section from the menu. Press `u' to go back up from a sub-section to the menu it is part of. Exercises in the tutorial all have cross-references to the appropriate page of the "answers" section. Press `f', then the exercise number, to see the answer to an exercise. After you have followed a cross-reference, you can press the letter `l' to return to where you were before. You can press `?' at any time for a brief summary of Info commands. Press `1' now to enter the first section of the Tutorial. * Menu: * Tutorial:: File: calc.info, Node: Tutorial, Next: Introduction, Prev: Getting Started, Up: Top Tutorial ******** This chapter explains how to use Calc and its many features, in a step-by-step, tutorial way. You are encouraged to run Calc and work along with the examples as you read (*note Starting Calc::.). If you are already familiar with advanced calculators, you may wish to skip on to the rest of this manual. This tutorial describes the standard user interface of Calc only. The "Quick Mode" and "Keypad Mode" interfaces are fairly self-explanatory. *Note Embedded Mode::, for a description of the "Embedded Mode" interface. The easiest way to read this tutorial on-line is to have two windows on your Emacs screen, one with Calc and one with the Info system. (If you have a printed copy of the manual you can use that instead.) Press `M-# c' to turn Calc on or to switch into the Calc window, and press `M-# i' to start the Info system or to switch into its window. Or, you may prefer to use the tutorial in printed form. This tutorial is designed to be done in sequence. But the rest of this manual does not assume you have gone through the tutorial. The tutorial does not cover everything in the Calculator, but it touches on most general areas. You may wish to print out a copy of the Calc Summary and keep notes on it as you learn Calc. *Note Installation::, to see how to make a printed summary. *Note Summary::. * Menu: * Basic Tutorial:: * Arithmetic Tutorial:: * Vector/Matrix Tutorial:: * Types Tutorial:: * Algebra Tutorial:: * Programming Tutorial:: * Answers to Exercises:: File: calc.info, Node: Basic Tutorial, Next: Arithmetic Tutorial, Prev: Tutorial, Up: Tutorial Basic Tutorial ============== In this section, we learn how RPN and algebraic-style calculations work, how to undo and redo an operation done by mistake, and how to control various modes of the Calculator. * Menu: * RPN Tutorial:: Basic operations with the stack. * Algebraic Tutorial:: Algebraic entry; variables. * Undo Tutorial:: If you make a mistake: Undo and the trail. * Modes Tutorial:: Common mode-setting commands. File: calc.info, Node: RPN Tutorial, Next: Algebraic Tutorial, Prev: Basic Tutorial, Up: Basic Tutorial RPN Calculations and the Stack ------------------------------ Calc normally uses RPN notation. You may be familiar with the RPN system from Hewlett-Packard calculators, FORTH, or PostScript. (Reverse Polish Notation, RPN, is named after the Polish mathematician Jan Lukasiewicz.) The central component of an RPN calculator is the "stack". A calculator stack is like a stack of dishes. New dishes (numbers) are added at the top of the stack, and numbers are normally only removed from the top of the stack. In an operation like `2+3', the 2 and 3 are called the "operands" and the `+' is the "operator". In an RPN calculator you always enter the operands first, then the operator. Each time you type a number, Calc adds or "pushes" it onto the top of the Stack. When you press an operator key like `+', Calc "pops" the appropriate number of operands from the stack and pushes back the result. Thus we could add the numbers 2 and 3 in an RPN calculator by typing: `2 RET 3 RET +'. (The RET key, Return, corresponds to the ENTER key on traditional RPN calculators.) Try this now if you wish; type `M-# c' to switch into the Calc window (you can type `M-# c' again or `M-# o' to switch back to the Tutorial window). The first four keystrokes "push" the numbers 2 and 3 onto the stack. The `+' key "pops" the top two numbers from the stack, adds them, and pushes the result (5) back onto the stack. Here's how the stack will look at various points throughout the calculation: . 1: 2 2: 2 1: 5 . . 1: 3 . . M-# c 2 RET 3 RET + DEL The `.' symbol is a marker that represents the top of the stack. Note that the "top" of the stack is really shown at the bottom of the Stack window. This may seem backwards, but it turns out to be less distracting in regular use. The numbers `1:' and `2:' on the left are "stack level numbers". Old RPN calculators always had four stack levels called `x', `y', `z', and `t'. Calc's stack can grow as large as you like, so it uses numbers instead of letters. Some stack-manipulation commands accept a numeric argument that says which stack level to work on. Normal commands like `+' always work on the top few levels of the stack. The Stack buffer is just an Emacs buffer, and you can move around in it using the regular Emacs motion commands. But no matter where the cursor is, even if you have scrolled the `.' marker out of view, most Calc commands always move the cursor back down to level 1 before doing anything. It is possible to move the `.' marker upwards through the stack, temporarily "hiding" some numbers from commands like `+'. This is called "stack truncation" and we will not cover it in this tutorial; *note Truncating the Stack::., if you are interested. You don't really need the second RET in `2 RET 3 RET +'. That's because if you type any operator name or other non-numeric key when you are entering a number, the Calculator automatically enters that number and then does the requested command. Thus `2 RET 3 +' will work just as well. Examples in this tutorial will often omit RET even when the stack displays shown would only happen if you did press RET: 1: 2 2: 2 1: 5 . 1: 3 . . 2 RET 3 + Here, after pressing `3' the stack would really show `1: 2' with `Calc: 3' in the minibuffer. In these situations, you can press the optional RET to see the stack as the figure shows. (*) *Exercise 1.* (This tutorial will include exercises at various points. Try them if you wish. Answers to all the exercises are located at the end of the Tutorial chapter. Each exercise will include a cross-reference to its particular answer. If you are reading with the Emacs Info system, press `f' and the exercise number to go to the answer, then the letter `l' to return to where you were.) Here's the first exercise: What will the keystrokes `1 RET 2 RET 3 RET 4 + * -' compute? (`*' is the symbol for multiplication.) Figure it out by hand, then try it with Calc to see if you're right. *Note 1: RPN Answer 1. (*) (*) *Exercise 2.* Compute `2*4 + 7*9.5 + 5/4' using the stack. *Note 2: RPN Answer 2. (*) The DEL key is called Backspace on some keyboards. It is whatever key you would use to correct a simple typing error when regularly using Emacs. The DEL key pops and throws away the top value on the stack. (You can still get that value back from the Trail if you should need it later on.) There are many places in this tutorial where we assume you have used DEL to erase the results of the previous example at the beginning of a new example. In the few places where it is really important to use DEL to clear away old results, the text will remind you to do so. (It won't hurt to let things accumulate on the stack, except that whenever you give a display-mode-changing command Calc will have to spend a long time reformatting such a large stack.) Since the `-' key is also an operator (it subtracts the top two stack elements), how does one enter a negative number? Calc uses the `_' (underscore) key to act like the minus sign in a number. So, typing `-5 RET' won't work because the `-' key will try to do a subtraction, but `_5 RET' works just fine. You can also press `n', which means "change sign." It changes the number at the top of the stack (or the number being entered) from positive to negative or vice-versa: `5 n RET'. If you press RET when you're not entering a number, the effect is to duplicate the top number on the stack. Consider this calculation: 1: 3 2: 3 1: 9 2: 9 1: 81 . 1: 3 . 1: 9 . . . 3 RET RET * RET * (Of course, an easier way to do this would be `3 RET 4 ^', to raise 3 to the fourth power.) The space-bar key (denoted SPC here) performs the same function as RET; you could replace all three occurrences of RET in the above example with SPC and the effect would be the same. Another stack manipulation key is TAB. This exchanges the top two stack entries. Suppose you have computed `2 RET 3 +' to get 5, and then you realize what you really wanted to compute was `20 / (2+3)'. 1: 5 2: 5 2: 20 1: 4 . 1: 20 1: 5 . . . 2 RET 3 + 20 TAB / Planning ahead, the calculation would have gone like this: 1: 20 2: 20 3: 20 2: 20 1: 4 . 1: 2 2: 2 1: 5 . . 1: 3 . . 20 RET 2 RET 3 + / A related stack command is `M-TAB' (hold META and type TAB). It rotates the top three elements of the stack upward, bringing the object in level 3 to the top. 1: 10 2: 10 3: 10 3: 20 3: 30 . 1: 20 2: 20 2: 30 2: 10 . 1: 30 1: 10 1: 20 . . . 10 RET 20 RET 30 RET M-TAB M-TAB (*) *Exercise 3.* Suppose the numbers 10, 20, and 30 are on the stack. Figure out how to add one to the number in level 2 without affecting the rest of the stack. Also figure out how to add one to the number in level 3. *Note 3: RPN Answer 3. (*) Operations like `+', `-', `*', `/', and `^' pop two arguments from the stack and push a result. Operations like `n' and `Q' (square root) pop a single number and push the result. You can think of them as simply operating on the top element of the stack. 1: 3 1: 9 2: 9 1: 25 1: 5 . . 1: 16 . . . 3 RET RET * 4 RET RET * + Q (Note that capital `Q' means to hold down the Shift key while typing `q'. Remember, plain unshifted `q' is the Quit command.) Here we've used the Pythagorean Theorem to determine the hypotenuse of a right triangle. Calc actually has a built-in command for that called `f h', but let's suppose we can't remember the necessary keystrokes. We can still enter it by its full name using `M-x' notation: 1: 3 2: 3 1: 5 . 1: 4 . . 3 RET 4 RET M-x calc-hypot All Calculator commands begin with the word `calc-'. Since it gets tiring to type this, Calc provides an `x' key which is just like the regular Emacs `M-x' key except that it types the `calc-' prefix for you: 1: 3 2: 3 1: 5 . 1: 4 . . 3 RET 4 RET x hypot What happens if you take the square root of a negative number? 1: 4 1: -4 1: (0, 2) . . . 4 RET n Q The notation `(a, b)' represents a complex number. Complex numbers are more traditionally written `a + b i'; Calc can display in this format, too, but for now we'll stick to the `(a, b)' notation. If you don't know how complex numbers work, you can safely ignore this feature. Complex numbers only arise from operations that would be errors in a calculator that didn't have complex numbers. (For example, taking the square root or logarithm of a negative number produces a complex result.) Complex numbers are entered in the notation shown. The `(' and `,' and `)' keys manipulate "incomplete complex numbers." 1: ( ... 2: ( ... 1: (2, ... 1: (2, ... 1: (2, 3) . 1: 2 . 3 . . . ( 2 , 3 ) You can perform calculations while entering parts of incomplete objects. However, an incomplete object cannot actually participate in a calculation: 1: ( ... 2: ( ... 3: ( ... 1: ( ... 1: ( ... . 1: 2 2: 2 5 5 . 1: 3 . . . (error) ( 2 RET 3 + + Adding 5 to an incomplete object makes no sense, so the last command produces an error message and leaves the stack the same. Incomplete objects can't participate in arithmetic, but they can be moved around by the regular stack commands. 2: 2 3: 2 3: 3 1: ( ... 1: (2, 3) 1: 3 2: 3 2: ( ... 2 . . 1: ( ... 1: 2 3 . . . 2 RET 3 RET ( M-TAB M-TAB ) Note that the `,' (comma) key did not have to be used here. When you press `)' all the stack entries between the incomplete entry and the top are collected, so there's never really a reason to use the comma. It's up to you. (*) *Exercise 4.* To enter the complex number `(2, 3)', your friend Joe typed `( 2 , SPC 3 )'. What happened? (Joe thought of a clever way to correct his mistake in only two keystrokes, but it didn't quite work. Try it to find out why.) *Note 4: RPN Answer 4. (*) Vectors are entered the same way as complex numbers, but with square brackets in place of parentheses. We'll meet vectors again later in the tutorial. Any Emacs command can be given a "numeric prefix argument" by typing a series of META-digits beforehand. If META is awkward for you, you can instead type `C-u' followed by the necessary digits. Numeric prefix arguments can be negative, as in `M-- M-3 M-5' or `C-u - 3 5'. Calc commands use numeric prefix arguments in a variety of ways. For example, a numeric prefix on the `+' operator adds any number of stack entries at once: 1: 10 2: 10 3: 10 3: 10 1: 60 . 1: 20 2: 20 2: 20 . . 1: 30 1: 30 . . 10 RET 20 RET 30 RET C-u 3 + For stack manipulation commands like RET, a positive numeric prefix argument operates on the top N stack entries at once. A negative argument operates on the entry in level N only. An argument of zero operates on the entire stack. In this example, we copy the second-to-top element of the stack: 1: 10 2: 10 3: 10 3: 10 4: 10 . 1: 20 2: 20 2: 20 3: 20 . 1: 30 1: 30 2: 30 . . 1: 20 . 10 RET 20 RET 30 RET C-u -2 RET Another common idiom is `M-0 DEL', which clears the stack. (The `M-0' numeric prefix tells DEL to operate on the entire stack.) File: calc.info, Node: Algebraic Tutorial, Next: Undo Tutorial, Prev: RPN Tutorial, Up: Basic Tutorial Algebraic-Style Calculations ---------------------------- If you are not used to RPN notation, you may prefer to operate the Calculator in "algebraic mode," which is closer to the way non-RPN calculators work. In algebraic mode, you enter formulas in traditional `2+3' notation. You don't really need any special "mode" to enter algebraic formulas. You can enter a formula at any time by pressing the apostrophe (`'') key. Answer the prompt with the desired formula, then press RET. The formula is evaluated and the result is pushed onto the RPN stack. If you don't want to think in RPN at all, you can enter your whole computation as a formula, read the result from the stack, then press DEL to delete it from the stack. Try pressing the apostrophe key, then `2+3+4', then RET. The result should be the number 9. 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 2 + 3*4*5 / 6*7^8 - 9 is equivalent to 2 + ((3*4*5) / (6*(7^8)) - 9 or, in large mathematical notation, 3 * 4 * 5 2 + --------- - 9 8 6 * 7 The result of this expression will be the number -6.99999826533. 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!). If you tire of typing the apostrophe all the time, there is an "algebraic mode" you can select in which Calc automatically senses when you are about to type an algebraic expression. To enter this mode, press the two letters `m a'. (An `Alg' indicator should appear in the Calc window's mode line.) Press `m a', then `2+3+4' with no apostrophe, then RET. In algebraic mode, when you press any key that would normally begin entering a number (such as a digit, a decimal point, or the `_' key), or if you press `(' or `[', Calc automatically begins an algebraic entry. Functions which do not have operator symbols like `+' and `*' must be entered in formulas using function-call notation. For example, the function name corresponding to the square-root key `Q' is `sqrt'. To compute a square root in a formula, you would use the notation `sqrt(X)'. Press the apostrophe, then type `sqrt(5*2) - 3'. The result should be `0.16227766017'. Note that if the formula begins with a function name, you need to use the apostrophe even if you are in algebraic mode. If you type `arcsin' out of the blue, the `a r' will be taken as an Algebraic Rewrite command, and the `csin' will be taken as the name of the rewrite rule to use! Some people prefer to enter complex numbers and vectors in algebraic form because they find RPN entry with incomplete objects to be too distracting, even though they otherwise use Calc as an RPN calculator. Still in algebraic mode, type: 1: (2, 3) 2: (2, 3) 1: (8, -1) 2: (8, -1) 1: (9, -1) . 1: (1, -2) . 1: 1 . . . (2,3) RET (1,-2) RET * 1 RET + Algebraic mode allows us to enter complex numbers without pressing an apostrophe first, but it also means we need to press RET after every entry, even for a simple number like `1'. (You can type `C-u m a' to enable a special "incomplete algebraic mode" in which the `(' and `[' keys use algebraic entry even though regular numeric keys still use RPN numeric entry. There is also a "total algebraic mode," started by typing `m t', in which all normal keys begin algebraic entry. You must then use the META key to type Calc commands: `M-m t' to get back out of total algebraic mode, `M-q' to quit, etc. Total algebraic mode is not supported under Emacs 19.) If you're still in algebraic mode, press `m a' again to turn it off. Actual non-RPN calculators use a mixture of algebraic and RPN styles. In general, operators of two numbers (like `+' and `*') use algebraic form, but operators of one number (like `n' and `Q') use RPN form. Also, a non-RPN calculator allows you to see the intermediate results of a calculation as you go along. You can accomplish this in Calc by performing your calculation as a series of algebraic entries, using the `$' sign to tie them together. In an algebraic formula, `$' represents the number on the top of the stack. Here, we perform the calculation `sqrt(2*4+1)', which on a traditional calculator would be done by pressing `2 * 4 + 1 =' and then the square-root key. 1: 8 1: 9 1: 3 . . . ' 2*4 RET $+1 RET Q Notice that we didn't need to press an apostrophe for the `$+1', because the dollar sign always begins an algebraic entry. (*) *Exercise 1.* How could you get the same effect as pressing `Q' but using an algebraic entry instead? How about if the `Q' key on your keyboard were broken? *Note 1: Algebraic Answer 1. (*) The notations `$$', `$$$', and so on stand for higher stack entries. For example, `' $$+$ RET' is just like typing `+'. Algebraic formulas can include "variables". To store in a variable, press `s s', then type the variable name, then press RET. (There are actually two flavors of store command: `s s' stores a number in a variable but also leaves the number on the stack, while `s t' removes a number from the stack and stores it in the variable.) A variable name should consist of one or more letters or digits, beginning with a letter. 1: 17 . 1: a + a^2 1: 306 . . . 17 s t a RET ' a+a^2 RET = The `=' key "evaluates" a formula by replacing all its variables by the values that were stored in them. For RPN calculations, you can recall a variable's value on the stack either by entering its name as a formula and pressing `=', or by using the `s r' command. 1: 17 2: 17 3: 17 2: 17 1: 306 . 1: 17 2: 17 1: 289 . . 1: 2 . . s r a RET ' a RET = 2 ^ + If you press a single digit for a variable name (as in `s t 3', you get one of ten "quick variables" `q0' through `q9'. They are "quick" simply because you don't have to type the letter `q' or the RET after their names. In fact, you can type simply `s 3' as a shorthand for `s s 3', and likewise for `t 3' and `r 3'. Any variables in an algebraic formula for which you have not stored values are left alone, even when you evaluate the formula. 1: 2 a + 2 b 1: 34 + 2 b . . ' 2a+2b RET = Calls to function names which are undefined in Calc are also left alone, as are calls for which the value is undefined. 1: 2 + log10(0) + log10(x) + log10(5, 6) + foo(3) . ' log10(100) + log10(0) + log10(x) + log10(5,6) + foo(3) RET In this example, the first call to `log10' works, but the other calls are not evaluated. In the second call, the logarithm is undefined for that value of the argument; in the third, the argument is symbolic, and in the fourth, there are too many arguments. In the fifth case, there is no function called `foo'. You will see a "Wrong number of arguments" message referring to `log10(5,6)'. Press the `w' ("why") key to see any other messages that may have arisen from the last calculation. In this case you will get "logarithm of zero," then "number expected: `x'". Calc automatically displays the first message only if the message is sufficiently important; for example, Calc considers "wrong number of arguments" and "logarithm of zero" to be important enough to report automatically, while a message like "number expected: `x'" will only show up if you explicitly press the `w' key. (*) *Exercise 2.* Joe entered the formula `2 x y', stored 5 in `x', pressed `=', and got the expected result, `10 y'. He then tried the same for the formula `2 x (1+y)', expecting `10 (1+y)', but it didn't work. Why not? *Note 2: Algebraic Answer 2. (*) (*) *Exercise 3.* What result would you expect `1 RET 0 /' to give? What if you then type `0 *'? *Note 3: Algebraic Answer 3. (*) One interesting way to work with variables is to use the "evaluates-to" (`=>') operator. It works like this: Enter a formula algebraically in the usual way, but follow the formula with an `=>' symbol. (There is also an `s =' command which builds an `=>' formula using the stack.) On the stack, you will see two copies of the formula with an `=>' between them. The lefthand formula is exactly like you typed it; the righthand formula has been evaluated as if by typing `='. 2: 2 + 3 => 5 2: 2 + 3 => 5 1: 2 a + 2 b => 34 + 2 b 1: 2 a + 2 b => 20 + 2 b . . ' 2+3 => RET ' 2a+2b RET s = 10 s t a RET Notice that the instant we stored a new value in `a', all `=>' operators already on the stack that referred to `a' were updated to use the new value. With `=>', you can push a set of formulas on the stack, then change the variables experimentally to see the effects on the formulas' values. You can also "unstore" a variable when you are through with it: 2: 2 + 5 => 5 1: 2 a + 2 b => 2 a + 2 b . s u a RET We will encounter formulas involving variables and functions again when we discuss the algebra and calculus features of the Calculator. File: calc.info, Node: Undo Tutorial, Next: Modes Tutorial, Prev: Algebraic Tutorial, Up: Basic Tutorial Undo and Redo ------------- If you make a mistake, you can usually correct it by pressing shift-`U', the "undo" command. First, clear the stack (`M-0 DEL') and exit and restart Calc (`M-# M-# M-# M-#') to make sure things start off with a clean slate. Now: 1: 2 2: 2 1: 8 2: 2 1: 6 . 1: 3 . 1: 3 . . . 2 RET 3 ^ U * You can undo any number of times. Calc keeps a complete record of all you have done since you last opened the Calc window. After the above example, you could type: 1: 6 2: 2 1: 2 . . . 1: 3 . . (error) U U U U You can also type `D' to "redo" a command that you have undone mistakenly. . 1: 2 2: 2 1: 6 1: 6 . 1: 3 . . . (error) D D D D It was not possible to redo past the `6', since that was placed there by something other than an undo command. You can think of undo and redo as a sort of "time machine." Press `U' to go backward in time, `D' to go forward. If you go backward and do something (like `*') then, as any science fiction reader knows, you have changed your future and you cannot go forward again. Thus, the inability to redo past the `6' even though there was an earlier undo command. You can always recall an earlier result using the Trail. We've ignored the trail so far, but it has been faithfully recording everything we did since we loaded the Calculator. If the Trail is not displayed, press `t d' now to turn it on. Let's try grabbing an earlier result. The `8' we computed was undone by a `U' command, and was lost even to Redo when we pressed `*', but it's still there in the trail. There should be a little `>' arrow (the "trail pointer") resting on the last trail entry. If there isn't, press `t ]' to reset the trail pointer. Now, press `t p' to move the arrow onto the line containing `8', and press `t y' to "yank" that number back onto the stack. If you press `t ]' again, you will see that even our Yank command went into the trail. Let's go further back in time. Earlier in the tutorial we computed a huge integer using the formula `2^3^4'. We don't remember what it was, but the first digits were "241". Press `t r' (which stands for trail-search-reverse), then type `241'. The trail cursor will jump back to the next previous occurrence of the string "241" in the trail. This is just a regular Emacs incremental search; you can now press `C-s' or `C-r' to continue the search forwards or backwards as you like. To finish the search, press RET. This halts the incremental search and leaves the trail pointer at the thing we found. Now we can type `t y' to yank that number onto the stack. If we hadn't remembered the "241", we could simply have searched for `2^3^4', then pressed `RET t n' to halt and then move to the next item. You may have noticed that all the trail-related commands begin with the letter `t'. (The store-and-recall commands, on the other hand, all began with `s'.) Calc has so many commands that there aren't enough keys for all of them, so various commands are grouped into two-letter sequences where the first letter is called the "prefix" key. If you type a prefix key by accident, you can press `C-g' to cancel it. (In fact, you can press `C-g' to cancel almost anything in Emacs.) To get help on a prefix key, press that key followed by `?'. Some prefixes have several lines of help, so you need to press `?' repeatedly to see them all. This may not work under Lucid Emacs, but you can also type `h h' to see all the help at once. Try pressing `t ?' now. You will see a line of the form, trail/time: Display; Fwd, Back; Next, Prev, Here, [, ]; Yank: [MORE] t- The word "trail" indicates that the `t' prefix key contains trail-related commands. Each entry on the line shows one command, with a single capital letter showing which letter you press to get that command. We have used `t n', `t p', `t ]', and `t y' so far. The `[MORE]' means you can press `?' again to see more `t'-prefix comands. Notice that the commands are roughly divided (by semicolons) into related groups. When you are in the help display for a prefix key, the prefix is still active. If you press another key, like `y' for example, it will be interpreted as a `t y' command. If all you wanted was to look at the help messages, press `C-g' afterwards to cancel the prefix. One more way to correct an error is by editing the stack entries. The actual Stack buffer is marked read-only and must not be edited directly, but you can press ``' (the backquote or accent grave) to edit a stack entry. Try entering `3.141439' now. If this is supposed to represent `pi', it's got several errors. Press ``' to edit this number. Now use the normal Emacs cursor motion and editing keys to change the second 4 to a 5, and to transpose the 3 and the 9. When you press RET, the number on the stack will be replaced by your new number. This works for formulas, vectors, and all other types of values you can put on the stack. The ``' key also works during entry of a number or algebraic formula.