[comp.sources.misc] v13i038: Emacs Calculator 1.01, part 12/19

daveg@csvax.caltech.edu (David Gillespie) (06/06/90)

Posting-number: Volume 13, Issue 38
Submitted-by: daveg@csvax.caltech.edu (David Gillespie)
Archive-name: gmcalc/part12

---- Cut Here and unpack ----
#!/bin/sh
# this is part 12 of a multipart archive
# do not concatenate these parts, unpack them in order with /bin/sh
# file calc.texinfo continued
#
CurArch=12
if test ! -r s2_seq_.tmp
then echo "Please unpack part 1 first!"
     exit 1; fi
( read Scheck
  if test "$Scheck" != $CurArch
  then echo "Please unpack part $Scheck next!"
       exit 1;
  else exit 0; fi
) < s2_seq_.tmp || exit 1
echo "x - Continuing file calc.texinfo"
sed 's/^X//' << 'SHAR_EOF' >> calc.texinfo
Xthat have been done.  The Calc Trail buffer uses a variant of Calc Mode,
Xso Calculator commands still work when the trail buffer's window is selected.
XIt is possible to turn the trail window off, but the @samp{*Calc Trail*}
Xbuffer still exists and is updated silently.
X@xref{Trail Commands}.@refill
X
X@kindex M-#
XIn most installations, the @kbd{M-#} (Meta-Shift-3) keystroke is a more
Xconvenient way to start the Calculator.  If you don't have a Meta key,
Xpress @key{ESC}, then @kbd{#}.
X
X@kindex x
X@kindex M-x
X@pindex calc-execute-extended-command
XMost Calc commands use one or two keystrokes.  Lower- and upper-case
Xletters are distinct.  Commands may also be entered in @kbd{M-x} form;
Xfor some commands this is the only form.  As a convenience, the @kbd{x}
Xkey (@code{calc-execute-extended-command})
Xis like @kbd{M-x} except that it enters the initial string @samp{calc-}
Xfor you.  For example, the following key sequences are equivalent:
X@kbd{S}, @kbd{M-x calc-sin @key{RET}}, @kbd{x sin @key{RET}}.@refill
X
X@cindex Extensions module
X@cindex calc-ext module
XThe Calculator exists in two parts.  When you type @kbd{M-x calc}, the
XEmacs ``auto-load'' mechanism will bring in only the first part, which
Xcontains the basic arithmetic functions.  The second half will be
Xauto-loaded the first time you use a more advanced command like a trig
Xfunction or matrix operation.  This is done to improve the response time
Xof the Calculator in the common case when all you need to do is a
Xlittle arithmetic.  If for some reason the Calculator fails to load the
Xextensions module automatically, you can force it to load the extensions
Xby pressing @kbd{x}, or by using the @kbd{m x} command.
X@xref{Mode Settings}.@refill
X
XIf you type @kbd{M-x calc} or @kbd{M-#} with any numeric prefix argument,
Xthe Calculator is loaded if necessary, but it is not actually started.
XIf the argument is positive, the extensions are also loaded if necessary.
XUser-written Lisp code that wishes to make use of Calc's arithmetic
Xroutines can use @samp{(calc 0)} or @samp{(calc 1)} to auto-load the
XCalculator.  (Also, @pxref{Grabbing From Buffers}.)@refill
X
X@kindex ?
X@kindex h
X@kindex i
X@pindex calc-help
X@pindex calc-info
X@cindex Help commands
XThe @kbd{?} key (@code{calc-help}) displays a series of brief help messages.
XSome keys (such as @kbd{b} and @kbd{d}) are prefix keys, like Emacs'
X@key{ESC} and @kbd{C-x} prefixes.  You can type
X@kbd{?} after a prefix to see a list of commands beginning with that
Xprefix.  (If the message includes @samp{[MORE]}, press @kbd{?} again
Xto see additional commands for that prefix.)  Also, @kbd{h}
X(@code{describe-mode}) calls up more complete help
Xinformation on the Calculator in another window, and
X@kbd{i} (@code{calc-info}) runs the Emacs Info system to read this
Xmanual on-line.@refill
X
X@kindex q
X@pindex calc-quit
X@cindex Quitting the Calculator
X@cindex Exiting the Calculator
XThe @kbd{q} key (@code{calc-quit}) exits Calc Mode and closes the
XCalculator's window(s).  It does not delete the Calculator buffers.
XIf you type @kbd{M-x calc} again, the Calculator will reappear with the
Xcontents of the stack intact.  Typing @kbd{M-#} or @kbd{M-x calc} from
Xinside the Calculator buffer is equivalent to executing @code{calc-quit};
Xyou can think of @kbd{M-#} as toggling the Calculator on and off.@refill
X
X@kindex d ~
X@pindex calc-refresh
X@kindex d `
X@pindex calc-realign
X@cindex Refreshing a garbled display
X@cindex Garbled displays, refreshing
XThe @kbd{d ~} key sequence (@code{calc-refresh}) redraws the contents of
Xthe Calculator buffer from memory.  Use this if the contents of the
Xbuffer have been damaged somehow.  The @kbd{o} key and @kbd{d `} key
X(@code{calc-realign}) move the cursor back to its ``home'' position at
Xthe bottom of the Calculator buffer.@refill
X
X@kindex <
X@pindex calc-scroll-left
X@kindex >
X@pindex calc-scroll-right
X@cindex Horizontal scrolling
X@cindex Scrolling horizontally
X@cindex Wide text, scrolling
XThe @kbd{<} and @kbd{>} keys are bound to @code{calc-scroll-left} and
X@code{calc-scroll-right}.  These are just like the normal horizontal
Xscrolling commands except that they scroll one half-screen at a time by
Xdefault.  (Calc formats its output to fit within the bounds of the
Xwindow whenever it can.)@refill
X
X@menu
X* Stack Basics::
X* Numeric Entry::
X* Algebraic Entry::
X* Quick Calculator::
X* Prefix Arguments::
X* Undo::
X* Error Messages::
X* Multiple Calculators::
X* Troubleshooting Commands::
X@end menu
X
X@comment
X@comment  --- Note that TeX order differs from Info order here! ---
X@node Quick Overview, Tutorial, Top, Top
X@section Quick Overview
X
X@cindex Quick overview
X@cindex Overview
XType @kbd{M-x calc} or @kbd{M-#} to start the Calculator; type @kbd{q} to quit.
XType @kbd{M-x quick-calc} to do one quick calculation in the minibuffer.
X
XCalc normally uses RPN notation.  @xref{Stack Basics}, for a discussion
Xof RPN for the beginner.  The @kbd{RET} or @kbd{SPC} key enters a number
Xor duplicates the top number on the stack, @kbd{DEL} removes the top
Xnumber from the stack, and @kbd{TAB} swaps the top two numbers.@refill
X
XYou can enter a formula in familiar ``algebraic'' form (as in @samp{1+2*3})
Xby pressing the apostrophe key.  If you find yourself always using the
Xapostrophe, press the two-key sequence @kbd{m a} so that the numeric,
X@kbd{(}, and @kbd{[} keys will automatically begin algebraic entry.  In
Xa formula, @samp{$} represents the previously computed result.@refill
X
XThe basic arithmetic operations are @kbd{+}, @kbd{-}, @kbd{*}, @kbd{/},
X@kbd{^} (raise to a power), @kbd{%} (modulo), @kbd{&} (reciprocal),
Xand @kbd{n} (change sign).@refill
X
XMathematical functions include @kbd{Q} (square root), @kbd{S} (sine),
X@kbd{C} (cosine), @kbd{T} (tangent), @kbd{E} (exponential), and @kbd{L}
X(natural logarithm).  The @kbd{I} (inverse) and @kbd{H} (hyperbolic)
Xprefix keys may be used with these commands.  These are all shifted
Xletter keys.@refill
X
XPress @kbd{U} to undo the effect of the previous Calculator command.
X
XThe Calculator can operate on integers, rational numbers (fractions), or
Xfloating-point numbers.  Dividing two integers produces a floating-point
Xapproximation; press @kbd{m f} to enable a mode where dividing integers
Xproduces fractions instead.@refill
X
XThe notation @samp{(a,b)} stands for the complex number @samp{a+bi}.
XThe notation @samp{(r;theta)} stands for a complex number in polar form.
XThe notation @samp{a+/-b} stands for @samp{a} with standard deviation @samp{b}.
XThe notation @samp{a mod M} stands for @samp{a} which is taken modulo @samp{M}.
XThe notation @samp{h@@ m' s"} stands for a value in hours, minutes, and seconds.
XThe notation @samp{[lo..hi)} stands for the interval @samp{lo <= x < hi}.
XThe notation @samp{[a,b,c]} stands for a vector of three values.
XThe notation @samp{[[a,b,c],[d,e,f]]} stands for a 2-by-3 matrix.
XMatrices can also be entered in the form @samp{[a,b,c;d,e,f]}.
X
XIntegers and fractions are computed to arbitrary size.  Floating-point
Xnumbers are computed to the current precision, which may be arbitrarily
Xlarge.  Press @kbd{p} to set the current precision.
X
XErroneous calculations such as dividing 1 by 0 simply leave an
Xunsimplified formula (like @samp{1 / 0}) on the stack.  If the
XCalculator leaves a formula unsimplified, press @kbd{w} to see why.
XPress @kbd{d w} to enable a mode where this happens automatically.
X
XPress @kbd{?} for a brief summary of Calculator commands.  Press @kbd{?}
Xrepeatedly to cycle among the lines of the summary.
X
XPress @kbd{d ?} for a list of commands for changing the display format.
XFor example, @kbd{d f} sets fixed-point notation.  (After you type @kbd{?}
Xthe Calculator is still waiting for a key to complete the @samp{d} sequence;
Xtype @kbd{C-g} to cancel the prefix.)
X
XPress @kbd{m ?} for a list of other mode-setting commands.  Press @kbd{m m}
Xto save all current mode settings in your @file{.emacs} file.
X
XPress @kbd{c ?} for a list of commands for numeric conversions, such as
Xdegrees-to-radians and back.
X
XPress @kbd{b ?} for a list of binary-arithmetic commands.  (Press @kbd{d 2}
Xor @kbd{d 0} to switch between binary and decimal display of integers.)
XPress @kbd{b w} to see or change the current word size for binary operations.
X
XPress @kbd{a ?} for a list of commands for manipulating algebraic formulas.
X
XPress @kbd{v ?} for a list of commands for manipulating vectors and
Xmatrices.
X
XPress @kbd{k ?} for a list of commands related to number theory and
Xcombinatorial analysis.
X
XPress @kbd{t ?} for a list of commands for manipulating the trail.
X
XPress @kbd{Z ?} for a list of commands for defining your own commands.
XPress @kbd{z ?} for a list of commands you have defined.
X
XPress @kbd{s} to store a number in a variable.  Variable names are single
Xdigits or whole words.  Press @kbd{r} to recall a value from a variable.
X
XPress @kbd{y} to copy the top number on the stack into the most recently
Xused editing buffer.  Use @kbd{M-x calc-grab-region} or @kbd{C-u - M-#}
Xin an editing buffer to transfer a region of data to the Calculator stack.
X
X@node Tutorial, Introduction, Quick Overview, Top
X@chapter Tutorial
X
XThis chapter explains how to use Calc and its many features, in
Xa step-by-step, tutorial way.  You are encouraged to run Calc and
Xwork along with the examples as you read.  If you are already
Xfamiliar with advanced calculators, you may wish to skip on to the
Xrest of this manual.
X
XThe easiest way to read the tutorial on-line is to have two windows on
Xyour Emacs screen, one with Calc and one with the Info system.  (If you
Xhave a printed copy of the manual you can use that instead.)  The
X@kbd{C-x o} command switches among the windows on the screen.  You may
Xfind it convenient to turn the Calc Trail display off so that there are
Xonly two windows on the screen.  Type the two-letter sequence @kbd{t d}
Xto turn the trail on and off.  (You may have to wait a few seconds
Xwhile Calc loads its ``extended commands'' package to do this.)
X
XThis tutorial is designed to be done in sequence.  But the rest of this
Xmanual does not assume you have gone through the tutorial.  The tutorial
Xdoes not cover everything in the Calculator, but it touches on most
Xgeneral areas.
X
X@menu
X* Basic Tutorial::
X* Arithmetic Tutorial::
X* Vector/Matrix Tutorial::
X* Types Tutorial::
X* Algebra Tutorial::
X* Programming Tutorial::
X@end menu
X
X@node Basic Tutorial, Arithmetic Tutorial, Tutorial, Tutorial
X@section Basic Tutorial
X
XIn this section, we learn how to turn the Calculator on and off, how
XRPN and algebraic-style calculations work, and how to undo and redo
Xan operation made by mistake.
X
X@menu
X* On and Off Tutorial::     Turning Calc on and off.
X* RPN Tutorial::            Basic operations with the stack.
X* Algebraic Tutorial::      Algebraic entry; variables.
X* Undo Tutorial::           If you make a mistake: Undo and the trail.
X* Modes Tutorial::          Common mode-setting commands.
X@end menu
X
X@node On and Off Tutorial, RPN Tutorial, Basic Tutorial, Basic Tutorial
X@subsection Turning the Calculator On and Off
X
XIn most installations, you can type @kbd{M-#} to start the Calculator.
XThe notation @kbd{M-#} is short for @kbd{@key{META}-#}.  On most
Xkeyboards this means holding down the @key{META} (or @key{ALT}) and
X@key{SHIFT} keys while typing @kbd{3}.  If you don't have a @key{META}
Xkey on your keyboard, you can type @key{ESC} first, then @kbd{#}, to
Xaccomplish the same effect.  If you don't even have an @key{ESC} key,
Xyou can hold down @key{CTRL} or @key{CONTROL} while typing a left square
Xbracket, denoted here by @kbd{C-[}.@refill
X
XIf @kbd{M-#} doesn't work for you, you can always type @kbd{M-x calc}.
XFirst type @kbd{M-x} (@key{META} with the letter @kbd{x}), then, at
Xthe prompt, type @kbd{calc} and press Return.  (In Emacs documentation,
Xthe return key is shown as @key{RET}, and is usually omitted for the
Xsake of brevity on commands like @kbd{M-x}.)
X
XIf you type @kbd{M-x calc} and Emacs doesn't recognize the command
X(it will say @samp{[No match]} when you try to press @key{RET}),
Xthen Calc has not been fully installed and you will have to load it
Xby hand.  Use @kbd{M-x load-file}, then type @samp{calc} when it
Xasks you for the file name to load.  Then, you can use @kbd{M-#} to
Xstart the Calculator.
X
XWhen you turn the Calculator on, your Emacs window should split into
Xtwo, the upper one showing whatever you were editing at the time,
Xand the lower one displaying the Calculator.  The lower part is
Xactually two windows side-by-side.  The lefthand one is called
Xthe @dfn{stack window} and the righthand one is called the
X@dfn{trail window.}  The Stack holds the numbers involved in the
Xcalculation you are currently performing.  The Trail holds a complete
Xrecord of all calculations you have done.  In a desk calculator with
Xa printer, the Trail corresponds to the paper tape that records what
Xyou do.
X
XMost Calculator commands deal explicitly with the Stack only, but
Xthere is a set of commands that allow you to search back through
Xthe Trail and retreive any previous result.
X
XTo turn the Calculator off, just press @kbd{M-#} again.  You can
Xalso press @kbd{q} for ``quit'' with the same effect.  When you
Xquit, the Calculator windows go away but the actual Stack and Trail
Xare not gone, just hidden.  When you press @kbd{M-#} once again
Xyou will get the same Stack and Trail contents you had when you
Xlast used the Calculator.
X
XThe Calculator does not remember its state between Emacs sessions.
XThus if you quit Emacs and start it again, @kbd{M-#} will give you
Xa fresh Stack and Trail.
X
X@node RPN Tutorial, Algebraic Tutorial, On and Off Tutorial, Basic Tutorial
X@subsection RPN Calculations and the Stack
X
X@cindex RPN notation
XCalc normally uses RPN notation.  You may be familiar with the RPN
Xsystem from HP calculators, FORTH, or PostScript.  The central component
Xof an RPN calculator is the @dfn{stack}.  A calculator stack is like a
Xstack of dishes.  New dishes (numbers) are added at the top of the
Xstack, and numbers are normally only removed from the top of the
Xstack.@refill
X
XIn an operation like @samp{2+3}, the 2 and 3 are called the @dfn{operands}
Xand the @samp{+} is the @dfn{operator}.  In an RPN calculator you always
Xenter the operands first, then the operator.  Each time you type a
Xnumber, Calc adds or @dfn{pushes} it onto the top of the Stack.
XWhen you press an operator key like @kbd{+}, Calc pops the appropriate
Xnumber of operands from the stack and pushes back the result.
X
XThus we could add the numbers 2 and 3 in an RPN calculator by typing the
Xkeys: @kbd{1 @key{RET} 2 @key{RET} +}.  (@key{RET} corresponds to the
X@key{ENTER} key on traditional RPN calculators.)  The first four
Xkeystrokes ``push'' the numbers 2 and 3 onto the stack.  The @kbd{+} key
X``pops'' the top two numbers from the stack, adds them, and pushes the
Xresult (5) back onto the stack.  Here's how the stack will look at
Xvarious points throughout the calculation:
X
X@group
X@example
X    .          1:  2          2:  2          1:  5              .
X                   .          1:  3              .
X                                  .
X
X   M-#           2 RET          3 RET            +             DEL
X@end example
X@end group
X
XThe @samp{.} symbol is a marker that represents the top of the stack.
XNote that the ``top'' of the stack is really shown at the bottom of
Xthe Stack window.  This may seem backwards, but it turns out to be
Xless distracting in regular use.
X
XThe numbers @samp{1:} and @samp{2:} on the left are @dfn{stack level
Xnumbers}.  Old RPN calculators always had four stack levels called
X@code{x}, @code{y}, @code{z}, and @code{t}.  Calc's stack can grow
Xas large as you like, so it uses numbers instead of letters.  Some
Xstack-manipulation commands accept a numeric argument which says
Xwhich stack level to work on.  Normal commands like @kbd{+} always
Xwork on the top few levels of the stack.
X
XThe Stack buffer is just an Emacs buffer, and you can move around in
Xit using the regular Emacs motion commands.  But no matter where the
Xcursor is, and even if you have scrolled the @samp{.} marker out of
Xview, Calc commands always move the cursor back down to level 1
Xbefore doing anything.  It is possible to move the @samp{.} marker
Xupwards through the stack, temporarily ``hiding'' some numbers from
Xcommands like @kbd{+}.  This is called @dfn{stack truncation} and
Xwe will not cover it in this tutorial; @pxref{Truncating the Stack},
Xif you are interested.
X
XYou don't really need the second @key{RET} in @kbd{2 @key{RET} 3
X@key{RET} +}.  That's because if you type any operator name or
Xother non-numeric key when you are entering a number, the Calculator
Xautomatically enters that number and then does the requested command.
XThus @kbd{2 @key{RET} 3 +} will work just as well.@refill
X
XThe @key{DEL} key is called Backspace on some keyboards.  It is
Xwhatever key you would use to correct a simple typing error when
Xregularly using Emacs.  The @key{DEL} key pops and throws away the
Xtop value on the stack.  (You can still get that value back from
Xthe Trail if you should need it later on.)
X
XSince the @kbd{-} key is also an operator (it subtracts the top two
Xstack elements), how does one enter a negative number?  Calc uses
Xthe @kbd{_} (underscore) key to act like the minus sign in a number.
XSo, typing @kbd{-5 @key{RET}} won't work because the @kbd{-} key
Xwill try to do a subtraction, but @kbd{_5 @key{RET}} works just fine.
X
XYou can also press @kbd{n}, which means ``change sign.''  It changes
Xthe number at the top of the stack (or the number being entered)
Xfrom positive to negative or vice-versa.
X
XIf you press @key{RET} when you're not entering a number, the effect
Xis to duplicate the top number on the stack.  Consider this calculation:
X
X@group
X@example
X1:  3          2:  3          1:  9          2:  9          1:  81
X    .          1:  3              .          1:  9              .
X                   .                             .          
X
X  3 RET           RET             *             RET             *
X@end example
X@end group
X
X(Of course, an easier way to do this would be @kbd{3 @key{RET} 4 ^},
Xto raise 3 to the fourth power.)
X
XThe space-bar key (denoted @key{SPC} here) performs the same function
Xas @key{RET}; you could replace all three occurrences of @key{RET} in
Xthe above example with @key{SPC} and the effect would be the same.
X
XAnother stack manipulation key is @key{TAB}.  This exchanges the top
Xtwo stack entries.  Suppose you have computed @kbd{2 @key{RET} 3 +}
Xto get 5, and then you realize what you really wanted to compute
Xwas @samp{20 / (2+3)}.
X
X@group
X@example
X1:  5          2:  5          2:  20         1:  4
X    .          1:  20         1:  5              .
X                   .              .
X
X 2 RET 3 +         20            TAB             /
X@end example
X@end group
X
XPlanning ahead, the calculation would have gone like this:
X
X@group
X@example
X1:  20         2:  20         3:  20         2:  20         1:  4
X    .          1:  2          2:  2          1:  5              .
X                   .          1:  3              .
X                                  .
X
X  20 RET         2 RET            3              +              /
X@end example
X@end group
X
XA related stack command is @kbd{M-@key{TAB}} (hold @key{META} and type
X@key{TAB}).  It rotates the top three elements of the stack upward,
Xbringing the object in level 3 to the top.
X
X@group
X@example
X1:  10         2:  10         3:  10         3:  20         3:  30
X    .          1:  20         2:  20         2:  30         2:  10
X                   .          1:  30         1:  10         1:  20
X                                  .              .              .
X
X  10 RET         20 RET         30 RET         M-TAB          M-TAB
X@end example
X@end group
X
XOperations like @kbd{+}, @kbd{-}, @kbd{*}, @kbd{/}, and @kbd{^} pop two
Xarguments from the stack and push a result.  Operations like @kbd{n} and
X@kbd{Q} (square root) pop a single number and push the result.  You can
Xthink of them as simply operating on the top element of the stack.
X
X@group
X@example
X1:  3          1:  9          2:  9          1:  25         1:  5
X    .              .          1:  16             .              .
X                                  .
X
X  3 RET          RET *        4 RET RET *        +              Q
X@end example
X@end group
X
XHere we use the Pythagorean Theorem to determine the hypotenuse of a
Xright triangle.  Calc actually has a built-in command for that, but
Xit isn't on any key.  Thus you must enter it using @kbd{M-x} notation:
X
X@group
X@example
X1:  3          2:  3          1:  5
X    .          1:  4              .
X                   .
X
X  3 RET          4 RET      M-x calc-hypot
X@end example
X@end group
X
XAll Calculator commands begin with the word @samp{calc-}.  Since it
Xgets tiring to type this, Calc provides an @kbd{x} key which is just
Xlike the regular Emacs @kbd{M-x} key except it provides the @samp{calc-}
Xprefix for you:
X
X@group
X@example
X1:  3          2:  3          1:  5
X    .          1:  4              .
X                   .
X
X  3 RET          4 RET         x hypot
X@end example
X@end group
X
XWhat happens if you take the square root of a negative number?
X
X@group
X@example
X1:  4          1:  -4         1:  (0, 2)
X    .              .              .
X                   .
X
X  4 RET            n              Q
X@end example
X@end group
X
XThe notation @samp{(@var{re}, @var{im})} represents a complex number.
XComplex numbers are more traditionally written @samp{@var{re} + @var{im}i};
XCalc can display in this format, too, but for now we'll stick to the
X@samp{(@var{re}, @var{im})} notation.
X
XIf you don't know how complex numbers work, you can safely ignore this
Xfeature.  Complex numbers only arise from operations that would be
Xerrors in a calculator that didn't have complex numbers.  (For example,
Xtaking the square root or logarithm of a negative number produces a
Xcomplex result.)
X
XComplex numbers are entered in the notation shown.  The @kbd{(} and
X@kbd{,} and @kbd{)} keys manipulate ``incomplete complex numbers.''
X
X@group
X@example
X1:  ( ...      2:  ( ...      1:  ( 2, ...   1:  ( 2, ...   1:  (2, 3)
X    .          1:  2              .              3              .
X                                                 .
X
X    (              2              ,              3              )
X@end example
X@end group
X
XYou can perform calculations while entering parts of incomplete objects.
XHowever, an incomplete object cannot actually be involved in a calculation:
X
X@group
X@example
X1:  ( ...      2:  ( ...      3:  ( ...      1:  ( ...      1:  ( ...
X    .          1:  2          2:  2              5              5
X                              1:  3              .              .
X                                  .
X                                                             (error)
X    (             2 RET           3              +              +
X@end example
X@end group
X
XAdding 5 to an incomplete object makes no sense, so the last command
Xproduces an error message and leaves the stack the same.
X
XIncomplete objects can't participate in arithmetic, but they can be
Xmoved around by the regular stack commands.
X
X@group
X@example
X2:  2          3:  2          3:  3          1:  ( ...      1:  (2, 3)
X1:  3          2:  3          2:  ( ...          2              .
X    .          1:  ( ...      1:  2              3
X                   .              .              .
X
X2 RET 3 RET        (            M-TAB          M-TAB            )
X@end example
X@end group
X
XNote that the @kbd{,} (comma) key did not have to be used here.
XWhen you press @kbd{)} all the stack entries between the incomplete
Xentry and the top are collected, so there's never really a reason
Xto use @kbd{,}.  It's up to you.
X
XVectors are entered the same way as complex numbers, but with square
Xbrackets in place of parentheses.  We'll meet vectors again later in
Xthe tutorial.
X
XAny Emacs command can be given a @dfn{numeric prefix argument} by
Xtyping a series of @key{META}-digits beforehand.  If @key{META} is
Xawkward for you, you can instead type @kbd{C-u} followed by the
Xnecessary digits.  Numeric prefix arguments can be negative, as in
X@kbd{M-- M-3 M-5} or @kbd{C-u - 3 5}.  Calc commands use numeric
Xprefix arguments in a variety of ways.  For example, a numeric prefix
Xon the @kbd{+} operator adds any number of stack entries at once:
X
X@group
X@example
X1:  10         2:  10         3:  10         3:  10         1:  60
X    .          1:  20         2:  20         2:  20             .
X                   .          1:  30         1:  30
X                                  .              .
X
X  10 RET         20 RET         30 RET         C-u 3            +
X@end example
X@end group
X
XFor stack manipulation commands, a positive numeric prefix argument
Xoperates on the top @var{n} stack entries at once.  A negative
Xargument operates on the entry in level @var{n} only.  An argument
Xof zero operates on the entire stack.  In this example, we copy
Xthe second-to-top element of the stack:
X
X@group
X@example
X1:  10         2:  10         3:  10         3:  10         4:  10
X    .          1:  20         2:  20         2:  20         3:  20
X                   .          1:  30         1:  30         2:  30
X                                  .              .          1:  20
X                                                                .
X
X  10 RET         20 RET         30 RET         C-u -2          RET
X@end example
X@end group
X
XAnother common idiom is @kbd{M-0 DEL}, which clears the stack.
X(The @kbd{M-0} numeric prefix tells @key{DEL} to operate on the
Xentire stack.)
X
X@node Algebraic Tutorial, Undo Tutorial, RPN Tutorial, Basic Tutorial
X@subsection Algebraic-Style Calculations
X
XIf you are not used to RPN notation, you may prefer to operate the
XCalculator in ``algebraic mode,'' which is closer to the way
Xnon-RPN calculators work.  In algebraic mode, you enter formulas
Xin traditional @samp{2+3} notation.
X
XYou don't really need any special ``mode'' to enter algebraic formulas.
XYou can enter a formula at any time by pressing the apostrophe (@kbd{'})
Xkey.  Answer the prompt with the desired formula, then press @key{RET}.
XThe formula is evaluated and the result is pushed onto the RPN stack.
XIf you don't want to think in RPN at all, you can enter your whole
Xcomputation as a formula, read the result from the stack, then press
X@key{DEL} to delete it from the stack.
X
XTry pressing the apostrophe key, then @kbd{2+3+4}, then @key{RET}.
XThe result should be the number 9.
X
XAlgebraic formulas use the operators @samp{+}, @samp{-}, @samp{*},
X@samp{/}, and @samp{^}.  You can use parentheses to make the order
Xof evaluation clear.  In the absence of parentheses, @samp{^} is
Xevaluated first, then @samp{*}, then @samp{/}, then finally
X@samp{+} and @samp{-}.  For example, the expression
X
X@example
X2 + 3*4*5 / 6*7^8 - 9
X@end example
X
X@noindent
Xis equivalent to
X
X@example
X2 + ((3*4*5) / (6*(7^8)) - 9
X@end example
X
X@noindent
Xor, in large mathematical notation,
X
X@group
X@example
X    3 * 4 * 5
X2 + --------- - 9
X          8
X     6 * 7
X@end example
X@end group
X
XThe result of this expression will be the number @samp{-6.99999826533}.
X
XCalc's order of evaluation is the same as for most computer languages,
Xexcept that @samp{*} binds more strongly than @samp{/}, as the above
Xexample shows.
X
XOperators at the same level are evaluated from left to right, except
Xthat @samp{^} is evaluated from right to left.  Thus, @samp{2-3-4} is
Xequivalent to @samp{(2-3)-4} or -5, whereas @samp{2^3^4} is equivalent
Xto @samp{2^(3^4)} (a very large integer; try it!).
X
XIf you tire of typing the apostrophe all the time, there is an
X``algebraic mode'' you can select in which Calc automatically senses
Xwhen you are about to type an algebraic expression.  To enter this
Xmode, press the two letters @kbd{m a}.  (An @samp{Alg} indicator
Xshould appear in the Calc window's mode line.)
X
XPress @kbd{m a}, then @kbd{2+3+4} with no apostrophe, then @key{RET}.
X
XIn algebraic mode, when you press any key that would normally begin
Xentering a number (such as a digit, a decimal point, or the @kbd{_}
Xkey), or if you press @kbd{(} or @kbd{[}, Calc automatically begins
Xan algebraic entry.
X
XFunctions which do not have operator symbols like @samp{+} and @samp{*}
Xmust be entered in formulas using function-call notation.  For example,
Xthe function name corresponding to the square-root key @kbd{Q} is
X@code{sqrt}.  To compute a square root in a formula, you would use
Xthe notation @samp{sqrt(@var{x})}.
X
XPress the apostrophe, then @kbd{sqrt(5*2) - 3}.  The result should
Xbe @samp{0.16227766017}.
X
XNote that if the formula begins with a function name, you need to use
Xthe apostrophe even if you are in algebraic mode.  If you type @kbd{sqrt}
Xout of the blue, the @kbd{s} will be taken as a Store command, and
Xthe @kbd{qrt} will be taken as the name of the variable to store!
X
XSome people prefer to enter complex numbers and vectors in algebraic
Xform because they find RPN entry with incomplete objects to be too
Xdistracting, even if they otherwise use Calc as an RPN calculator.
X
XStill in algebraic mode, type:
X
X@group
X@example
X1:  (2, 3)     2:  (2, 3)     1:  (8, -1)    2:  (8, -1)    1:  (9, -1)
X    .          1:  (1, -2)        .          1:  1              .
X                   .                             .          
X                                                            
X (2,3) RET      (1,-2) RET        *              1 RET          +
X@end example
X@end group
X
XAlgebraic mode allows us to enter complex numbers without pressing
Xan apostrophe first, but it also means we need to press @key{RET}
Xafter every entry, even for a simple number like @samp{1}.
X
XPress @kbd{m a} again to leave algebraic mode.
X
XActual non-RPN calculators use a mixture of algebraic and RPN styles.
XIn general, operators of two numbers (like @kbd{+} and @kbd{*})
Xuse algebraic form, but operators of one number (like @kbd{n} and @kbd{Q})
Xuse RPN form.  Also, a non-RPN calculator allows you to see the
Xintermediate results of a calculation as you go along.  You can
Xaccomplish this in Calc by performing your calculation as a series
Xof algebraic entries, using the @kbd{$} sign to tie them together.
XIn an algebraic formula, @kbd{$} represents the number on the top
Xof the stack.  Here, we perform the calculation @samp{sqrt(2*4+1)},
Xwhich on a traditional calculator would be done by pressing
X@kbd{2 * 4 + 1 =} and then the square-root key.
X
X@group
X@example
X1:  8          1:  9          1:  3
X    .              .              .
X
X  ' 2*4 RET        $+1 RET        Q
X@end example
X@end group
X
XNotice that we didn't need to press an apostrophe for the @kbd{$+1},
Xbecause the dollar sign always begins an algebraic entry.
X
XAlgebraic formulas can include @dfn{variables}.  To store in a
Xvariable, press @kbd{s}, then type the variable name, then press
X@key{RET}.  A variable name should consist of one or more letters
Xor digits, beginning with a letter.
X
X@group
X@example
X1:  17             .          1:  a + a^2    1:  306
X    .                             .              .
X
X  17 s a RET      DEL          ' a+a^2 RET       =
X@end example
X@end group
X
XThe @kbd{=} key @dfn{evaluates} a formula by replacing all variables
Xby the values that were stored in them.
X
XFor RPN calculations, you can recall a variable's value on the
Xstack either by entering its name as a formula and pressing @kbd{=},
Xor by using the @kbd{r} command.
X
X@group
X@example
X1:  17         2:  17         3:  17         2:  17         1:  306
X    .          1:  17         2:  17         1:  289            .
X                   .          1:  2              .
X                                  .
X
X   r a RET      ' a RET =         2              ^              +
X@end example
X@end group
X
XIf you press a single digit after @kbd{s} or @kbd{r}, you get one
Xof ten ``quick'' variables.  They are quick simply because you don't
Xhave to press @key{RET} after their names.
X
XAny variables in an algebraic formula for which you have not stored
Xvalues are left alone, even when you evaluate the formula.
X
X@group
X@example
X1:  2 a + 2 b     1:  34 + 2 b
X    .                 .
X
X ' 2a+2b RET          =
X@end example
X@end group
X
XCalls to function names which are undefined in Calc are also left
Xalone, as are calls for which the value is undefined.
X
X@group
X@example
X1:  2. + log10(0) + log10(5, 6) + foo(3)
X    .
X
X ' log10(100) + log10(0) + log10(5,6) + foo(3) RET
X@end example
X@end group
X
XIn this example, the first call to @code{log10} works, but the other
Xcalls are not evaluated.  In the second call, the logarithm is
Xundefined for that value of the argument; in the third, there are
Xtoo many arguments.  In the fourth case, there is no function
Xcalled @code{foo}.
X
XWe will encounter formulas involving variables and functions again
Xwhen we discuss the algebra and calculus features of the Calculator.
X
X@node Undo Tutorial, Modes Tutorial, Algebraic Tutorial, Basic Tutorial
X@subsection Undo and Redo
X
XIf you make a mistake, you can usually correct it by pressing shift-@kbd{U},
Xthe ``undo'' command.  First, to get a clean slate, press @kbd{M-0 DEL}
Xto clear the stack, then @kbd{M-# M-#} to stop and start the Calculator
Xover.  Now:
X
X@group
X@example
X1:  2          2:  2          1:  8          2:  2          1:  6
X    .          1:  3              .          1:  3              .
X                   .                             .
X
X   2 RET           3              ^              U              *
X@end example
X@end group
X
XYou can undo any number of times.  Calc keeps a complete record of
Xall you have done since you last pressed @kbd{M-#}.  After the
Xabove example, you could type:
X
X@group
X@example
X1:  6          2:  2          1:  2              .              .
X    .          1:  3              .
X                   .
X                                                             (error)
X                   U              U              U              U
X@end example
X@end group
X
XYou can also type @kbd{D} to ``redo'' a command that you have undone
Xmistakenly.
X
X@group
X@example
X    .          1:  2          2:  2          1:  6          1:  6
X                   .          1:  3              .              .
X                                  .
X                                                             (error)
X                   D              D              D              D
X@end example
X@end group
X
XWe are unable to redo past the @samp{6}, since that was placed there
Xby something other than an undo command.
X
XYou can think of undo and redo as a sort of ``time machine.''  Press
X@kbd{U} to go backward in time, @kbd{D} to go forward.  If you go
Xbackward and do something (like @kbd{*}) then, as any science fiction
Xreader knows, you have changed your future and you cannot go forward
Xagain.  Thus, the inability to redo past the @samp{6} even though there
Xwas an earlier undo command.
X
XYou can always recall an earlier result using the Trail.  We've ignored
Xthe trail so far, but it has been faithfully recording everything we
Xdid since we loaded the Calculator.  If the Trail is not displayed,
Xpress @kbd{t d} now to turn it on.
X
XLet's try grabbing an earlier result.  The @samp{8} we computed was
Xundone by a @kbd{U} command, and was lost even to Redo when we pressed
X@kbd{*}, but it's still there in the trail.  There should be a little
X@samp{>} arrow (the @dfn{trail pointer}) resting on the last trail
Xentry.  If there isn't, press @kbd{t ]} to reset the trail pointer.
XNow, press @kbd{t p} to move the arrow onto the line containing
X@samp{8}, and press @kbd{t y} to ``yank'' that number back onto the
Xstack.
X
XIf you press @kbd{t ]}, you will see that even our Yank command went
Xinto the trail.
X
XLet's go further back in time.  Earlier in the tutorial we computed
Xa huge integer using the formula @samp{2^3^4}.  We don't remember
Xwhat it was, but the first digits were ``241''.  Press @kbd{t r}
X(which stands for trail-search-reverse), then type @kbd{241}.
XThe trail cursor will jump back to the next previous occurrence of
Xthe string ``241'' in the trail.  This is just a regular Emacs
Xincremental search; you can now press @kbd{C-s} or @kbd{C-r} to
Xcontinue the search forwards or backwards as you like.
X
XTo finish the search, press @key{ESC} (or @kbd{C-[} if your keyboard
Xdoesn't have @key{ESC}).  This halts the incremental search and
Xleaves the trail pointer at the thing we found.  Now we can type
X@kbd{t y} to yank that number onto the stack.  If we hadn't
Xremembered the ``241'', we could simply have searched for @kbd{2^3^4},
Xthen pressed @kbd{ESC t n} to halt and then move to the next item.
X
XYou may have noticed that all the trail-related commands begin with
Xthe letter @kbd{t}.  Calc has so many commands that there aren't
Xenough keys for all of them, so various commands are grouped into
Xtwo-letter sequences where the first letter is called the @dfn{prefix}
Xkey.  If you type a prefix key by accident, you can press @kbd{C-g}
Xto cancel it.  (In fact, you can press @kbd{C-g} to cancel almost
Xanything in Emacs.)  To get help on a prefix key, press the key
Xfollowed by @kbd{?}.  Some prefixes have several lines of help,
Xso you need to press @kbd{?} repeatedly to see them all.
X
XTry pressing @kbd{t ?} now.  You will see a line of the form,
X
X@example
Xtrail: Display; Fwd, Back; Next, Prev, Here, [, ]; Yank:  [MORE]  t-
X@end example
X
XThe word ``trail'' indicates that the @kbd{t} prefix key contains
Xtrail-related commands.  Each entry on the line shows one command,
Xwith a single capital letter showing which letter you press to get
Xthat command.  We have used @kbd{t n}, @kbd{t p}, @kbd{t ]}, and
X@kbd{t y} so far.  The @samp{[MORE]} means you can press @kbd{?}
Xagain to see more @kbd{t}-prefix comands.  Notice that the commands
Xare roughly divided (by semicolons) into related groups.
X
XWhen you are in the help display for a prefix key, the prefix is
Xstill active.  If you press another key, like @kbd{y} for example,
Xit will be interpreted as a @kbd{t y} command.  If all you wanted
Xwas to look at the help messages, press @kbd{C-g} afterwards to cancel
Xthe prefix.
X
XOne more way to correct an error is by editing the stack entries.
XThe actual Stack buffer is marked read-only and must not be edited
Xdirectly, but you can press @kbd{`} (the backquote or accent grave)
Xto edit a stack entry.
X
XTry entering @samp{3.141439} now.  If this is supposed to represent
X@samp{pi}, it's got several errors.  Press @kbd{`} to edit this number.
XNow use the normal Emacs cursor motion and editing keys to change
Xthe second 4 to a 5, and to transpose the 3 and the 9.  When you
Xpress @key{RET}, the number of the stack will be replaced by your
Xnew number.  This also works for formulas, vectors, and other types
Xof values you can put on the stack.
X
X@node Modes Tutorial, , Undo Tutorial, Basic Tutorial
X@subsection Mode-Setting Commands
X
XCalc has many types of @dfn{modes} that affect the way it interprets
Xyour commands or the way it displays data.  We have already seen one
Xmode, namely algebraic mode.  There are many others, too; we'll
Xtry some of the most common ones here.
X
XPerhaps the most fundamental mode in Calc is the current @dfn{precision}.
XNotice the @samp{12} on the Calc window's mode line:
X
X@example
X--%%-Calc: 12 Deg       (Calculator)----All------
X@end example
X
XMost of the symbols there are Emacs things you don't need to worry
Xabout, but the @samp{12} and the @samp{Deg} are mode indicators.
XThe @samp{12} means that calculations should always be carried to
X12 significant figures.  That is why, when we type @kbd{1 @key{RET} 7 /},
Xwe get @samp{0.142857142857} with exactly 12 digits, not counting
Xleading and trailing zeros.
X
XYou can set the precision to anything you like by pressing @kbd{p},
Xthen entering a suitable number.  Try pressing @kbd{p 30 @key{RET}},
Xthen doing @kbd{1 @key{RET} 7 /} again:
X
X@group
X@example
X1:  0.142857142857
X2:  0.142857142857142857142857142857
X    .
X@end example
X@end group
X
XAlthough the precision can be set arbitrarily high, Calc always
Xhas to have @emph{some} value for the current precision.  After
Xall, the true value @samp{1/7} is an infinitely repeating decimal;
XCalc has to stop somewhere.
X
XOf course, calculations are slower the more digits you request.
XPress @kbd{p 12} to set the precision back down to the default.
X
XCalculations always use the current precision.  For example, even
Xthough we have a 30-digit value for @samp{1/7} on the stack, if
Xwe use it in a calculation in 12-digit mode, it will be rounded
Xdown to 12 digits before it is used.  Try it; press @key{RET} to
Xduplicate the number, then @kbd{1 +}.  Notice that the @key{RET}
Xkey didn't round the number, because it doesn't do any calculation.
XBut the instant we pressed @kbd{+}, the number was rounded down.
X
X@group
X@example
X1:  0.142857142857
X2:  0.142857142857142857142857142857
X3:  1.14285714286
X    .
X@end example
X@end group
X
XIn fact, since we added a digit on the left, we had to lose one
Xdigit on the right from even the 12-digit value of @samp{1/7}.
X
XHow did we get more than 12 digits when we computed @samp{2^3^4}?  The
Xanswer is that Calc makes a distinction between @dfn{integers} and
X@dfn{floating-point} numbers, or @dfn{floats}.  An integer is a number
Xthat does not contain a decimal point.  There is no such thing as an
X``infinitely repeating fraction integer,'' so Calc doesn't have to limit
Xitself.  If you asked for @kbd{2^10000} (don't try this!), you would
Xhave to wait a long time but you would eventually get an exact answer.
XIf you ask for @samp{2.^10000}, you will quickly get an answer which is
Xcorrect only to 12 places.  The decimal point tells Calc that it should
Xuse floating-point arithmetic to get the answer, not exact integer
Xarithmetic.
X
XLet's try entering that last calculation:
X
X@group
X@example
X1:  2.         2:  2.         1:  1.99506311689e3010
X    .          1:  10000          .
X                   .
X
X  2.0 RET          10000 RET      ^
X@end example
X@end group
X
XNotice the letter @kbd{e} in there.  It represents ``times ten to the
Xpower of,'' and is used by Calc automatically whenever writing the
Xnumber out fully would introduce more extra zeros than you probably
Xwant to see.  You can enter numbers in this notation, too.
X
X@group
X@example
X1:  2.         2:  2.         1:  1.99506311678e3010
X    .          1:  10000.         .
X                   .
X
X  2. RET           1e4 RET        ^
X@end example
X@end group
X
XHey, the answer is different!  Look closely at the middle columns
Xof the two examples.  In the first, the stack contained the
Xexact integer @samp{10000}, but in the second it contained
Xa floating-point value with a decimal point.  When you raise a
Xnumber to an integer power, Calc uses repeated squaring and
Xmultiplication to get the answer.  When you use a floating-point
Xpower, Calc uses logarithms and exponentials.  As you can see,
Xa slight error crept in during one of these methods.  Which
Xone should we trust?  Let's raise the precision a bit and find
Xout:
X
X@group
X@example
X    .          1:  2.         2:  2.         1:  1.995063116880828e3010
X                   .          1:  10000.         .
X                                  .
X
X p 16 RET        2. RET           1e4            ^
X@end example
X@end group
X
XPresumably, it doesn't matter whether we do this higher-precision
Xcalculation using an integer or floating-point power, since we
Xhave added enough ``guard digits'' to trust the first 12 digits
Xno matter what.  And the verdict is@dots{}  Integer powers were more
Xaccurate; in fact, the result was only off by one unit in the
Xlast place.
X
XCalc does many of its internal calculations to a slightly higher
Xprecision, but it doesn't always bump the precision up enough.
XIn each case, Calc added about two digits of precision during
Xits calculation and then rounded back down to 12 digits
Xafterward.  In one case, it was enough; in the the other, it
Xwasn't.  If you really need @var{x} digits of precision, it
Xnever hurts to do the calculation with a few extra guard digits.
X
XWhat if we want guard digits but don't want to look at them?
XWe can set the @dfn{float format}.  Calc supports four major
Xformats for floating-point numbers, called @dfn{normal},
X@dfn{fixed-point}, @dfn{scientific notation}, and @dfn{engineering
Xnotation}.  You get them by pressing @kbd{d n}, @kbd{d f},
X@kbd{d s}, and @kbd{d e}, respectively.  In each case, you can
Xsupply a numeric prefix argument which says how many digits
Xshould be displayed.  As an example, put the following numbers
Xonto the stack, then try some different display modes.  First,
Xuse @kbd{M-0 DEL} to clear the stack, then enter the four
Xnumbers shown here:
X
X@group
X@example
X4:  12345      4:  12345      4:  12345      4:  12345      4:  12345
X3:  12345.     3:  12300.     3:  1.2345e4   3:  1.23e4     3:  12345.000
X2:  123.45     2:  123.       2:  1.2345e2   2:  1.23e2     2:  123.450
X1:  12.345     1:  12.3       1:  1.2345e1   1:  1.23e1     1:  12.345
X    .              .              .              .              .
X
X   d n          M-3 d n          d s          M-3 d s        M-3 d f
X@end example
X@end group
X
XNotice that when we typed @kbd{M-3 d n}, the numbers were rounded down
Xto three significant digits, but then when we typed @kbd{d s} all
Xfive significant figures reappeared.  The float format does not
Xaffect how numbers are stored, it only affects how they are
Xdisplayed.  Only the current precision governs the actual rounding
Xof numbers in the Calculator's memory.
X
XEngineering notation, not shown here, is like scientific notation
Xexcept the exponent (the power-of-ten part) is always adjusted to be
Xa multiple of three (as in ``kilo,'' ``micro,'' etc.).  As a result
Xthere will be one, two, or three digits before the decimal point.
X
XAlso notice that the integer @samp{12345} was not affected by any
Xof the float formats.  Integers are integers, and are always
Xdisplayed exactly.
X
XType @kbd{d n} now to return to the normal float format.
X
XLarge integers have their own problems.  Let's look back at
Xthe result of @kbd{2^3^4}.
X
X@example
X2417851639229258349412352
X@end example
X
XQuick---how many digits does this have?  Try typing @kbd{d g}:
X
X@example
X2,417,851,639,229,258,349,412,352
X@end example
X
XNow how many digits does this have?  It's much easier to tell!
XWe can actually group digits into clumps of any size.  Some
Xpeople prefer @kbd{M-5 d g}:
X
X@example
X24178,51639,22925,83494,12352
X@end example
X
XLet's see what happens to floating-point numbers when they are grouped.
XFirst, type @kbd{p 25 @key{RET}} to make sure we have enough precision
Xto get ourselves into trouble.  Now, type @kbd{1e13 /}:
X
X@example
X24,17851,63922.9258349412352
X@end example
X
XThe integer part is grouped but the fractional part isn't.  Now try
X@kbd{M-- M-5 d g} (that's meta-minus-sign, meta-five):
X
X@example
X24,17851,63922.92583,49412,352
X@end example
X
XIf you find it hard to tell the decimal point from the commas, try
Xchanging the grouping character to a space with @kbd{d , @key{SPC}}:
X
X@example
X24 17851 63922.92583 49412 352
X@end example
X
XType @kbd{d , ,} to restore the normal grouping character, then
X@kbd{d g} again to turn grouping off.  Also, press @kbd{p 12} to
Xrestore the default precision.
X
XPress @kbd{U} enough times to get the original big integer back.
X(Notice that @kbd{U} does not undo each mode-setting command; if
Xyou want to undo a mode-setting command, you have to do it yourself.)
XNow, type @kbd{d r 16 @key{RET}}:
X
X@example
X16#200000000000000000000
X@end example
X
XThe number is now displayed in @dfn{hexadecimal}, or ``base-16'' form.
XSuddenly it looks pretty simple; this should be no surprise, since we
Xgot this number by computing a power of two, and 16 is a multiple of 2.
XIn fact, we can use @kbd{d r 2 @key{RET}} to see it in actual binary
Xform:
X
X@example
X2#1000000000000000000000000000000000000000000000000000000 @dots{}
X@end example
X
XWe don't have enough space here to show all the zeros!  They won't
Xall fit on the screen, either, so you will have to use horizontal
Xscrolling to see them all.  Press @kbd{<} and @kbd{>} to scroll the
Xstack window left and right by half its width.
X
XYou can enter non-decimal numbers using the @kbd{#} symbol, too.
XLet's see what the hexadecimal number @samp{5FE} looks like in
Xbinary.  Type @kbd{16#5FE} (the F's can be in upper or lower case).
XIt will also help to turn grouping on with @kbd{d g}:
X
X@example
X2#101,1111,1110
X@end example
X
XNotice that @kbd{d g} groups by fours by default if the display radix
Xis binary or hexadecimal, but by threes if it is decimal, octal, or any
Xother radix.
X
XNow let's see that number in decimal; type @kbd{d r 10}:
X
X@example
X1,534
X@end example
X
XNumbers are not @emph{stored} with any particular radix attached.  They're
Xjust integers; they can be entered in any radix, and are always displayed
Xin whatever radix you've chosen with @kbd{d r}.  Floating-point numbers
Xare always entered and displayed in decimal.
X
XThe @kbd{m} prefix key has another set of modes, relating to the way
XCalc interprets your inputs and does computations.  Whereas @kbd{d}-prefix
Xmodes generally affect the way thing look, @kbd{m}-prefix modes affect
Xthey way they are actually computed.
X
XThe most popular @kbd{m}-prefix mode is the @dfn{angular mode}.  Notice
Xthe @samp{Deg} indicator in the mode line.  This means that if you use
Xa command that interprets a number as an angle, it will assume the
Xangle is measured in degrees.  For example,
X
X@group
X@example
X1:  45         1:  0.707106781187   1:  0.500000000001    1:  0.5
X    .              .                    .                     .
X
X    45             S                    2 ^                   c 1
X@end example
X@end group
X
XThe shift-@kbd{S} command computes the sine of an angle.  The sine
Xof 45 degrees is @samp{sqrt(2)/2}; squaring this yields {2/4 = 0.5}.
XHowever, there has been a slight roundoff error because the
Xresentation of @samp{sqrt(2)/2} wasn't exact.  The @kbd{c 1}
Xcommand is a handy way to clean up numbers in this case; it
Xtemporarily reduces the precision by one digit while it
Xre-rounds the number on the top of the stack.
X
XTo do this calculation in radians, we would type @kbd{m r} first.
X(The indicator changes to @samp{Rad}.)  45 degrees corresponds to
X@samp{pi/4} radians.  To get @samp{pi}, press the @kbd{P} key.  (Once
Xagain, this is a shifted capital @kbd{P}.  Remember, unshifted
X@kbd{p} sets the precision.)
X
X@group
X@example
X1:  3.14159265359   1:  0.785398163398   1:  0.707106781187
X    .                   .                .
X                                        
X    P                   4 /              S
X@end example
X@end group
X
XLikewise, inverse trigonometric functions generate results in
Xeither radians or degrees, depending on the current angular mode.
X
X@group
X@example
X1:  0.707106781187   1:  0.785398163398   1:  45.
X    .                    .                .
X                                        
X    .5 Q        m r      I S      m d     U I S
X@end example
X@end group
X
XHere we compute the Inverse Sine of @samp{sqrt(0.5)}, first in
Xradians, then in degrees.
X
XUse @kbd{c d} and @kbd{c r} to convert a number from radians to degrees
Xand vice-versa.
X
X@group
X@example
X1:  45         1:  0.785398163396   1:  45.
X    .              .                .
X
X    45             c r              c d
X@end example
X@end group
X
XAnother interesting mode is @dfn{fraction mode}.  Normally,
Xdividing two integers produces a floating-point result if the
Xquotient can't be expressed as an exact integer.  Fraction mode
Xcauses integer division to produce a fraction, i.e., a rational
Xnumber, instead.
X
X@group
X@example
X2:  12         1:  1.33333333333    1:  4:3
X1:  9              .                    .
X    .
X
X 12 RET 9          /          m f       U /
X@end example
X@end group
X
XIn the first case, we get an approximate floating-point result.
XIn the second case, we get an exact fractional result.
X
XYou can enter a fraction at any time using @kbd{:} notation.
X(Calc uses @kbd{:} instead of @kbd{/} as the fraction separator
Xbecause @kbd{/} is already used to divide the top two stack
Xelements.)  Calculations involving fractions will always
Xproduce exact fractional results; fraction mode only says
Xwhat to do when dividing integers.
X
X@node Arithmetic Tutorial, Vector/Matrix Tutorial, Basic Tutorial, Tutorial
X@section Arithmetic Tutorial
X
XIn this section, we explore the arithmetic and scientific functions
Xavailable in the Calculator.
X
XThe standard arithmetic commands are @kbd{+}, @kbd{-}, @kbd{*}, @kbd{/},
Xand @kbd{^}.  Each normally takes two numbers from the top of the stack
Xand pushes back a result.  The @kbd{n} and @kbd{&} keys perform
Xchange-sign and reciprocal operations, respectively.
X
X@group
X@example
X1:  5          1:  0.2        1:  5.         1:  -5.        1:  5.
X    .              .              .              .              .
X
X    5              &              &              n              n
X@end example
X@end group
X
XYou can apply a ``binary operator'' like @kbd{+} across any number of
Xstack entries by giving it a numeric prefix.  You can also apply it
Xpairwise to several stack elements along with the top one if you use
Xa negative prefix.
X
X@group
X@example
X3:  2          1:  9          3:  2          4:  2          3:  12
X2:  3              .          2:  3          3:  3          2:  13
X1:  4                         1:  4          2:  4          1:  14
X    .                             .          1:  10             .
X                                                 .
X
X2 RET 3 RET 4     M-3 +           U              10          M-- M-3 +
X@end example
X@end group
X
XYou can apply a ``unary operator'' like @kbd{&} to the top @var{n}
Xstack entries with a numeric prefix, too.
X
X@group
X@example
X3:  2          3:  0.5                3:  0.5
X2:  3          2:  0.333333333333     2:  3.
X1:  4          1:  0.25               1:  4.
X    .              .                      .
X
X2 RET 3 RET 4     M-3 &                  M-2 &
X@end example
X@end group
X
XNotice that the results here are left in floating-point form.
XWe can convert them back to integers by pressing @kbd{F}, the
X``floor'' function.  This function rounds down to the next lower
Xinteger.  There is also @kbd{R}, which rounds to the nearest
Xinteger.
X
X@group
X@example
X7:  2.         7:  2          7:  2
X6:  2.4        6:  2          6:  2
X5:  2.5        5:  2          5:  3
X4:  2.6        4:  2          4:  3
X3:  -2.        3:  -2         3:  -2
X2:  -2.4       2:  -3         2:  -2
X1:  -2.6       1:  -3         1:  -3
X    .              .              .
X
X                  M-7 F        U M-7 R
X@end example
X@end group
X
XSince dividing-and-flooring is such a common operation, Calc
Xprovides a special command for that purpose, the backslash @kbd{\}.
XAnother common arithmetic operator is @kbd{%}, which computes the
Xremainder that would arise from a @kbd{\} operation, i.e., the
X``modulo'' of two numbers.  For example,
X
X@group
X@example
X2:  1234       1:  12         2:  1234       1:  34
X1:  100            .          1:  100            .
X    .                             .
X
X1234 RET 100       \              U              %
X@end example
X@end group
X
XThese commands actually work for any real numbers, not just integers.
X
X@group
X@example
X2:  3.1415     1:  3          2:  3.1415     1:  0.1415
X1:  1              .          1:  1              .
X    .                             .
X
X3.1415 RET 1       \              U              %
X@end example
X@end group
X
XWe've already seen the @kbd{Q} (square root) and @kbd{S} (sine)
Xcommands.  Other commands along those lines are @kbd{C} (cosine),
X@kbd{T} (tangent), @kbd{E} (@samp{e^x}) and @kbd{L} (natural
Xlogarithm).  These can be modified by the @kbd{I} (inverse) and
X@kbd{H} (hyperbolic) prefix keys.
X
XLet's compute the sine and cosine of an angle, and verify the
Xidentity @samp{sin(@var{x})^2 + cos(@var{x})^2 = 1}.  We'll
Xarbitrarily pick -64 degrees as a good value for @var{x}.  With
Xthe angular mode set to degrees (@kbd{m d}), do:
X
X@group
X@example
X2:  -64        2:  -64        2:  -0.89879   2:  -0.89879   1:  1.
X1:  -64        1:  -0.89879   1:  -64        1:  0.43837        .
X    .              .              .              .
X
X 64 n RET RET      S             TAB             C            x hypot
X@end example
X@end group
X
X(For brevity, we're showing only five digits of the results here.
XYou can of course do these calculations to any precision you like.)
X
XAnother identity is @samp{tan(@var{x}) = sin(@var{x}) / cos(@var{x})}.
X
X@group
X@example
X2:  -0.89879   1:  -2.0503    1:  -64.
X1:  0.43837        .              .
X    .
X
X    U              /             I T
X@end example
X@end group
X
XA physical interpretation of this calculation is that if you move
X@samp{0.89879} units downward and @samp{0.43837} units to the right,
SHAR_EOF
echo "End of part 12"
echo "File calc.texinfo is continued in part 13"
echo "13" > s2_seq_.tmp
exit 0