calc.texi 1.42 MB
Newer Older
Glenn Morris's avatar
Glenn Morris committed
1 2 3
\input texinfo                  @c -*-texinfo-*-
@comment %**start of header (This is for running Texinfo on a region.)
@c smallbook
4
@setfilename ../../info/calc
Glenn Morris's avatar
Glenn Morris committed
5
@c [title]
Jay Belanger's avatar
Jay Belanger committed
6
@settitle GNU Emacs Calc Manual
Glenn Morris's avatar
Glenn Morris committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
@setchapternewpage odd
@comment %**end of header (This is for running Texinfo on a region.)

@c The following macros are used for conditional output for single lines.
@c @texline foo
@c    `foo' will appear only in TeX output
@c @infoline foo
@c    `foo' will appear only in non-TeX output

@c @expr{expr} will typeset an expression;
@c $x$ in TeX, @samp{x} otherwise.

@iftex
@macro texline
@end macro
@alias infoline=comment
@alias expr=math
@alias tfn=code
@alias mathit=expr
26
@alias summarykey=key
Glenn Morris's avatar
Glenn Morris committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
@macro cpi{}
@math{@pi{}}
@end macro
@macro cpiover{den}
@math{@pi/\den\}
@end macro
@end iftex

@ifnottex
@alias texline=comment
@macro infoline{stuff}
\stuff\
@end macro
@alias expr=samp
@alias tfn=t
@alias mathit=i
43 44 45
@macro summarykey{ky}
\ky\
@end macro
Glenn Morris's avatar
Glenn Morris committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
@macro cpi{}
@expr{pi}
@end macro
@macro cpiover{den}
@expr{pi/\den\}
@end macro
@end ifnottex


@tex
% Suggested by Karl Berry <karl@@freefriends.org>
\gdef\!{\mskip-\thinmuskip}
@end tex

@c Fix some other things specifically for this manual.
@iftex
@finalout
@mathcode`@:=`@:  @c Make Calc fractions come out right in math mode
@tex
\gdef\coloneq{\mathrel{\mathord:\mathord=}}

\gdef\beforedisplay{\vskip-10pt}
\gdef\afterdisplay{\vskip-5pt}
\gdef\beforedisplayh{\vskip-25pt}
\gdef\afterdisplayh{\vskip-10pt}
@end tex
@newdimen@kyvpos @kyvpos=0pt
@newdimen@kyhpos @kyhpos=0pt
@newcount@calcclubpenalty @calcclubpenalty=1000
@ignore
@newcount@calcpageno
@newtoks@calcoldeverypar @calcoldeverypar=@everypar
@everypar={@calceverypar@the@calcoldeverypar}
@ifx@turnoffactive@undefinedzzz@def@turnoffactive{}@fi
@ifx@ninett@undefinedzzz@font@ninett=cmtt9@fi
@catcode`@\=0 \catcode`\@=11
\r@ggedbottomtrue
\catcode`\@=0 @catcode`@\=@active
@end ignore
@end iftex

@copying
Jay Belanger's avatar
Jay Belanger committed
88
@ifinfo
Glenn Morris's avatar
Glenn Morris committed
89
This file documents Calc, the GNU Emacs calculator.
Jay Belanger's avatar
Jay Belanger committed
90 91 92 93
@end ifinfo
@ifnotinfo
This file documents Calc, the GNU Emacs calculator, included with GNU Emacs 23.1.
@end ifnotinfo
Glenn Morris's avatar
Glenn Morris committed
94 95

Copyright @copyright{} 1990, 1991, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
96
2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
Glenn Morris's avatar
Glenn Morris committed
97 98 99

@quotation
Permission is granted to copy, distribute and/or modify this document
100
under the terms of the GNU Free Documentation License, Version 1.3 or
Glenn Morris's avatar
Glenn Morris committed
101 102 103 104 105 106
any later version published by the Free Software Foundation; with the
Invariant Sections being just ``GNU GENERAL PUBLIC LICENSE'', with the
Front-Cover texts being ``A GNU Manual,'' and with the Back-Cover
Texts as in (a) below.  A copy of the license is included in the section
entitled ``GNU Free Documentation License.''

107 108 109
(a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
modify this GNU manual.  Buying copies from the FSF supports it in
developing GNU and promoting software freedom.''
Glenn Morris's avatar
Glenn Morris committed
110 111 112 113 114 115 116 117 118 119 120 121
@end quotation
@end copying

@dircategory Emacs
@direntry
* Calc: (calc).         Advanced desk calculator and mathematical tool.
@end direntry

@titlepage
@sp 6
@center @titlefont{Calc Manual}
@sp 4
Jay Belanger's avatar
Jay Belanger committed
122
@center GNU Emacs Calc
Glenn Morris's avatar
Glenn Morris committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
@c [volume]
@sp 5
@center Dave Gillespie
@center daveg@@synaptics.com
@page

@vskip 0pt plus 1filll
@insertcopying
@end titlepage


@summarycontents

@c [end]

@contents

@c [begin]
@ifnottex
@node Top, Getting Started, (dir), (dir)
@chapter The GNU Emacs Calculator

@noindent
@dfn{Calc} is an advanced desk calculator and mathematical tool
written by Dave Gillespie that runs as part of the GNU Emacs environment.

This manual, also written (mostly) by Dave Gillespie, is divided into
three major parts: ``Getting Started,'' the ``Calc Tutorial,'' and the
``Calc Reference.''  The Tutorial introduces all the major aspects of
Calculator use in an easy, hands-on way.  The remainder of the manual is
a complete reference to the features of the Calculator.
@end ifnottex

@ifinfo
For help in the Emacs Info system (which you are using to read this
file), type @kbd{?}.  (You can also type @kbd{h} to run through a
longer Info tutorial.)
@end ifinfo

162 163
@insertcopying

Glenn Morris's avatar
Glenn Morris committed
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
@menu
* Getting Started::       General description and overview.
@ifinfo
* Interactive Tutorial::
@end ifinfo
* Tutorial::              A step-by-step introduction for beginners.

* Introduction::          Introduction to the Calc reference manual.
* Data Types::            Types of objects manipulated by Calc.
* Stack and Trail::       Manipulating the stack and trail buffers.
* Mode Settings::         Adjusting display format and other modes.
* Arithmetic::            Basic arithmetic functions.
* Scientific Functions::  Transcendentals and other scientific functions.
* Matrix Functions::      Operations on vectors and matrices.
* Algebra::               Manipulating expressions algebraically.
* Units::                 Operations on numbers with units.
* Store and Recall::      Storing and recalling variables.
* Graphics::              Commands for making graphs of data.
* Kill and Yank::         Moving data into and out of Calc.
* Keypad Mode::           Operating Calc from a keypad.
* Embedded Mode::         Working with formulas embedded in a file.
* Programming::           Calc as a programmable calculator.

* Copying::               How you can copy and share Calc.
* GNU Free Documentation License:: The license for this documentation.
* Customizing Calc::      Customizing Calc.
* Reporting Bugs::        How to report bugs and make suggestions.

* Summary::               Summary of Calc commands and functions.

* Key Index::             The standard Calc key sequences.
* Command Index::         The interactive Calc commands.
* Function Index::        Functions (in algebraic formulas).
* Concept Index::         General concepts.
* Variable Index::        Variables used by Calc (both user and internal).
* Lisp Function Index::   Internal Lisp math functions.
@end menu

@ifinfo
@node Getting Started, Interactive Tutorial, Top, Top
@end ifinfo
@ifnotinfo
@node Getting Started, Tutorial, Top, Top
@end ifnotinfo
@chapter Getting Started
@noindent
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.

@menu
* What is Calc::
* About This Manual::
* Notations Used in This Manual::
* Demonstration of Calc::
* Using Calc::
* History and Acknowledgements::
@end menu

@node What is Calc, About This Manual, Getting Started, Getting Started
@section What is Calc?

@noindent
@dfn{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:

@itemize @bullet
@item
Choice of algebraic or RPN (stack-based) entry of calculations.

@item
Arbitrary precision integers and floating-point numbers.

@item
Arithmetic on rational numbers, complex numbers (rectangular and polar),
error forms with standard deviations, open and closed intervals, vectors
and matrices, dates and times, infinities, sets, quantities with units,
and algebraic formulas.

@item
Mathematical operations such as logarithms and trigonometric functions.

@item
Programmer's features (bitwise operations, non-decimal numbers).

@item
Financial functions such as future value and internal rate of return.

@item
Number theoretical features such as prime factorization and arithmetic
modulo @var{m} for any @var{m}.

@item
Algebraic manipulation features, including symbolic calculus.

@item
Moving data to and from regular editing buffers.

@item
Embedded mode for manipulating Calc formulas and data directly
inside any editing buffer.

@item
Graphics using GNUPLOT, a versatile (and free) plotting program.

@item
Easy programming using keyboard macros, algebraic formulas,
algebraic rewrite rules, or extended Emacs Lisp.
@end itemize

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.  Calc does not
have the scope and depth of a fully-functional symbolic math package,
but Calc has the advantages of convenience, portability, and freedom.

@node About This Manual, Notations Used in This Manual, What is Calc, Getting Started
@section About This Manual

@noindent
This document serves as a complete description of the GNU Emacs
289
Calculator.  It works both as an introduction for novices and as
Glenn Morris's avatar
Glenn Morris committed
290 291 292 293 294
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.

295
This manual is divided into three major parts:@: the ``Getting
296 297
Started'' chapter you are reading now, the Calc tutorial, and the Calc
reference manual.
Glenn Morris's avatar
Glenn Morris committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
@c [when-split]
@c This manual has been printed in two volumes, the @dfn{Tutorial} and the
@c @dfn{Reference}.  Both volumes include a copy of the ``Getting Started''
@c chapter.

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.
@xref{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.

323
@c @cindex Marginal notes
Glenn Morris's avatar
Glenn Morris committed
324 325 326
Every Calc keyboard command is listed in the Calc Summary, and also
in the Key Index.  Algebraic functions, @kbd{M-x} commands, and
variables also have their own indices.  
327 328 329 330
@c @texline Each
@c @infoline In the printed manual, each
@c paragraph that is referenced in the Key or Function Index is marked
@c in the margin with its index entry.
Glenn Morris's avatar
Glenn Morris committed
331 332

@c [fix-ref Help Commands]
333 334 335 336 337 338 339 340 341
You can access this manual on-line at any time within Calc by pressing
the @kbd{h i} key sequence.  Outside of the Calc window, you can press
@kbd{C-x * i} to read the manual on-line.  From within Calc the command
@kbd{h t} will jump directly to the Tutorial; from outside of Calc the
command @kbd{C-x * t} will jump to the Tutorial and start Calc if
necessary.  Pressing @kbd{h s} or @kbd{C-x * s} will take you directly
to the Calc Summary.  Within Calc, you can also go to the part of the
manual describing any Calc key, function, or variable using 
@w{@kbd{h k}}, @kbd{h f}, or @kbd{h v}, respectively.  @xref{Help Commands}.
Glenn Morris's avatar
Glenn Morris committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

@ifnottex
The Calc manual can be printed, but because the manual is so large, you
should only make a printed copy if you really need it.  To print the
manual, you will need the @TeX{} typesetting program (this is a free
program by Donald Knuth at Stanford University) as well as the
@file{texindex} program and @file{texinfo.tex} file, both of which can
be obtained from the FSF as part of the @code{texinfo} package.
To print the Calc manual in one huge tome, you will need the
source code to this manual, @file{calc.texi}, available as part of the
Emacs source.  Once you have this file, type @kbd{texi2dvi calc.texi}.
Alternatively, change to the @file{man} subdirectory of the Emacs
source distribution, and type @kbd{make calc.dvi}. (Don't worry if you
get some ``overfull box'' warnings while @TeX{} runs.)
The result will be a device-independent output file called
@file{calc.dvi}, which you must print in whatever way is right
for your system.  On many systems, the command is

@example
lpr -d calc.dvi
@end example

@noindent
or

@example
dvips calc.dvi
@end example
@end ifnottex
@c Printed copies of this manual are also available from the Free Software
@c Foundation.

@node Notations Used in This Manual, Demonstration of Calc, About This Manual, Getting Started
@section Notations Used in This Manual

@noindent
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 @kbd{C-x}.  Keys pressed with Meta held down
are shown as @kbd{M-x}.  Other notations are @key{RET} for the
Return key, @key{SPC} for the space bar, @key{TAB} for the Tab key,
@key{DEL} for the Delete key, and @key{LFD} for the Line-Feed key.
The @key{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.

(If you don't have the @key{LFD} or @key{TAB} keys on your keyboard,
the @kbd{C-j} and @kbd{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 @key{ESC} or @kbd{C-[} first to get the same effect, so
that @kbd{M-x}, @kbd{@key{ESC} x}, and @kbd{C-[ x} are all equivalent.)

Sometimes the @key{RET} key is not shown when it is ``obvious''
that you must press @key{RET} to proceed.  For example, the @key{RET}
is usually omitted in key sequences like @kbd{M-x calc-keypad @key{RET}}.

Commands are generally shown like this:  @kbd{p} (@code{calc-precision})
or @kbd{C-x * k} (@code{calc-keypad}).  This means that the command is
normally used by pressing the @kbd{p} key or @kbd{C-x * k} key sequence,
but it also has the full-name equivalent shown, e.g., @kbd{M-x calc-precision}.

Commands that correspond to functions in algebraic notation
are written:  @kbd{C} (@code{calc-cos}) [@code{cos}].  This means
the @kbd{C} key is equivalent to @kbd{M-x calc-cos}, and that
the corresponding function in an algebraic-style formula would
be @samp{cos(@var{x})}.

A few commands don't have key equivalents:  @code{calc-sincos}
[@code{sincos}].

@node Demonstration of Calc, Using Calc, Notations Used in This Manual, Getting Started
@section A Demonstration of Calc

@noindent
@cindex Demonstration of Calc
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 @code{emacs}
does this), and type @kbd{C-x * c} to start the
Calculator.  (You can also use @kbd{M-x calc} if this doesn't work.
@xref{Starting Calc}, for various ways of starting the Calculator.)

Be sure to type all the sample input exactly, especially noting the
difference between lower-case and upper-case letters.  Remember,
@key{RET}, @key{TAB}, @key{DEL}, and @key{SPC} are the Return, Tab,
Delete, and Space keys.

@strong{RPN calculation.}  In RPN, you type the input number(s) first,
then the command to operate on the numbers.

@noindent
Type @kbd{2 @key{RET} 3 + Q} to compute 
@texline @math{\sqrt{2+3} = 2.2360679775}.
@infoline the square root of 2+3, which is 2.2360679775.

@noindent
Type @kbd{P 2 ^} to compute 
@texline @math{\pi^2 = 9.86960440109}.
@infoline the value of `pi' squared, 9.86960440109.

@noindent
Type @key{TAB} to exchange the order of these two results.

@noindent
Type @kbd{- I H S} to subtract these results and compute the Inverse
Hyperbolic sine of the difference, 2.72996136574.

@noindent
Type @key{DEL} to erase this result.

@strong{Algebraic calculation.}  You can also enter calculations using
conventional ``algebraic'' notation.  To enter an algebraic formula,
use the apostrophe key.

@noindent
Type @kbd{' sqrt(2+3) @key{RET}} to compute 
@texline @math{\sqrt{2+3}}.
@infoline the square root of 2+3.

@noindent
Type @kbd{' pi^2 @key{RET}} to enter 
@texline @math{\pi^2}.
@infoline `pi' squared.  
To evaluate this symbolic formula as a number, type @kbd{=}.

@noindent
Type @kbd{' arcsinh($ - $$) @key{RET}} to subtract the second-most-recent
result from the most-recent and compute the Inverse Hyperbolic sine.

@strong{Keypad mode.}  If you are using the X window system, press
@w{@kbd{C-x * k}} to get Keypad mode.  (If you don't use X, skip to
the next section.)

@noindent
Click on the @key{2}, @key{ENTER}, @key{3}, @key{+}, and @key{SQRT}
``buttons'' using your left mouse button.

@noindent
Click on @key{PI}, @key{2}, and @tfn{y^x}.

@noindent
Click on @key{INV}, then @key{ENTER} to swap the two results.

@noindent
Click on @key{-}, @key{INV}, @key{HYP}, and @key{SIN}.

@noindent
Click on @key{<-} to erase the result, then click @key{OFF} to turn
the Keypad Calculator off.

@strong{Grabbing data.}  Type @kbd{C-x * x} if necessary to exit Calc.
Now select the following numbers as an Emacs region:  ``Mark'' the
front of the list by typing @kbd{C-@key{SPC}} or @kbd{C-@@} there,
then move to the other end of the list.  (Either get this list from
the on-line copy of this manual, accessed by @w{@kbd{C-x * i}}, or just
type these numbers into a scratch file.)  Now type @kbd{C-x * g} to
``grab'' these numbers into Calc.

@example
@group
1.23  1.97
1.6   2
1.19  1.08
@end group
@end example

@noindent
The result @samp{[1.23, 1.97, 1.6, 2, 1.19, 1.08]} is a Calc ``vector.''
Type @w{@kbd{V R +}} to compute the sum of these numbers.

@noindent
Type @kbd{U} to Undo this command, then type @kbd{V R *} to compute
the product of the numbers.

@noindent
You can also grab data as a rectangular matrix.  Place the cursor on
the upper-leftmost @samp{1} and set the mark, then move to just after
the lower-right @samp{8} and press @kbd{C-x * r}.

@noindent
Type @kbd{v t} to transpose this 
@texline @math{3\times2}
@infoline 3x2 
matrix into a 
@texline @math{2\times3}
@infoline 2x3
matrix.  Type @w{@kbd{v u}} to unpack the rows into two separate
vectors.  Now type @w{@kbd{V R + @key{TAB} V R +}} to compute the sums
of the two original columns. (There is also a special
grab-and-sum-columns command, @kbd{C-x * :}.)

@strong{Units conversion.}  Units are entered algebraically.
Type @w{@kbd{' 43 mi/hr @key{RET}}} to enter the quantity 43 miles-per-hour.
Type @w{@kbd{u c km/hr @key{RET}}}.  Type @w{@kbd{u c m/s @key{RET}}}.

@strong{Date arithmetic.}  Type @kbd{t N} to get the current date and
time.  Type @kbd{90 +} to find the date 90 days from now.  Type
@kbd{' <25 dec 87> @key{RET}} to enter a date, then @kbd{- 7 /} to see how
many weeks have passed since then.

@strong{Algebra.}  Algebraic entries can also include formulas
or equations involving variables.  Type @kbd{@w{' [x + y} = a, x y = 1] @key{RET}}
to enter a pair of equations involving three variables.
(Note the leading apostrophe in this example; also, note that the space
552
in @samp{x y} is required.)  Type @w{@kbd{a S x,y @key{RET}}} to solve
Glenn Morris's avatar
Glenn Morris committed
553 554 555 556 557 558 559 560 561 562 563
these equations for the variables @expr{x} and @expr{y}.

@noindent
Type @kbd{d B} to view the solutions in more readable notation.
Type @w{@kbd{d C}} to view them in C language notation, @kbd{d T}
to view them in the notation for the @TeX{} typesetting system,
and @kbd{d L} to view them in the notation for the La@TeX{} typesetting
system.  Type @kbd{d N} to return to normal notation.

@noindent
Type @kbd{7.5}, then @kbd{s l a @key{RET}} to let @expr{a = 7.5} in these formulas.
564
(That's the letter @kbd{l}, not the numeral @kbd{1}.)
Glenn Morris's avatar
Glenn Morris committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

@ifnotinfo
@strong{Help functions.}  You can read about any command in the on-line
manual.  Type @kbd{C-x * c} to return to Calc after each of these
commands: @kbd{h k t N} to read about the @kbd{t N} command,
@kbd{h f sqrt @key{RET}} to read about the @code{sqrt} function, and
@kbd{h s} to read the Calc summary.
@end ifnotinfo
@ifinfo
@strong{Help functions.}  You can read about any command in the on-line
manual.  Remember to type the letter @kbd{l}, then @kbd{C-x * c}, to
return here after each of these commands: @w{@kbd{h k t N}} to read
about the @w{@kbd{t N}} command, @kbd{h f sqrt @key{RET}} to read about the
@code{sqrt} function, and @kbd{h s} to read the Calc summary.
@end ifinfo

Press @key{DEL} repeatedly to remove any leftover results from the stack.
To exit from Calc, press @kbd{q} or @kbd{C-x * c} again.

@node Using Calc, History and Acknowledgements, Demonstration of Calc, Getting Started
@section Using Calc

@noindent
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.

@menu
* Starting Calc::
* The Standard Interface::
* Quick Mode Overview::
* Keypad Mode Overview::
* Standalone Operation::
* Embedded Mode Overview::
* Other C-x * Commands::
@end menu

@node Starting Calc, The Standard Interface, Using Calc, Using Calc
@subsection Starting Calc

@noindent
On most systems, you can type @kbd{C-x *} to start the Calculator.
The key sequence @kbd{C-x *} is bound to the command @code{calc-dispatch}, 
which can be rebound if convenient (@pxref{Customizing Calc}).

When you press @kbd{C-x *}, Emacs waits for you to press a second key to
complete the command.  In this case, you will follow @kbd{C-x *} with a
letter (upper- or lower-case, it doesn't matter for @kbd{C-x *}) that says
which Calc interface you want to use.

To get Calc's standard interface, type @kbd{C-x * c}.  To get
Keypad mode, type @kbd{C-x * k}.  Type @kbd{C-x * ?} to get a brief
list of the available options, and type a second @kbd{?} to get
a complete list.

To ease typing, @kbd{C-x * *} also works to start Calc.  It starts the
same interface (either @kbd{C-x * c} or @w{@kbd{C-x * k}}) that you last
used, selecting the @kbd{C-x * c} interface by default.

If @kbd{C-x *} doesn't work for you, you can always type explicit
commands like @kbd{M-x calc} (for the standard user interface) or
@w{@kbd{M-x calc-keypad}} (for Keypad mode).  First type @kbd{M-x}
(that's Meta with the letter @kbd{x}), then, at the prompt,
type the full command (like @kbd{calc-keypad}) and press Return.

The same commands (like @kbd{C-x * c} or @kbd{C-x * *}) that start
the Calculator also turn it off if it is already on.

@node The Standard Interface, Quick Mode Overview, Starting Calc, Using Calc
@subsection The Standard Calc Interface

@noindent
@cindex Standard user interface
Calc's standard interface acts like a traditional RPN calculator,
operated by the normal Emacs keyboard.  When you type @kbd{C-x * 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.

@smallexample
@group

...
--**-Emacs: myfile             (Fundamental)----All----------------------
--- Emacs Calculator Mode ---                   |Emacs Calculator Trail
2:  17.3                                        |    17.3
1:  -5                                          |    3
    .                                           |    2
                                                |    4
                                                |  * 8
                                                |  ->-5
                                                |
656
--%*-Calc: 12 Deg       (Calculator)----All----- --%*- *Calc Trail*
Glenn Morris's avatar
Glenn Morris committed
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
@end group
@end smallexample

In this figure, the mode-line for @file{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 @dfn{stack window} and the righthand one is called the
@dfn{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 @mathit{-5}.
(The @samp{>} symbol shows that this was the most recent calculation.)
The net result is the two numbers 17.3 and @mathit{-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 @dfn{prefix} keys that begin two-letter
commands.  For example, @kbd{e} means ``enter exponent'' and shifted
@kbd{E} means @expr{e^x}.  With the @kbd{d} (``display modes'') prefix
the letter ``e'' takes on very different meanings:  @kbd{d e} means
``engineering notation'' and @kbd{d E} means ``@dfn{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
@w{@kbd{C-x o}} (@code{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 @kbd{C-x * c} a second time, the Calculator
windows go away but the actual Stack and Trail are not gone, just
hidden.  When you press @kbd{C-x * 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, @kbd{C-x * c} will give you
a fresh stack and trail.  There is a command (@kbd{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 @kbd{C-x * *} the same as @kbd{C-x * c}.

The @kbd{q} key is another equivalent way to turn the Calculator off.

If you type @kbd{C-x * b} first and then @kbd{C-x * c}, you get a
full-screen version of Calc (@code{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 @kbd{q} or @kbd{C-x * c} again to quit,
the file you were editing before reappears.  The @kbd{C-x * b} key
switches back and forth between ``big'' full-screen mode and the
normal partial-screen mode.

Finally, @kbd{C-x * o} (@code{calc-other-window}) is like @kbd{C-x * c}
except that the Calc window is not selected.  The buffer you were
721 722 723 724 725
editing before remains selected instead.  If you are in a Calc window,
then @kbd{C-x * o} will switch you out of it, being careful not to
switch you to the Calc Trail window.  So @kbd{C-x * o} is a handy
way to switch out of Calc momentarily to edit your file; you can then
type @kbd{C-x * c} to switch back into Calc when you are done.
Glenn Morris's avatar
Glenn Morris committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775

@node Quick Mode Overview, Keypad Mode Overview, The Standard Interface, Using Calc
@subsection Quick Mode (Overview)

@noindent
@dfn{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 @kbd{C-x * q}
(@code{quick-calc}) in any regular editing buffer.

Quick mode is very simple:  It prompts you to type any formula in
standard algebraic notation (like @samp{4 - 2/3}) and then displays
the result at the bottom of the Emacs screen (@mathit{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 @kbd{C-x * q}
again to do another quick calculation.  The result of the calculation
will also be in the Emacs ``kill ring'' so that a @kbd{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 @kbd{C-x * c}) to change the mode settings.

@c [fix-ref Quick Calculator mode]
@xref{Quick Calculator}, for further information.

@node Keypad Mode Overview, Standalone Operation, Quick Mode Overview, Using Calc
@subsection Keypad Mode (Overview)

@noindent
@dfn{Keypad mode} is a mouse-based interface to the Calculator.
It is designed for use with terminals that support a mouse.  If you
don't have a mouse, you will have to operate Keypad mode with your
arrow keys (which is probably more trouble than it's worth).

Type @kbd{C-x * 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.

@tex
\dimen0=\pagetotal%
\advance \dimen0 by 24\baselineskip%
\ifdim \dimen0>\pagegoal \vfill\eject \fi%
\medskip
@end tex
@smallexample
@group
|--- Emacs Calculator Mode ---
|2:  17.3
|1:  -5
|    .
776
|--%*-Calc: 12 Deg       (Calcul
Jay Belanger's avatar
Jay Belanger committed
777
|----+----+--Calc---+----+----1
Glenn Morris's avatar
Glenn Morris committed
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
|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  |  +  |
|-----+-----+-----+-----+-----+
@end group
@end smallexample

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 @w{@kbd{1 7 .@: 3 ENTER 5 +/- ENTER}}; to
add them together you would then click @kbd{+} (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.

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 @kbd{C-x * b} first, you get a full-screen Keypad mode
(@code{full-calc-keypad}) with three windows:  The keypad in the lower
left, the stack in the lower right, and the trail on top.

@c [fix-ref Keypad Mode]
@xref{Keypad Mode}, for further information.

@node Standalone Operation, Embedded Mode Overview, Keypad Mode Overview, Using Calc
@subsection Standalone Operation

@noindent
@cindex Standalone Operation
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:

@example
emacs -f full-calc
@end example

@noindent
or

@example
emacs -f full-calc-keypad
@end example

@noindent
which run a full-screen Calculator (as if by @kbd{C-x * b C-x * c}) or
a full-screen X-based Calculator (as if by @kbd{C-x * b C-x * k}).
In standalone operation, quitting the Calculator (by pressing
@kbd{q} or clicking on the keypad @key{EXIT} button) quits Emacs
itself.

@node Embedded Mode Overview, Other C-x * Commands, Standalone Operation, Using Calc
@subsection Embedded Mode (Overview)

@noindent
@dfn{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:

@smallexample
@group
The derivative of

                                   ln(ln(x))

is
@end group
@end smallexample

@noindent
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
877 878
you want the result to be, leaving a blank line before and after the
formula:
Glenn Morris's avatar
Glenn Morris committed
879 880 881 882 883 884 885 886 887 888 889 890 891 892

@smallexample
@group
The derivative of

                                   ln(ln(x))

is

                                   ln(ln(x))
@end group
@end smallexample

Now, move the cursor onto this new formula and press @kbd{C-x * e}.
893 894 895 896 897 898 899 900 901 902
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 line with the formula will now appear as it would on the Calc stack
(in this case, it will be left-aligned) and the buffer's mode line will
change to look like the Calc mode line (with mode indicators like
@samp{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 @kbd{a d x @key{RET}}.
Glenn Morris's avatar
Glenn Morris committed
903 904 905 906 907 908 909 910 911 912 913 914 915

@smallexample
@group
The derivative of

                                   ln(ln(x))

is

1 / ln(x) x
@end group
@end smallexample

916 917
(Note that by default, Calc gives division lower precedence than multiplication,
so that @samp{1 / ln(x) x} is equivalent to @samp{1 / (ln(x) x)}.)
918

Glenn Morris's avatar
Glenn Morris committed
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
To make this look nicer, you might want to press @kbd{d =} to center
the formula, and even @kbd{d B} to use Big display mode.

@smallexample
@group
The derivative of

                                   ln(ln(x))

is
% [calc-mode: justify: center]
% [calc-mode: language: big]

                                       1
                                    -------
                                    ln(x) x
@end group
@end smallexample

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{} or
La@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 @kbd{d @} (1) @key{RET}}.

@smallexample
@group
% [calc-mode: justify: center]
% [calc-mode: language: big]
% [calc-mode: right-label: " (1)"]

                                       1
                                    -------                      (1)
                                    ln(x) x
@end group
@end smallexample

To leave Embedded mode, type @kbd{C-x * e} again.  The mode line
and keyboard will revert to the way they were before.

The related command @kbd{C-x * w} operates on a single word, which
964 965 966
generally means a single number, inside text.  It searches for an
expression which ``looks'' like a number containing the point.
Here's an example of its use:
Glenn Morris's avatar
Glenn Morris committed
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175

@smallexample
A slope of one-third corresponds to an angle of 1 degrees.
@end smallexample

Place the cursor on the @samp{1}, then type @kbd{C-x * w} to enable
Embedded mode on that number.  Now type @kbd{3 /} (to get one-third),
and @kbd{I T} (the Inverse Tangent converts a slope into an angle),
then @w{@kbd{C-x * w}} again to exit Embedded mode.

@smallexample
A slope of one-third corresponds to an angle of 18.4349488229 degrees.
@end smallexample

@c [fix-ref Embedded Mode]
@xref{Embedded Mode}, for full details.

@node Other C-x * Commands,  , Embedded Mode Overview, Using Calc
@subsection Other @kbd{C-x *} Commands

@noindent
Two more Calc-related commands are @kbd{C-x * g} and @kbd{C-x * 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 @kbd{C-x * 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 @kbd{v u} if you want
to unpack this vector into separate numbers on the stack.  Also,
@kbd{C-u C-x * g} interprets the region as a single number or
formula.

The @kbd{C-x * 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 @kbd{C-x * y}, which ``yanks'' the
value at the top of the Calc stack back into an editing buffer.
If you type @w{@kbd{C-x * y}} while in such a buffer, the value is
yanked at the current position.  If you type @kbd{C-x * 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 @kbd{C-x *} commands.
The shift, control, and meta keys are ignored for the keystroke
following @kbd{C-x *}.

@noindent
Commands for turning Calc on and off:

@table @kbd
@item *
Turn Calc on or off, employing the same user interface as last time.

@item =, +, -, /, \, &, #
Alternatives for @kbd{*}.

@item C
Turn Calc on or off using its standard bottom-of-the-screen
interface.  If Calc is already turned on but the cursor is not
in the Calc window, move the cursor into the window.

@item O
Same as @kbd{C}, but don't select the new Calc window.  If
Calc is already turned on and the cursor is in the Calc window,
move it out of that window.

@item B
Control whether @kbd{C-x * c} and @kbd{C-x * k} use the full screen.

@item Q
Use Quick mode for a single short calculation.

@item K
Turn Calc Keypad mode on or off.

@item E
Turn Calc Embedded mode on or off at the current formula.

@item J
Turn Calc Embedded mode on or off, select the interesting part.

@item W
Turn Calc Embedded mode on or off at the current word (number).

@item Z
Turn Calc on in a user-defined way, as defined by a @kbd{Z I} command.

@item X
Quit Calc; turn off standard, Keypad, or Embedded mode if on.
(This is like @kbd{q} or @key{OFF} inside of Calc.)
@end table
@iftex
@sp 2
@end iftex

@noindent
Commands for moving data into and out of the Calculator:

@table @kbd
@item G
Grab the region into the Calculator as a vector.

@item R
Grab the rectangular region into the Calculator as a matrix.

@item :
Grab the rectangular region and compute the sums of its columns.

@item _
Grab the rectangular region and compute the sums of its rows.

@item Y
Yank a value from the Calculator into the current editing buffer.
@end table
@iftex
@sp 2
@end iftex

@noindent
Commands for use with Embedded mode:

@table @kbd
@item A
``Activate'' the current buffer.  Locate all formulas that
contain @samp{:=} or @samp{=>} symbols and record their locations
so that they can be updated automatically as variables are changed.

@item D
Duplicate the current formula immediately below and select
the duplicate.

@item F
Insert a new formula at the current point.

@item N
Move the cursor to the next active formula in the buffer.

@item P
Move the cursor to the previous active formula in the buffer.

@item U
Update (i.e., as if by the @kbd{=} key) the formula at the current point.

@item `
Edit (as if by @code{calc-edit}) the formula at the current point.
@end table
@iftex
@sp 2
@end iftex

@noindent
Miscellaneous commands:

@table @kbd
@item I
Run the Emacs Info system to read the Calc manual.
(This is the same as @kbd{h i} inside of Calc.)

@item T
Run the Emacs Info system to read the Calc Tutorial.

@item S
Run the Emacs Info system to read the Calc Summary.

@item L
Load Calc entirely into memory.  (Normally the various parts
are loaded only as they are needed.)

@item M
Read a region of written keystroke names (like @kbd{C-n a b c @key{RET}})
and record them as the current keyboard macro.

@item 0
(This is the ``zero'' digit key.)  Reset the Calculator to
its initial state:  Empty stack, and initial mode settings.
@end table

@node History and Acknowledgements,  , Using Calc, Getting Started
@section History and Acknowledgements

@noindent
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 
@texline @math{2^{32}}.
@infoline @expr{2^32}.  
I didn't offhand, but I said, ``that's easy, just call up an
@code{xcalc}.''  @code{Xcalc} duly reported that the answer to our
question was @samp{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 didn't have built-in floating point math (now it does), so
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
this had to be simulated in software.  In fact, Emacs integers would
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.
Glenn Morris's avatar
Glenn Morris committed
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223

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 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; 
@texline Fran\c{c}ois
@infoline 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;
1224
Randal Schwartz, who suggested the @code{calc-eval} function; Juha
Glenn Morris's avatar
Glenn Morris committed
1225
Sarlin, who first worked out how to split Calc into quickly-loading
1226 1227 1228
parts; Bob Weiner, who helped immensely with the Lucid Emacs port; and
Robert J. Chassell, who suggested the Calc Tutorial and exercises as
well as many other things.  
Glenn Morris's avatar
Glenn Morris committed
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276

@cindex Bibliography
@cindex Knuth, Art of Computer Programming
@cindex Numerical Recipes
@c Should these be expanded into more complete references?
Among the books used in the development of Calc were Knuth's @emph{Art
of Computer Programming} (especially volume II, @emph{Seminumerical
Algorithms}); @emph{Numerical Recipes} by Press, Flannery, Teukolsky,
and Vetterling; Bevington's @emph{Data Reduction and Error Analysis
for the Physical Sciences}; @emph{Concrete Mathematics} by Graham,
Knuth, and Patashnik; Steele's @emph{Common Lisp, the Language}; the
@emph{CRC Standard Math Tables} (William H. Beyer, ed.); and
Abramowitz and Stegun's venerable @emph{Handbook of Mathematical
Functions}.  Also, of course, Calc could not have been written without
the excellent @emph{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.

@c [tutorial]

@ifinfo
@c This node is accessed by the `C-x * t' command.
@node Interactive Tutorial, Tutorial, Getting Started, Top
@chapter Tutorial

@noindent
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 @kbd{n} or @kbd{p} to go to the Next or Previous section.
If the section has a @dfn{menu}, press a digit key like @kbd{1}
or @kbd{2} to go to a sub-section from the menu.  Press @kbd{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 @kbd{f}, then
the exercise number, to see the answer to an exercise.  After
you have followed a cross-reference, you can press the letter
@kbd{l} to return to where you were before.

You can press @kbd{?} at any time for a brief summary of Info commands.

1277
Press the number @kbd{1} now to enter the first section of the Tutorial.
Glenn Morris's avatar
Glenn Morris committed
1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306

@menu
* Tutorial::
@end menu

@node Tutorial, Introduction, Interactive Tutorial, Top
@end ifinfo
@ifnotinfo
@node Tutorial, Introduction, Getting Started, Top
@end ifnotinfo
@chapter Tutorial

@noindent
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 (@pxref{Starting Calc}).
If you are already familiar with advanced calculators, you may wish
@c [not-split]
to skip on to the rest of this manual.
@c [when-split]
@c to skip on to volume II of this manual, the @dfn{Calc Reference}.

@c [fix-ref Embedded Mode]
This tutorial describes the standard user interface of Calc only.
The Quick mode and Keypad mode interfaces are fairly
self-explanatory.  @xref{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
1307 1308 1309 1310 1311 1312 1313
your Emacs screen, one with Calc and one with the Info system.  Press
@kbd{C-x * t} to set this up; the on-line tutorial will be opened in the
current window and Calc will be started in another window.  From the
Info window, the command @kbd{C-x * c} can be used to switch to the Calc
window and @kbd{C-x * o} can be used to switch back to the Info window.
(If you have a printed copy of the manual you can use that instead; in
that case you only need to press @kbd{C-x * c} to start Calc.)
Glenn Morris's avatar
Glenn Morris committed
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760

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.

@ifnottex
You may wish to print out a copy of the Calc Summary and keep notes on
it as you learn Calc.  @xref{About This Manual}, to see how to make a
printed summary.  @xref{Summary}.
@end ifnottex
@iftex
The Calc Summary at the end of the reference manual includes some blank
space for your own use.  You may wish to keep notes there as you learn
Calc.
@end iftex

@menu
* Basic Tutorial::
* Arithmetic Tutorial::
* Vector/Matrix Tutorial::
* Types Tutorial::
* Algebra Tutorial::
* Programming Tutorial::

* Answers to Exercises::
@end menu

@node Basic Tutorial, Arithmetic Tutorial, Tutorial, Tutorial
@section Basic Tutorial

@noindent
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.
@end menu

@node RPN Tutorial, Algebraic Tutorial, Basic Tutorial, Basic Tutorial
@subsection RPN Calculations and the Stack

@cindex RPN notation
@ifnottex
@noindent
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.)
@end ifnottex
@tex
\noindent
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 \L ukasiewicz.)
@end tex

The central component of an RPN calculator is the @dfn{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.

@cindex Operators
@cindex Operands
In an operation like @expr{2+3}, the 2 and 3 are called the @dfn{operands}
and the @expr{+} is the @dfn{operator}.  In an RPN calculator you always
enter the operands first, then the operator.  Each time you type a
number, Calc adds or @dfn{pushes} it onto the top of the Stack.
When you press an operator key like @kbd{+}, Calc @dfn{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:
@kbd{2 @key{RET} 3 @key{RET} +}.  (The @key{RET} key, Return, corresponds to
the @key{ENTER} key on traditional RPN calculators.)  Try this now if
you wish; type @kbd{C-x * c} to switch into the Calc window (you can type
@kbd{C-x * c} again or @kbd{C-x * o} to switch back to the Tutorial window).
The first four keystrokes ``push'' the numbers 2 and 3 onto the stack.
The @kbd{+} 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:

@smallexample
@group
    .          1:  2          2:  2          1:  5              .
                   .          1:  3              .
                                  .

  C-x * c          2 @key{RET}          3 @key{RET}            +             @key{DEL}
@end group
@end smallexample

The @samp{.} 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.

@cindex Stack levels
@cindex Levels of stack
The numbers @samp{1:} and @samp{2:} on the left are @dfn{stack level
numbers}.  Old RPN calculators always had four stack levels called
@expr{x}, @expr{y}, @expr{z}, and @expr{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 @kbd{+} always
work on the top few levels of the stack.

@c [fix-ref Truncating 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 @samp{.} 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 @samp{.} marker
upwards through the stack, temporarily ``hiding'' some numbers from
commands like @kbd{+}.  This is called @dfn{stack truncation} and
we will not cover it in this tutorial; @pxref{Truncating the Stack},
if you are interested.

You don't really need the second @key{RET} in @kbd{2 @key{RET} 3
@key{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 @kbd{2 @key{RET} 3 +} will work just as well.

Examples in this tutorial will often omit @key{RET} even when the
stack displays shown would only happen if you did press @key{RET}:

@smallexample
@group
1:  2          2:  2          1:  5
    .          1:  3              .
                   .

  2 @key{RET}            3              +
@end group
@end smallexample

@noindent
Here, after pressing @kbd{3} the stack would really show @samp{1:  2}
with @samp{Calc:@: 3} in the minibuffer.  In these situations, you can
press the optional @key{RET} to see the stack as the figure shows.

(@bullet{}) @strong{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 @kbd{f} and the
exercise number to go to the answer, then the letter @kbd{l} to
return to where you were.)

@noindent
Here's the first exercise:  What will the keystrokes @kbd{1 @key{RET} 2
@key{RET} 3 @key{RET} 4 + * -} compute?  (@samp{*} is the symbol for
multiplication.)  Figure it out by hand, then try it with Calc to see
if you're right.  @xref{RPN Answer 1, 1}. (@bullet{})

(@bullet{}) @strong{Exercise 2.}  Compute 
@texline @math{(2\times4) + (7\times9.4) + {5\over4}}
@infoline @expr{2*4 + 7*9.5 + 5/4} 
using the stack.  @xref{RPN Answer 2, 2}. (@bullet{})

The @key{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 @key{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 @key{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 @key{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 @kbd{-} key is also an operator (it subtracts the top two
stack elements), how does one enter a negative number?  Calc uses
the @kbd{_} (underscore) key to act like the minus sign in a number.
So, typing @kbd{-5 @key{RET}} won't work because the @kbd{-} key
will try to do a subtraction, but @kbd{_5 @key{RET}} works just fine.

You can also press @kbd{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:  @kbd{5 n @key{RET}}.

@cindex Duplicating a stack entry
If you press @key{RET} when you're not entering a number, the effect
is to duplicate the top number on the stack.  Consider this calculation:

@smallexample
@group
1:  3          2:  3          1:  9          2:  9          1:  81
    .          1:  3              .          1:  9              .
                   .                             .

  3 @key{RET}           @key{RET}             *             @key{RET}             *
@end group
@end smallexample

@noindent
(Of course, an easier way to do this would be @kbd{3 @key{RET} 4 ^},
to raise 3 to the fourth power.)

The space-bar key (denoted @key{SPC} here) performs the same function
as @key{RET}; you could replace all three occurrences of @key{RET} in
the above example with @key{SPC} and the effect would be the same.

@cindex Exchanging stack entries
Another stack manipulation key is @key{TAB}.  This exchanges the top
two stack entries.  Suppose you have computed @kbd{2 @key{RET} 3 +}
to get 5, and then you realize what you really wanted to compute
was @expr{20 / (2+3)}.

@smallexample
@group
1:  5          2:  5          2:  20         1:  4
    .          1:  20         1:  5              .
                   .              .

 2 @key{RET} 3 +         20            @key{TAB}             /
@end group
@end smallexample

@noindent
Planning ahead, the calculation would have gone like this:

@smallexample
@group
1:  20         2:  20         3:  20         2:  20         1:  4
    .          1:  2          2:  2          1:  5              .
                   .          1:  3              .
                                  .

  20 @key{RET}         2 @key{RET}            3              +              /
@end group
@end smallexample

A related stack command is @kbd{M-@key{TAB}} (hold @key{META} and type
@key{TAB}).  It rotates the top three elements of the stack upward,
bringing the object in level 3 to the top.

@smallexample
@group
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 @key{RET}         20 @key{RET}         30 @key{RET}         M-@key{TAB}          M-@key{TAB}
@end group
@end smallexample

(@bullet{}) @strong{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.  @xref{RPN Answer 3, 3}. (@bullet{})

Operations like @kbd{+}, @kbd{-}, @kbd{*}, @kbd{/}, and @kbd{^} pop two
arguments from the stack and push a result.  Operations like @kbd{n} and
@kbd{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.

@smallexample
@group
1:  3          1:  9          2:  9          1:  25         1:  5
    .              .          1:  16             .              .
                                  .

  3 @key{RET}          @key{RET} *        4 @key{RET} @key{RET} *        +              Q
@end group
@end smallexample

@noindent
(Note that capital @kbd{Q} means to hold down the Shift key while
typing @kbd{q}.  Remember, plain unshifted @kbd{q} is the Quit command.)

@cindex Pythagorean Theorem
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
@kbd{f h}, but let's suppose we can't remember the necessary keystrokes.
We can still enter it by its full name using @kbd{M-x} notation:

@smallexample
@group
1:  3          2:  3          1:  5
    .          1:  4              .
                   .

  3 @key{RET}          4 @key{RET}      M-x calc-hypot
@end group
@end smallexample

All Calculator commands begin with the word @samp{calc-}.  Since it
gets tiring to type this, Calc provides an @kbd{x} key which is just
like the regular Emacs @kbd{M-x} key except that it types the @samp{calc-}
prefix for you:

@smallexample
@group
1:  3          2:  3          1:  5
    .          1:  4              .
                   .

  3 @key{RET}          4 @key{RET}         x hypot
@end group
@end smallexample

What happens if you take the square root of a negative number?

@smallexample
@group
1:  4          1:  -4         1:  (0, 2)
    .              .              .

  4 @key{RET}            n              Q
@end group
@end smallexample

@noindent
The notation @expr{(a, b)} represents a complex number.
Complex numbers are more traditionally written @expr{a + b i};
Calc can display in this format, too, but for now we'll stick to the
@expr{(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 @kbd{(} and
@kbd{,} and @kbd{)} keys manipulate ``incomplete complex numbers.''

@smallexample
@group
1:  ( ...      2:  ( ...      1:  (2, ...    1:  (2, ...    1:  (2, 3)
    .          1:  2              .              3              .
                   .                             .

    (              2              ,              3              )
@end group
@end smallexample

You can perform calculations while entering parts of incomplete objects.
However, an incomplete object cannot actually participate in a calculation:

@smallexample
@group
1:  ( ...      2:  ( ...      3:  ( ...      1:  ( ...      1:  ( ...
    .          1:  2          2:  2              5              5
                   .          1:  3              .              .
                                  .
                                                             (error)
    (             2 @key{RET}           3              +              +
@end group
@end smallexample

@noindent
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.

@smallexample
@group
2:  2          3:  2          3:  3          1:  ( ...      1:  (2, 3)
1:  3          2:  3          2:  ( ...          2              .
    .          1:  ( ...      1:  2              3
                   .              .              .

2 @key{RET} 3 @key{RET}        (            M-@key{TAB}          M-@key{TAB}            )
@end group
@end smallexample

@noindent
Note that the @kbd{,} (comma) key did not have to be used here.
When you press @kbd{)} 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.

(@bullet{}) @strong{Exercise 4.}  To enter the complex number @expr{(2, 3)},
your friend Joe typed @kbd{( 2 , @key{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.)
@xref{RPN Answer 4, 4}. (@bullet{})

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 @dfn{numeric prefix argument} by
typing a series of @key{META}-digits beforehand.  If @key{META} is
awkward for you, you can instead type @kbd{C-u} followed by the
necessary digits.  Numeric prefix arguments can be negative, as in
@kbd{M-- M-3 M-5} or @w{@kbd{C-u - 3 5}}.  Calc commands use numeric
prefix arguments in a variety of ways.  For example, a numeric prefix
on the @kbd{+} operator adds any number of stack entries at once:

@smallexample
@group
1:  10         2:  10         3:  10         3:  10         1:  60
    .          1:  20         2:  20         2:  20             .
                   .          1:  30         1:  30
                                  .              .

  10 @key{RET}         20 @key{RET}         30 @key{RET}         C-u 3            +
@end group
@end smallexample

For stack manipulation commands like @key{RET}, a positive numeric
prefix argument operates on the top @var{n} stack entries at once.  A
negative argument operates on the entry in level @var{n} only.  An
argument of zero operates on the entire stack.  In this example, we copy
the second-to-top element of the stack:

@smallexample
@group
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 @key{RET}         20 @key{RET}         30 @key{RET}         C-u -2          @key{RET}
@end group
@end smallexample

@cindex Clearing the stack
@cindex Emptying the stack
Another common idiom is @kbd{M-0 @key{DEL}}, which clears the stack.
(The @kbd{M-0} numeric prefix tells @key{DEL} to operate on the
entire stack.)

@node Algebraic Tutorial, Undo Tutorial, RPN Tutorial, Basic Tutorial
@subsection Algebraic-Style Calculations

@noindent
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 @expr{2+3} notation.

1761 1762 1763
@strong{Notice:} Calc gives @samp{/} lower precedence than @samp{*}, so
that @samp{a/b*c} is interpreted as @samp{a/(b*c)}; this is not
standard across all computer languages.  See below for details.
Glenn Morris's avatar
Glenn Morris committed
1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202

You don't really need any special ``mode'' to enter algebraic formulas.
You can enter a formula at any time by pressing the apostrophe (@kbd{'})
key.  Answer the prompt with the desired formula, then press @key{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
@key{DEL} to delete it from the stack.

Try pressing the apostrophe key, then @kbd{2+3+4}, then @key{RET}.
The result should be the number 9.

Algebraic formulas use the operators @samp{+}, @samp{-}, @samp{*},
@samp{/}, and @samp{^}.  You can use parentheses to make the order
of evaluation clear.  In the absence of parentheses, @samp{^} is
evaluated first, then @samp{*}, then @samp{/}, then finally
@samp{+} and @samp{-}.  For example, the expression

@example
2 + 3*4*5 / 6*7^8 - 9
@end example

@noindent
is equivalent to

@example
2 + ((3*4*5) / (6*(7^8)) - 9
@end example

@noindent
or, in large mathematical notation,

@ifnottex
@example
@group
    3 * 4 * 5
2 + --------- - 9
          8
     6 * 7
@end group
@end example
@end ifnottex
@tex
\turnoffactive
\beforedisplay
$$ 2 + { 3 \times 4 \times 5 \over 6 \times 7^8 } - 9 $$
\afterdisplay
@end tex

@noindent
The result of this expression will be the number @mathit{-6.99999826533}.

Calc's order of evaluation is the same as for most computer languages,
except that @samp{*} binds more strongly than @samp{/}, as the above
example shows.  As in normal mathematical notation, the @samp{*} symbol
can often be omitted:  @samp{2 a} is the same as @samp{2*a}.

Operators at the same level are evaluated from left to right, except
that @samp{^} is evaluated from right to left.  Thus, @samp{2-3-4} is
equivalent to @samp{(2-3)-4} or @mathit{-5}, whereas @samp{2^3^4} is equivalent
to @samp{2^(3^4)} (a very large integer; try it!).

If you tire of typing the apostrophe all the time, there is
Algebraic mode, where Calc automatically senses
when you are about to type an algebraic expression.  To enter this
mode, press the two letters @w{@kbd{m a}}.  (An @samp{Alg} indicator
should appear in the Calc window's mode line.)

Press @kbd{m a}, then @kbd{2+3+4} with no apostrophe, then @key{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 @kbd{_}
key), or if you press @kbd{(} or @kbd{[}, Calc automatically begins
an algebraic entry.

Functions which do not have operator symbols like @samp{+} and @samp{*}
must be entered in formulas using function-call notation.  For example,
the function name corresponding to the square-root key @kbd{Q} is
@code{sqrt}.  To compute a square root in a formula, you would use
the notation @samp{sqrt(@var{x})}.

Press the apostrophe, then type @kbd{sqrt(5*2) - 3}.  The result should
be @expr{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 @kbd{arcsin}
out of the blue, the @kbd{a r} will be taken as an Algebraic Rewrite
command, and the @kbd{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:

@smallexample
@group
1:  (2, 3)     2:  (2, 3)     1:  (8, -1)    2:  (8, -1)    1:  (9, -1)
    .          1:  (1, -2)        .          1:  1              .
                   .                             .

 (2,3) @key{RET}      (1,-2) @key{RET}        *              1 @key{RET}          +
@end group
@end smallexample

Algebraic mode allows us to enter complex numbers without pressing
an apostrophe first, but it also means we need to press @key{RET}
after every entry, even for a simple number like @expr{1}.

(You can type @kbd{C-u m a} to enable a special Incomplete Algebraic
mode in which the @kbd{(} and @kbd{[} keys use algebraic entry even
though regular numeric keys still use RPN numeric entry.  There is also
Total Algebraic mode, started by typing @kbd{m t}, in which all
normal keys begin algebraic entry.  You must then use the @key{META} key
to type Calc commands:  @kbd{M-m t} to get back out of Total Algebraic
mode, @kbd{M-q} to quit, etc.)

If you're still in Algebraic mode, press @kbd{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 @kbd{+} and @kbd{*})
use algebraic form, but operators of one number (like @kbd{n} and @kbd{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 @kbd{$} sign to tie them together.
In an algebraic formula, @kbd{$} represents the number on the top
of the stack.  Here, we perform the calculation 
@texline @math{\sqrt{2\times4+1}},
@infoline @expr{sqrt(2*4+1)},
which on a traditional calculator would be done by pressing
@kbd{2 * 4 + 1 =} and then the square-root key.

@smallexample
@group
1:  8          1:  9          1:  3
    .              .              .

  ' 2*4 @key{RET}        $+1 @key{RET}        Q
@end group
@end smallexample

@noindent
Notice that we didn't need to press an apostrophe for the @kbd{$+1},
because the dollar sign always begins an algebraic entry.

(@bullet{}) @strong{Exercise 1.}  How could you get the same effect as
pressing @kbd{Q} but using an algebraic entry instead?  How about
if the @kbd{Q} key on your keyboard were broken?
@xref{Algebraic Answer 1, 1}. (@bullet{})

The notations @kbd{$$}, @kbd{$$$}, and so on stand for higher stack
entries.  For example, @kbd{' $$+$ @key{RET}} is just like typing @kbd{+}.

Algebraic formulas can include @dfn{variables}.  To store in a
variable, press @kbd{s s}, then type the variable name, then press
@key{RET}.  (There are actually two flavors of store command:
@kbd{s s} stores a number in a variable but also leaves the number
on the stack, while @w{@kbd{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.

@smallexample
@group
1:  17             .          1:  a + a^2    1:  306
    .                             .              .

    17          s t a @key{RET}      ' a+a^2 @key{RET}       =
@end group
@end smallexample

@noindent
The @kbd{=} key @dfn{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 @kbd{=},
or by using the @kbd{s r} command.

@smallexample
@group
1:  17         2:  17         3:  17         2:  17         1:  306
    .          1:  17         2:  17         1:  289            .
                   .          1:  2              .
                                  .

  s r a @key{RET}     ' a @key{RET} =         2              ^              +
@end group
@end smallexample

If you press a single digit for a variable name (as in @kbd{s t 3}, you
get one of ten @dfn{quick variables} @code{q0} through @code{q9}.
They are ``quick'' simply because you don't have to type the letter
@code{q} or the @key{RET} after their names.  In fact, you can type
simply @kbd{s 3} as a shorthand for @kbd{s s 3}, and likewise for
@kbd{t 3} and @w{@kbd{r 3}}.

Any variables in an algebraic formula for which you have not stored
values are left alone, even when you evaluate the formula.

@smallexample
@group
1:  2 a + 2 b     1:  34 + 2 b
    .                 .

 ' 2a+2b @key{RET}          =
@end group
@end smallexample

Calls to function names which are undefined in Calc are also left
alone, as are calls for which the value is undefined.

@smallexample
@group
1:  2 + log10(0) + log10(x) + log10(5, 6) + foo(3)
    .

 ' log10(100) + log10(0) + log10(x) + log10(5,6) + foo(3) @key{RET}
@end group
@end smallexample

@noindent
In this example, the first call to @code{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 @code{foo}.  You will see a
``Wrong number of arguments'' message referring to @samp{log10(5,6)}.
Press the @kbd{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: @code{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: @code{x}''
will only show up if you explicitly press the @kbd{w} key.

(@bullet{}) @strong{Exercise 2.}  Joe entered the formula @samp{2 x y},
stored 5 in @code{x}, pressed @kbd{=}, and got the expected result,
@samp{10 y}.  He then tried the same for the formula @samp{2 x (1+y)},
expecting @samp{10 (1+y)}, but it didn't work.  Why not?
@xref{Algebraic Answer 2, 2}. (@bullet{})

(@bullet{}) @strong{Exercise 3.}  What result would you expect
@kbd{1 @key{RET} 0 /} to give?  What if you then type @kbd{0 *}?
@xref{Algebraic Answer 3, 3}. (@bullet{})

One interesting way to work with variables is to use the
@dfn{evaluates-to} (@samp{=>}) operator.  It works like this:
Enter a formula algebraically in the usual way, but follow
the formula with an @samp{=>} symbol.  (There is also an @kbd{s =}
command which builds an @samp{=>} formula using the stack.)  On
the stack, you will see two copies of the formula with an @samp{=>}
between them.  The lefthand formula is exactly like you typed it;
the righthand formula has been evaluated as if by typing @kbd{=}.

@smallexample
@group
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 => @key{RET}  ' 2a+2b @key{RET} s =          10 s t a @key{RET}
@end group
@end smallexample

@noindent
Notice that the instant we stored a new value in @code{a}, all
@samp{=>} operators already on the stack that referred to @expr{a}
were updated to use the new value.  With @samp{=>}, 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:

@smallexample
@group
2:  2 + 5 => 5
1:  2 a + 2 b => 2 a + 2 b
    .

    s u a @key{RET}
@end group
@end smallexample

We will encounter formulas involving variables and functions again
when we discuss the algebra and calculus features of the Calculator.

@node Undo Tutorial, Modes Tutorial, Algebraic Tutorial, Basic Tutorial
@subsection Undo and Redo

@noindent
If you make a mistake, you can usually correct it by pressing shift-@kbd{U},
the ``undo'' command.  First, clear the stack (@kbd{M-0 @key{DEL}}) and exit
and restart Calc (@kbd{C-x * * C-x * *}) to make sure things start off
with a clean slate.  Now:

@smallexample
@group
1:  2          2:  2          1:  8          2:  2          1:  6
    .          1:  3              .          1:  3              .
                   .                             .

   2 @key{RET}           3              ^              U              *
@end group
@end smallexample

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:

@smallexample
@group
1:  6          2:  2          1:  2              .              .
    .          1:  3              .
                   .
                                                             (error)
                   U              U              U              U
@end group
@end smallexample

You can also type @kbd{D} to ``redo'' a command that you have undone
mistakenly.

@smallexample
@group
    .          1:  2          2:  2          1:  6          1:  6
                   .          1:  3              .              .
                                  .
                                                             (error)
                   D              D              D              D
@end group
@end smallexample

@noindent
It was not possible to redo past the @expr{6}, since that was placed there
by something other than an undo command.

@cindex Time travel
You can think of undo and redo as a sort of ``time machine.''  Press
@kbd{U} to go backward in time, @kbd{D} to go forward.  If you go
backward and do something (like @kbd{*}) 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 @expr{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 @kbd{t d} now to turn it on.

Let's try grabbing an earlier result.  The @expr{8} we computed was
undone by a @kbd{U} command, and was lost even to Redo when we pressed
@kbd{*}, but it's still there in the trail.  There should be a little
@samp{>} arrow (the @dfn{trail pointer}) resting on the last trail
entry.  If there isn't, press @kbd{t ]} to reset the trail pointer.
Now, press @w{@kbd{t p}} to move the arrow onto the line containing
@expr{8}, and press @w{@kbd{t y}} to ``yank'' that number back onto the
stack.

If you press @kbd{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 @samp{2^3^4}.  We don't remember
what it was, but the first digits were ``241''.  Press @kbd{t r}
(which stands for trail-search-reverse), then type @kbd{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 @kbd{C-s} or @kbd{C-r} to
continue the search forwards or backwards as you like.

To finish the search, press @key{RET}.  This halts the incremental
search and leaves the trail pointer at the thing we found.  Now we
can type @kbd{t y} to yank that number onto the stack.  If we hadn't
remembered the ``241'', we could simply have searched for @kbd{2^3^4},
then pressed @kbd{@key{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 @kbd{t}.  (The store-and-recall commands, on the other hand,
all began with @kbd{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 @dfn{prefix}
key.  If you type a prefix key by accident, you can press @kbd{C-g}
to cancel it.  (In fact, you can press @kbd{C-g} to cancel almost
anything in Emacs.)  To get help on a prefix key, press that key
followed by @kbd{?}.  Some prefixes have several lines of help,
so you need to press @kbd{?} repeatedly to see them all.  
You can also type @kbd{h h} to see all the help at once.

Try pressing @kbd{t ?} now.  You will see a line of the form,

@smallexample
trail/time: Display; Fwd, Back; Next, Prev, Here, [, ]; Yank:  [MORE]  t-
@end smallexample

@noindent
The word ``trail'' indicates that the @kbd{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 @kbd{t n}, @kbd{t p}, @kbd{t ]}, and
@kbd{t y} so far.  The @samp{[MORE]} means you can press @kbd{?}
again to see more @kbd{t}-prefix commands.  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 @kbd{y} for example,
it will be interpreted as a @kbd{t y} command.  If all you wanted
was to look at the help messages, press @kbd{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 @kbd{`} (the backquote or accent grave)
to edit a stack entry.

Try entering @samp{3.141439} now.  If this is supposed to represent
@cpi{}, it's got several errors.  Press @kbd{`} 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 @key{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 @kbd{`} key also works
during entry of a number or algebraic formula.

@node Modes Tutorial,  , Undo Tutorial, Basic Tutorial
@subsection Mode-Setting Commands

@noindent
Calc has many types of @dfn{modes} that affect the way it interprets
your commands or the way it displays data.  We have already seen one
mode, namely Algebraic mode.  There are many others, too; we'll
try some of the most common ones here.

Perhaps the most fundamental mode in Calc is the current @dfn{precision}.
Notice the @samp{12} on the Calc window's mode line:

@smallexample
2203
--%*-Calc: 12 Deg       (Calculator)----All------
Glenn Morris's avatar
Glenn Morris committed
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443