home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume24 / gnucalc / part32 < prev    next >
Encoding:
Text File  |  1991-10-31  |  55.5 KB  |  1,536 lines

  1. Newsgroups: comp.sources.misc
  2. From: daveg@synaptics.com (David Gillespie)
  3. Subject:  v24i080:  gnucalc - GNU Emacs Calculator, v2.00, Part32/56
  4. Message-ID: <1991Oct31.214441.2310@sparky.imd.sterling.com>
  5. X-Md4-Signature: 72b72746ae9b5f12caa6a0ec114001b6
  6. Date: Thu, 31 Oct 1991 21:44:41 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: daveg@synaptics.com (David Gillespie)
  10. Posting-number: Volume 24, Issue 80
  11. Archive-name: gnucalc/part32
  12. Environment: Emacs
  13. Supersedes: gmcalc: Volume 13, Issue 27-45
  14.  
  15. ---- Cut Here and unpack ----
  16. #!/bin/sh
  17. # do not concatenate these parts, unpack them in order with /bin/sh
  18. # file calc.texinfo continued
  19. #
  20. if test ! -r _shar_seq_.tmp; then
  21.     echo 'Please unpack part 1 first!'
  22.     exit 1
  23. fi
  24. (read Scheck
  25.  if test "$Scheck" != 32; then
  26.     echo Please unpack part "$Scheck" next!
  27.     exit 1
  28.  else
  29.     exit 0
  30.  fi
  31. ) < _shar_seq_.tmp || exit 1
  32. if test ! -f _shar_wnt_.tmp; then
  33.     echo 'x - still skipping calc.texinfo'
  34. else
  35. echo 'x - continuing file calc.texinfo'
  36. sed 's/^X//' << 'SHAR_EOF' >> 'calc.texinfo' &&
  37. @samp{+} and @samp{-}.  For example, the expression
  38. X
  39. @example
  40. 2 + 3*4*5 / 6*7^8 - 9
  41. @end example
  42. X
  43. @noindent
  44. is equivalent to
  45. X
  46. @example
  47. 2 + ((3*4*5) / (6*(7^8)) - 9
  48. @end example
  49. X
  50. @noindent
  51. or, in large mathematical notation,
  52. X
  53. @ifinfo
  54. @group
  55. @example
  56. X    3 * 4 * 5
  57. 2 + --------- - 9
  58. X          8
  59. X     6 * 7
  60. @end example
  61. @end group
  62. @end ifinfo
  63. @tex
  64. \turnoffactive
  65. $$ 2 + { 3 \times 4 \times 5 \over 6 \times 7^8 } - 9 $$
  66. @end tex
  67. X
  68. @noindent
  69. The result of this expression will be the number @i{-6.99999826533}.
  70. X
  71. Calc's order of evaluation is the same as for most computer languages,
  72. except that @samp{*} binds more strongly than @samp{/}, as the above
  73. example shows.  As in normal mathematical notation, the @samp{*} symbol
  74. can often be omitted:  @samp{2 a} is the same as @samp{2*a}.
  75. X
  76. Operators at the same level are evaluated from left to right, except
  77. that @samp{^} is evaluated from right to left.  Thus, @samp{2-3-4} is
  78. equivalent to @samp{(2-3)-4} or @i{-5}, whereas @samp{2^3^4} is equivalent
  79. to @samp{2^(3^4)} (a very large integer; try it!).
  80. X
  81. If you tire of typing the apostrophe all the time, there is an
  82. ``algebraic mode'' you can select in which Calc automatically senses
  83. when you are about to type an algebraic expression.  To enter this
  84. mode, press the two letters @w{@kbd{m a}}.  (An @samp{Alg} indicator
  85. should appear in the Calc window's mode line.)
  86. X
  87. Press @kbd{m a}, then @kbd{2+3+4} with no apostrophe, then @key{RET}.
  88. X
  89. In algebraic mode, when you press any key that would normally begin
  90. entering a number (such as a digit, a decimal point, or the @kbd{_}
  91. key), or if you press @kbd{(} or @kbd{[}, Calc automatically begins
  92. an algebraic entry.
  93. X
  94. Functions which do not have operator symbols like @samp{+} and @samp{*}
  95. must be entered in formulas using function-call notation.  For example,
  96. the function name corresponding to the square-root key @kbd{Q} is
  97. @code{sqrt}.  To compute a square root in a formula, you would use
  98. the notation @samp{sqrt(@var{x})}.
  99. X
  100. Press the apostrophe, then type @kbd{sqrt(5*2) - 3}.  The result should
  101. be @cite{0.16227766017}.
  102. X
  103. Note that if the formula begins with a function name, you need to use
  104. the apostrophe even if you are in algebraic mode.  If you type @kbd{arcsin}
  105. out of the blue, the @kbd{a r} will be taken as an Algebraic Rewrite
  106. command, and the @kbd{csin} will be taken as the name of the rewrite
  107. rule to use!
  108. X
  109. Some people prefer to enter complex numbers and vectors in algebraic
  110. form because they find RPN entry with incomplete objects to be too
  111. distracting, even though they otherwise use Calc as an RPN calculator.
  112. X
  113. Still in algebraic mode, type:
  114. X
  115. @group
  116. @smallexample
  117. 1:  (2, 3)     2:  (2, 3)     1:  (8, -1)    2:  (8, -1)    1:  (9, -1)
  118. X    .          1:  (1, -2)        .          1:  1              .
  119. X                   .                             .
  120. X
  121. X (2,3) RET      (1,-2) RET        *              1 RET          +
  122. @end smallexample
  123. @end group
  124. X
  125. Algebraic mode allows us to enter complex numbers without pressing
  126. an apostrophe first, but it also means we need to press @key{RET}
  127. after every entry, even for a simple number like @cite{1}.
  128. X
  129. (You can type @kbd{C-u m a} to enable a special ``incomplete algebraic
  130. mode'' in which the @kbd{(} and @kbd{[} keys use algebraic entry even
  131. though regular numeric keys still use RPN numeric entry.  There is also
  132. a ``total algebraic mode'', started by typing @kbd{m t}, in which all
  133. normal keys begin algebraic entry.  You must then use the @key{META} key
  134. to type Calc commands:  @kbd{M-m t} to get back out of total algebraic
  135. mode, @kbd{M-q} to quit, etc.)
  136. X
  137. If you're still in algebraic mode, press @kbd{m a} again to turn it off.
  138. X
  139. Actual non-RPN calculators use a mixture of algebraic and RPN styles.
  140. In general, operators of two numbers (like @kbd{+} and @kbd{*})
  141. use algebraic form, but operators of one number (like @kbd{n} and @kbd{Q})
  142. use RPN form.  Also, a non-RPN calculator allows you to see the
  143. intermediate results of a calculation as you go along.  You can
  144. accomplish this in Calc by performing your calculation as a series
  145. of algebraic entries, using the @kbd{$} sign to tie them together.
  146. In an algebraic formula, @kbd{$} represents the number on the top
  147. of the stack.  Here, we perform the calculation @c{$\sqrt{2\times4+1}$}
  148. @cite{sqrt(2*4+1)},
  149. which on a traditional calculator would be done by pressing
  150. @kbd{2 * 4 + 1 =} and then the square-root key.
  151. X
  152. @group
  153. @smallexample
  154. 1:  8          1:  9          1:  3
  155. X    .              .              .
  156. X
  157. X  ' 2*4 RET        $+1 RET        Q
  158. @end smallexample
  159. @end group
  160. X
  161. @noindent
  162. Notice that we didn't need to press an apostrophe for the @kbd{$+1},
  163. because the dollar sign always begins an algebraic entry.
  164. X
  165. (@bullet{}) @strong{Exercise 1.}  How could you get the same effect as
  166. pressing @kbd{Q} but using an algebraic entry instead?  How about
  167. if the @kbd{Q} key on your keyboard were broken?
  168. @xref{Algebraic Answer 1, 1}. (@bullet{})
  169. X
  170. Algebraic formulas can include @dfn{variables}.  To store in a
  171. variable, press @kbd{s s}, then type the variable name, then press
  172. @key{RET}.  (There are actually two flavors of store command:
  173. @kbd{s s} stores a number in a variable but also leaves the number
  174. on the stack, while @w{@kbd{s t}} removes a number from the stack and
  175. stores it in the variable.)  A variable name should consist of one
  176. or more letters or digits, beginning with a letter.
  177. X
  178. @group
  179. @smallexample
  180. 1:  17             .          1:  a + a^2    1:  306
  181. X    .                             .              .
  182. X
  183. X    17          s t a RET      ' a+a^2 RET       =
  184. @end smallexample
  185. @end group
  186. X
  187. @noindent
  188. The @kbd{=} key @dfn{evaluates} a formula by replacing all its
  189. variables by the values that were stored in them.
  190. X
  191. For RPN calculations, you can recall a variable's value on the
  192. stack either by entering its name as a formula and pressing @kbd{=},
  193. or by using the @kbd{s r} command.
  194. X
  195. @group
  196. @smallexample
  197. 1:  17         2:  17         3:  17         2:  17         1:  306
  198. X    .          1:  17         2:  17         1:  289            .
  199. X                   .          1:  2              .
  200. X                                  .
  201. X
  202. X  s r a RET     ' a RET =         2              ^              +
  203. @end smallexample
  204. @end group
  205. X
  206. If you press a single digit for a variable name (as in @kbd{s t 3}, you
  207. get one of ten @dfn{quick variables} @code{q0} through @code{q9}.
  208. They are ``quick'' simply because you don't have to type the letter
  209. @code{q} or the @key{RET} after their names.  In fact, you can type
  210. simply @kbd{s 3} as a shorthand for @kbd{s s 3}, and likewise for
  211. @kbd{t 3} and @w{@kbd{r 3}}.
  212. X
  213. Any variables in an algebraic formula for which you have not stored
  214. values are left alone, even when you evaluate the formula.
  215. X
  216. @group
  217. @smallexample
  218. 1:  2 a + 2 b     1:  34 + 2 b
  219. X    .                 .
  220. X
  221. X ' 2a+2b RET          =
  222. @end smallexample
  223. @end group
  224. X
  225. Calls to function names which are undefined in Calc are also left
  226. alone, as are calls for which the value is undefined.
  227. X
  228. @group
  229. @smallexample
  230. 1:  2 + log10(0) + log10(x) + log10(5, 6) + foo(3)
  231. X    .
  232. X
  233. X ' log10(100) + log10(0) + log10(x) + log10(5,6) + foo(3) RET
  234. @end smallexample
  235. @end group
  236. X
  237. @noindent
  238. In this example, the first call to @code{log10} works, but the other
  239. calls are not evaluated.  In the second call, the logarithm is
  240. undefined for that value of the argument; in the third, the argument
  241. is symbolic, and in the fourth, there are too many arguments.  In the
  242. fifth case, there is no function called @code{foo}.  You will see a
  243. ``Wrong number of arguments'' message referring to @samp{log10(5,6)}.
  244. Press the @kbd{w} (``why'') key to see any other messages that may
  245. have arisen from the last calculation.  In this case you will get
  246. ``logarithm of zero,'' then ``number expected: @code{x}''.  Calc
  247. automatically displays the first message only if the message is
  248. sufficiently important; for example, Calc considers ``wrong number
  249. of arguments'' and ``logarithm of zero'' to be important enough to
  250. report automatically, while a message like ``number expected: @code{x}''
  251. will only show up if you explicitly press the @kbd{w} key.
  252. X
  253. (@bullet{}) @strong{Exercise 2.}  Joe entered the formula @samp{2 x y},
  254. stored 5 in @code{x}, pressed @kbd{=}, and got the expected result,
  255. @samp{10 y}.  He then tried the same for the formula @samp{2 x (1+y)},
  256. expecting @samp{10 (1+y)}, but it didn't work.  Why not?
  257. @xref{Algebraic Answer 2, 2}. (@bullet{})
  258. X
  259. (@bullet{}) @strong{Exercise 3.}  What result would you expect
  260. @kbd{1 @key{RET} 0 /} to give?  What if you then type @kbd{0 *}?
  261. @xref{Algebraic Answer 3, 3}. (@bullet{})
  262. X
  263. One interesting way to work with variables is to use the
  264. @dfn{evaluates-to} (@samp{=>}) operator.  It works like this:
  265. Enter a formula algebraically in the usual way, but follow
  266. the formula with an @samp{=>} symbol.  (There is also an @kbd{s =}
  267. command which builds an @samp{=>} formula using the stack.)  On
  268. the stack, you will see two copies of the formula with an @samp{=>}
  269. between them.  The lefthand formula is exactly like you typed it;
  270. the righthand formula has been evaluated as if by typing @kbd{=}.
  271. X
  272. @group
  273. @smallexample
  274. 2:  2 + 3 => 5                     2:  2 + 3 => 5
  275. 1:  2 a + 2 b => 34 + 2 b          1:  2 a + 2 b => 20 + 2 b
  276. X    .                                  .
  277. X
  278. ' 2+3 => RET  ' 2a+2b RET s =          10 s t a RET
  279. @end smallexample
  280. @end group
  281. X
  282. @noindent
  283. Notice that the instant we stored a new value in @code{a}, all
  284. @samp{=>} operators already on the stack that referred to @cite{a}
  285. were updated to use the new value.  With @samp{=>}, you can push a
  286. set of formulas on the stack, then change the variables experimentally
  287. to see the effects on the formulas' values.
  288. X
  289. You can also ``unstore'' a variable when you are through with it:
  290. X
  291. @group
  292. @smallexample
  293. 2:  2 + 5 => 5
  294. 1:  2 a + 2 b => 2 a + 2 b
  295. X    .
  296. X
  297. X    s u a RET
  298. @end smallexample
  299. @end group
  300. X
  301. We will encounter formulas involving variables and functions again
  302. when we discuss the algebra and calculus features of the Calculator.
  303. X
  304. @node Undo Tutorial, Modes Tutorial, Algebraic Tutorial, Basic Tutorial
  305. @subsection Undo and Redo
  306. X
  307. @noindent
  308. If you make a mistake, you can usually correct it by pressing shift-@kbd{U},
  309. the ``undo'' command.  First, clear the stack (@kbd{M-0 DEL}) and exit
  310. and restart Calc (@kbd{M-# M-# M-# M-#}) to make sure things start off
  311. with a clean slate.  Now:
  312. X
  313. @group
  314. @smallexample
  315. 1:  2          2:  2          1:  8          2:  2          1:  6
  316. X    .          1:  3              .          1:  3              .
  317. X                   .                             .
  318. X
  319. X   2 RET           3              ^              U              *
  320. @end smallexample
  321. @end group
  322. X
  323. You can undo any number of times.  Calc keeps a complete record of
  324. all you have done since you last opened the Calc window.  After the
  325. above example, you could type:
  326. X
  327. @group
  328. @smallexample
  329. 1:  6          2:  2          1:  2              .              .
  330. X    .          1:  3              .
  331. X                   .
  332. X                                                             (error)
  333. X                   U              U              U              U
  334. @end smallexample
  335. @end group
  336. X
  337. You can also type @kbd{D} to ``redo'' a command that you have undone
  338. mistakenly.
  339. X
  340. @group
  341. @smallexample
  342. X    .          1:  2          2:  2          1:  6          1:  6
  343. X                   .          1:  3              .              .
  344. X                                  .
  345. X                                                             (error)
  346. X                   D              D              D              D
  347. @end smallexample
  348. @end group
  349. X
  350. @noindent
  351. It was not possible to redo past the @cite{6}, since that was placed there
  352. by something other than an undo command.
  353. X
  354. @cindex Time travel
  355. You can think of undo and redo as a sort of ``time machine.''  Press
  356. @kbd{U} to go backward in time, @kbd{D} to go forward.  If you go
  357. backward and do something (like @kbd{*}) then, as any science fiction
  358. reader knows, you have changed your future and you cannot go forward
  359. again.  Thus, the inability to redo past the @cite{6} even though there
  360. was an earlier undo command.
  361. X
  362. You can always recall an earlier result using the Trail.  We've ignored
  363. the trail so far, but it has been faithfully recording everything we
  364. did since we loaded the Calculator.  If the Trail is not displayed,
  365. press @kbd{t d} now to turn it on.
  366. X
  367. Let's try grabbing an earlier result.  The @cite{8} we computed was
  368. undone by a @kbd{U} command, and was lost even to Redo when we pressed
  369. @kbd{*}, but it's still there in the trail.  There should be a little
  370. @samp{>} arrow (the @dfn{trail pointer}) resting on the last trail
  371. entry.  If there isn't, press @kbd{t ]} to reset the trail pointer.
  372. Now, press @w{@kbd{t p}} to move the arrow onto the line containing
  373. @cite{8}, and press @w{@kbd{t y}} to ``yank'' that number back onto the
  374. stack.
  375. X
  376. If you press @kbd{t ]} again, you will see that even our Yank command
  377. went into the trail.
  378. X
  379. Let's go further back in time.  Earlier in the tutorial we computed
  380. a huge integer using the formula @samp{2^3^4}.  We don't remember
  381. what it was, but the first digits were ``241''.  Press @kbd{t r}
  382. (which stands for trail-search-reverse), then type @kbd{241}.
  383. The trail cursor will jump back to the next previous occurrence of
  384. the string ``241'' in the trail.  This is just a regular Emacs
  385. incremental search; you can now press @kbd{C-s} or @kbd{C-r} to
  386. continue the search forwards or backwards as you like.
  387. X
  388. To finish the search, press @key{RET}.  This halts the incremental
  389. search and leaves the trail pointer at the thing we found.  Now we
  390. can type @kbd{t y} to yank that number onto the stack.  If we hadn't
  391. remembered the ``241'', we could simply have searched for @kbd{2^3^4},
  392. then pressed @kbd{@key{RET} t n} to halt and then move to the next item.
  393. X
  394. You may have noticed that all the trail-related commands begin with
  395. the letter @kbd{t}.  (The store-and-recall commands, on the other hand,
  396. all began with @kbd{s}.)  Calc has so many commands that there aren't
  397. enough keys for all of them, so various commands are grouped into
  398. two-letter sequences where the first letter is called the @dfn{prefix}
  399. key.  If you type a prefix key by accident, you can press @kbd{C-g}
  400. to cancel it.  (In fact, you can press @kbd{C-g} to cancel almost
  401. anything in Emacs.)  To get help on a prefix key, press that key
  402. followed by @kbd{?}.  Some prefixes have several lines of help,
  403. so you need to press @kbd{?} repeatedly to see them all.
  404. X
  405. Try pressing @kbd{t ?} now.  You will see a line of the form,
  406. X
  407. @smallexample
  408. trail/time: Display; Fwd, Back; Next, Prev, Here, [, ]; Yank:  [MORE]  t-
  409. @end smallexample
  410. X
  411. @noindent
  412. The word ``trail'' indicates that the @kbd{t} prefix key contains
  413. trail-related commands.  Each entry on the line shows one command,
  414. with a single capital letter showing which letter you press to get
  415. that command.  We have used @kbd{t n}, @kbd{t p}, @kbd{t ]}, and
  416. @kbd{t y} so far.  The @samp{[MORE]} means you can press @kbd{?}
  417. again to see more @kbd{t}-prefix comands.  Notice that the commands
  418. are roughly divided (by semicolons) into related groups.
  419. X
  420. When you are in the help display for a prefix key, the prefix is
  421. still active.  If you press another key, like @kbd{y} for example,
  422. it will be interpreted as a @kbd{t y} command.  If all you wanted
  423. was to look at the help messages, press @kbd{C-g} afterwards to cancel
  424. the prefix.
  425. X
  426. One more way to correct an error is by editing the stack entries.
  427. The actual Stack buffer is marked read-only and must not be edited
  428. directly, but you can press @kbd{`} (the backquote or accent grave)
  429. to edit a stack entry.
  430. X
  431. Try entering @samp{3.141439} now.  If this is supposed to represent
  432. @c{$\pi$}
  433. @cite{pi}, it's got several errors.  Press @kbd{`} to edit this number.
  434. Now use the normal Emacs cursor motion and editing keys to change
  435. the second 4 to a 5, and to transpose the 3 and the 9.  When you
  436. press @key{RET}, the number on the stack will be replaced by your
  437. new number.  This works for formulas, vectors, and all other types
  438. of values you can put on the stack.  The @kbd{`} key also works
  439. during entry of a number or algebraic formula.
  440. X
  441. @node Modes Tutorial, , Undo Tutorial, Basic Tutorial
  442. @subsection Mode-Setting Commands
  443. X
  444. @noindent
  445. Calc has many types of @dfn{modes} that affect the way it interprets
  446. your commands or the way it displays data.  We have already seen one
  447. mode, namely algebraic mode.  There are many others, too; we'll
  448. try some of the most common ones here.
  449. X
  450. Perhaps the most fundamental mode in Calc is the current @dfn{precision}.
  451. Notice the @samp{12} on the Calc window's mode line:
  452. X
  453. @smallexample
  454. --%%-Calc: 12 Deg       (Calculator)----All------
  455. @end smallexample
  456. X
  457. @noindent
  458. Most of the symbols there are Emacs things you don't need to worry
  459. about, but the @samp{12} and the @samp{Deg} are mode indicators.
  460. The @samp{12} means that calculations should always be carried to
  461. 12 significant figures.  That is why, when we type @kbd{1 @key{RET} 7 /},
  462. we get @cite{0.142857142857} with exactly 12 digits, not counting
  463. leading and trailing zeros.
  464. X
  465. You can set the precision to anything you like by pressing @kbd{p},
  466. then entering a suitable number.  Try pressing @kbd{p 30 @key{RET}},
  467. then doing @kbd{1 @key{RET} 7 /} again:
  468. X
  469. @group
  470. @smallexample
  471. 1:  0.142857142857
  472. 2:  0.142857142857142857142857142857
  473. X    .
  474. @end smallexample
  475. @end group
  476. X
  477. Although the precision can be set arbitrarily high, Calc always
  478. has to have @emph{some} value for the current precision.  After
  479. all, the true value @cite{1/7} is an infinitely repeating decimal;
  480. Calc has to stop somewhere.
  481. X
  482. Of course, calculations are slower the more digits you request.
  483. Press @w{@kbd{p 12}} now to set the precision back down to the default.
  484. X
  485. Calculations always use the current precision.  For example, even
  486. though we have a 30-digit value for @cite{1/7} on the stack, if
  487. we use it in a calculation in 12-digit mode it will be rounded
  488. down to 12 digits before it is used.  Try it; press @key{RET} to
  489. duplicate the number, then @w{@kbd{1 +}}.  Notice that the @key{RET}
  490. key didn't round the number, because it doesn't do any calculation.
  491. But the instant we pressed @kbd{+}, the number was rounded down.
  492. X
  493. @group
  494. @smallexample
  495. 1:  0.142857142857
  496. 2:  0.142857142857142857142857142857
  497. 3:  1.14285714286
  498. X    .
  499. @end smallexample
  500. @end group
  501. X
  502. @noindent
  503. In fact, since we added a digit on the left, we had to lose one
  504. digit on the right from even the 12-digit value of @cite{1/7}.
  505. X
  506. How did we get more than 12 digits when we computed @samp{2^3^4}?  The
  507. answer is that Calc makes a distinction between @dfn{integers} and
  508. @dfn{floating-point} numbers, or @dfn{floats}.  An integer is a number
  509. that does not contain a decimal point.  There is no such thing as an
  510. ``infinitely repeating fraction integer,'' so Calc doesn't have to limit
  511. itself.  If you asked for @samp{2^10000} (don't try this!), you would
  512. have to wait a long time but you would eventually get an exact answer.
  513. If you ask for @samp{2.^10000}, you will quickly get an answer which is
  514. correct only to 12 places.  The decimal point tells Calc that it should
  515. use floating-point arithmetic to get the answer, not exact integer
  516. arithmetic.
  517. X
  518. You can use the @kbd{F} (@code{calc-floor}) command to convert a
  519. floating-point value to an integer, and @kbd{c f} (@code{calc-float})
  520. to convert an integer to floating-point form.
  521. X
  522. Let's try entering that last calculation:
  523. X
  524. @group
  525. @smallexample
  526. 1:  2.         2:  2.         1:  1.99506311689e3010
  527. X    .          1:  10000          .
  528. X                   .
  529. X
  530. X  2.0 RET          10000 RET      ^
  531. @end smallexample
  532. @end group
  533. X
  534. @noindent
  535. Notice the letter @samp{e} in there.  It represents ``times ten to the
  536. power of,'' and is used by Calc automatically whenever writing the
  537. number out fully would introduce more extra zeros than you probably
  538. want to see.  You can enter numbers in this notation, too.
  539. X
  540. @group
  541. @smallexample
  542. 1:  2.         2:  2.         1:  1.99506311678e3010
  543. X    .          1:  10000.         .
  544. X                   .
  545. X
  546. X  2.0 RET          1e4 RET        ^
  547. @end smallexample
  548. @end group
  549. X
  550. @cindex Round-off errors
  551. @noindent
  552. Hey, the answer is different!  Look closely at the middle columns
  553. of the two examples.  In the first, the stack contained the
  554. exact integer @cite{10000}, but in the second it contained
  555. a floating-point value with a decimal point.  When you raise a
  556. number to an integer power, Calc uses repeated squaring and
  557. multiplication to get the answer.  When you use a floating-point
  558. power, Calc uses logarithms and exponentials.  As you can see,
  559. a slight error crept in during one of these methods.  Which
  560. one should we trust?  Let's raise the precision a bit and find
  561. out:
  562. X
  563. @group
  564. @smallexample
  565. X    .          1:  2.         2:  2.         1:  1.995063116880828e3010
  566. X                   .          1:  10000.         .
  567. X                                  .
  568. X
  569. X p 16 RET        2. RET           1e4            ^    p 12 RET
  570. @end smallexample
  571. @end group
  572. X
  573. @noindent
  574. Presumably, it doesn't matter whether we do this higher-precision
  575. calculation using an integer or floating-point power, since we
  576. have added enough ``guard digits'' to trust the first 12 digits
  577. no matter what.  And the verdict is@dots{}  Integer powers were more
  578. accurate; in fact, the result was only off by one unit in the
  579. last place.
  580. X
  581. @cindex Guard digits
  582. Calc does many of its internal calculations to a slightly higher
  583. precision, but it doesn't always bump the precision up enough.
  584. In each case, Calc added about two digits of precision during
  585. its calculation and then rounded back down to 12 digits
  586. afterward.  In one case, it was enough; in the the other, it
  587. wasn't.  If you really need @var{x} digits of precision, it
  588. never hurts to do the calculation with a few extra guard digits.
  589. X
  590. What if we want guard digits but don't want to look at them?
  591. We can set the @dfn{float format}.  Calc supports four major
  592. formats for floating-point numbers, called @dfn{normal},
  593. @dfn{fixed-point}, @dfn{scientific notation}, and @dfn{engineering
  594. notation}.  You get them by pressing @w{@kbd{d n}}, @kbd{d f},
  595. @kbd{d s}, and @kbd{d e}, respectively.  In each case, you can
  596. supply a numeric prefix argument which says how many digits
  597. should be displayed.  As an example, let's put a few numbers
  598. onto the stack and try some different display modes.  First,
  599. use @kbd{M-0 DEL} to clear the stack, then enter the four
  600. numbers shown here:
  601. X
  602. @group
  603. @smallexample
  604. 4:  12345      4:  12345      4:  12345      4:  12345      4:  12345
  605. 3:  12345.     3:  12300.     3:  1.2345e4   3:  1.23e4     3:  12345.000
  606. 2:  123.45     2:  123.       2:  1.2345e2   2:  1.23e2     2:  123.450
  607. 1:  12.345     1:  12.3       1:  1.2345e1   1:  1.23e1     1:  12.345
  608. X    .              .              .              .              .
  609. X
  610. X   d n          M-3 d n          d s          M-3 d s        M-3 d f
  611. @end smallexample
  612. @end group
  613. X
  614. @noindent
  615. Notice that when we typed @kbd{M-3 d n}, the numbers were rounded down
  616. to three significant digits, but then when we typed @kbd{d s} all
  617. five significant figures reappeared.  The float format does not
  618. affect how numbers are stored, it only affects how they are
  619. displayed.  Only the current precision governs the actual rounding
  620. of numbers in the Calculator's memory.
  621. X
  622. Engineering notation, not shown here, is like scientific notation
  623. except the exponent (the power-of-ten part) is always adjusted to be
  624. a multiple of three (as in ``kilo,'' ``micro,'' etc.).  As a result
  625. there will be one, two, or three digits before the decimal point.
  626. X
  627. Whenever you change a display-related mode, Calc redraws everything
  628. in the stack.  This may be slow if there are many things on the stack,
  629. so Calc allows you to type shift-@kbd{H} before any mode command to
  630. prevent it from updating the stack.  Anything Calc displays after the
  631. mode-changing command will appear in the new format.
  632. X
  633. @group
  634. @smallexample
  635. 4:  12345      4:  12345      4:  12345      4:  12345      4:  12345
  636. 3:  12345.000  3:  12345.000  3:  12345.000  3:  1.2345e4   3:  12345.
  637. 2:  123.450    2:  123.450    2:  1.2345e1   2:  1.2345e1   2:  123.45
  638. 1:  12.345     1:  1.2345e1   1:  1.2345e2   1:  1.2345e2   1:  12.345
  639. X    .              .              .              .              .
  640. X
  641. X    H d s          DEL U          TAB            d SPC          d n
  642. @end smallexample
  643. @end group
  644. X
  645. @noindent
  646. Here the @kbd{H d s} command changes to scientific notation but without
  647. updating the screen.  Deleting the top stack entry and undoing it back
  648. causes it to show up in the new format; swapping the top two stack
  649. entries reformats both entries.  The @kbd{d SPC} command refreshes the
  650. whole stack.  The @kbd{d n} command changes back to the normal float
  651. format; since it doesn't have an @kbd{H} prefix, it also updates all
  652. the stack entries to be in @kbd{d n} format.
  653. X
  654. Notice that the integer @cite{12345} was not affected by any
  655. of the float formats.  Integers are integers, and are always
  656. displayed exactly.
  657. X
  658. @cindex Large numbers, readability
  659. Large integers have their own problems.  Let's look back at
  660. the result of @kbd{2^3^4}.
  661. X
  662. @example
  663. 2417851639229258349412352
  664. @end example
  665. X
  666. @noindent
  667. Quick---how many digits does this have?  Try typing @kbd{d g}:
  668. X
  669. @example
  670. 2,417,851,639,229,258,349,412,352
  671. @end example
  672. X
  673. @noindent
  674. Now how many digits does this have?  It's much easier to tell!
  675. We can actually group digits into clumps of any size.  Some
  676. people prefer @kbd{M-5 d g}:
  677. X
  678. @example
  679. 24178,51639,22925,83494,12352
  680. @end example
  681. X
  682. Let's see what happens to floating-point numbers when they are grouped.
  683. First, type @kbd{p 25 @key{RET}} to make sure we have enough precision
  684. to get ourselves into trouble.  Now, type @kbd{1e13 /}:
  685. X
  686. @example
  687. 24,17851,63922.9258349412352
  688. @end example
  689. X
  690. @noindent
  691. The integer part is grouped but the fractional part isn't.  Now try
  692. @kbd{M-- M-5 d g} (that's meta-minus-sign, meta-five):
  693. X
  694. @example
  695. 24,17851,63922.92583,49412,352
  696. @end example
  697. X
  698. If you find it hard to tell the decimal point from the commas, try
  699. changing the grouping character to a space with @kbd{d , @key{SPC}}:
  700. X
  701. @example
  702. 24 17851 63922.92583 49412 352
  703. @end example
  704. X
  705. Type @kbd{d , ,} to restore the normal grouping character, then
  706. @kbd{d g} again to turn grouping off.  Also, press @kbd{p 12} to
  707. restore the default precision.
  708. X
  709. Press @kbd{U} enough times to get the original big integer back.
  710. (Notice that @kbd{U} does not undo each mode-setting command; if
  711. you want to undo a mode-setting command, you have to do it yourself.)
  712. Now, type @kbd{d r 16 @key{RET}}:
  713. X
  714. @example
  715. 16#200000000000000000000
  716. @end example
  717. X
  718. @noindent
  719. The number is now displayed in @dfn{hexadecimal}, or ``base-16'' form.
  720. Suddenly it looks pretty simple; this should be no surprise, since we
  721. got this number by computing a power of two, and 16 is a power of 2.
  722. In fact, we can use @w{@kbd{d r 2 @key{RET}}} to see it in actual binary
  723. form:
  724. X
  725. @example
  726. 2#1000000000000000000000000000000000000000000000000000000 @dots{}
  727. @end example
  728. X
  729. @noindent
  730. We don't have enough space here to show all the zeros!  They won't
  731. fit on a typical screen, either, so you will have to use horizontal
  732. scrolling to see them all.  Press @kbd{<} and @kbd{>} to scroll the
  733. stack window left and right by half its width.  Another way to view
  734. something large is to press @kbd{`} (back-quote) to edit the top of
  735. stack in a separate window.  (Press @kbd{M-# M-#} when you are done.)
  736. X
  737. You can enter non-decimal numbers using the @kbd{#} symbol, too.
  738. Let's see what the hexadecimal number @samp{5FE} looks like in
  739. binary.  Type @kbd{16#5FE} (the letters can be typed in upper or
  740. lower case; they will always appear in upper case).  It will also
  741. help to turn grouping on with @kbd{d g}:
  742. X
  743. @example
  744. 2#101,1111,1110
  745. @end example
  746. X
  747. Notice that @kbd{d g} groups by fours by default if the display radix
  748. is binary or hexadecimal, but by threes if it is decimal, octal, or any
  749. other radix.
  750. X
  751. Now let's see that number in decimal; type @kbd{d r 10}:
  752. X
  753. @example
  754. 1,534
  755. @end example
  756. X
  757. Numbers are not @emph{stored} with any particular radix attached.  They're
  758. just numbers; they can be entered in any radix, and are always displayed
  759. in whatever radix you've chosen with @kbd{d r}.  The current radix applies
  760. to integers, fractions, and floats.
  761. X
  762. (@bullet{}) @strong{Exercise 1.}  Your friend Joe tried to enter one-third
  763. as @samp{3#0.1} in @kbd{d r 3} mode with a precision of 12.  He got
  764. @samp{3#0.0222222...} (with 25 2's) in the display.  When he multiplied
  765. that by three, he got @samp{3#0.222222...} instead of the expected
  766. @samp{3#1}.  Next, Joe entered @samp{3#0.2} and, to his great relief,
  767. saw @samp{3#0.2} on the screen.  But when he typed @kbd{2 /}, he got
  768. @samp{3#0.10000001} (some zeros omitted).  What's going on here?
  769. @xref{Modes Answer 1, 1}. (@bullet{})
  770. X
  771. (@bullet{}) @strong{Exercise 2.}  Scientific notation works in non-decimal
  772. modes in the natural way (the exponent is a power of the radix instead of
  773. a power of ten, although the exponent itself is always written in decimal).
  774. Thus @samp{8#1.23e3 = 8#1230.0}.  Suppose we have the hexadecimal number
  775. @samp{f.e8f} times 16 to the 15th power:  We write @samp{16#f.e8fe15}.
  776. What is wrong with this picture?  What could we write instead that would
  777. work better?  @xref{Modes Answer 2, 2}. (@bullet{})
  778. X
  779. The @kbd{m} prefix key has another set of modes, relating to the way
  780. Calc interprets your inputs and does computations.  Whereas @kbd{d}-prefix
  781. modes generally affect the way thing look, @kbd{m}-prefix modes affect
  782. the way they are actually computed.
  783. X
  784. The most popular @kbd{m}-prefix mode is the @dfn{angular mode}.  Notice
  785. the @samp{Deg} indicator in the mode line.  This means that if you use
  786. a command that interprets a number as an angle, it will assume the
  787. angle is measured in degrees.  For example,
  788. X
  789. @group
  790. @smallexample
  791. 1:  45         1:  0.707106781187   1:  0.500000000001    1:  0.5
  792. X    .              .                    .                     .
  793. X
  794. X    45             S                    2 ^                   c 1
  795. @end smallexample
  796. @end group
  797. X
  798. @noindent
  799. The shift-@kbd{S} command computes the sine of an angle.  The sine
  800. of 45 degrees is @c{$\sqrt{2}/2$}
  801. @cite{sqrt(2)/2}; squaring this yields @cite{2/4 = 0.5}.
  802. However, there has been a slight roundoff error because the
  803. representation of @c{$\sqrt{2}/2$}
  804. @cite{sqrt(2)/2} wasn't exact.  The @kbd{c 1}
  805. command is a handy way to clean up numbers in this case; it
  806. temporarily reduces the precision by one digit while it
  807. re-rounds the number on the top of the stack.
  808. X
  809. (@bullet{}) @strong{Exercise 3.}  Your friend Joe computed the sine
  810. of 45 degrees as shown above, then, hoping to avoid an inexact
  811. result, he increased the precision to 16 digits before squaring.
  812. What happened?  @xref{Modes Answer 3, 3}. (@bullet{})
  813. X
  814. To do this calculation in radians, we would type @kbd{m r} first.
  815. (The indicator changes to @samp{Rad}.)  45 degrees corresponds to
  816. @c{$\pi\over4$}
  817. @cite{pi/4} radians.  To get @c{$\pi$}
  818. @cite{pi}, press the @kbd{P} key.  (Once
  819. again, this is a shifted capital @kbd{P}.  Remember, unshifted
  820. @kbd{p} sets the precision.)
  821. X
  822. @group
  823. @smallexample
  824. 1:  3.14159265359   1:  0.785398163398   1:  0.707106781187
  825. X    .                   .                .
  826. X
  827. X    P                   4 /       m r    S
  828. @end smallexample
  829. @end group
  830. X
  831. Likewise, inverse trigonometric functions generate results in
  832. either radians or degrees, depending on the current angular mode.
  833. X
  834. @group
  835. @smallexample
  836. 1:  0.707106781187   1:  0.785398163398   1:  45.
  837. X    .                    .                    .
  838. X
  839. X    .5 Q        m r      I S        m d       U I S
  840. @end smallexample
  841. @end group
  842. X
  843. @noindent
  844. Here we compute the Inverse Sine of @c{$\sqrt{0.5}$}
  845. @cite{sqrt(0.5)}, first in
  846. radians, then in degrees.
  847. X
  848. Use @kbd{c d} and @kbd{c r} to convert a number from radians to degrees
  849. and vice-versa.
  850. X
  851. @group
  852. @smallexample
  853. 1:  45         1:  0.785398163397     1:  45.
  854. X    .              .                      .
  855. X
  856. X    45             c r                    c d
  857. @end smallexample
  858. @end group
  859. X
  860. Another interesting mode is @dfn{fraction mode}.  Normally,
  861. dividing two integers produces a floating-point result if the
  862. quotient can't be expressed as an exact integer.  Fraction mode
  863. causes integer division to produce a fraction, i.e., a rational
  864. number, instead.
  865. X
  866. @group
  867. @smallexample
  868. 2:  12         1:  1.33333333333    1:  4:3
  869. 1:  9              .                    .
  870. X    .
  871. X
  872. X 12 RET 9          /          m f       U /      m f
  873. @end smallexample
  874. @end group
  875. X
  876. @noindent
  877. In the first case, we get an approximate floating-point result.
  878. In the second case, we get an exact fractional result (four-thirds).
  879. X
  880. You can enter a fraction at any time using @kbd{:} notation.
  881. (Calc uses @kbd{:} instead of @kbd{/} as the fraction separator
  882. because @kbd{/} is already used to divide the top two stack
  883. elements.)  Calculations involving fractions will always
  884. produce exact fractional results; fraction mode only says
  885. what to do when dividing two integers.
  886. X
  887. @cindex Fractions vs. floats
  888. @cindex Floats vs. fractions
  889. (@bullet{}) @strong{Exercise 4.}  If fractional arithmetic is exact,
  890. why would you ever use floating-point numbers instead?
  891. @xref{Modes Answer 4, 4}. (@bullet{})
  892. X
  893. Typing @kbd{m f} doesn't change any existing values in the stack.
  894. In the above example, we had to Undo the division and do it over
  895. again when we changed to fraction mode.  But if you use the
  896. evaluates-to operator you can get commands like @kbd{m f} to
  897. recompute for you.
  898. X
  899. @group
  900. @smallexample
  901. 1:  12 / 9 => 1.33333333333    1:  12 / 9 => 1.333    1:  12 / 9 => 4:3
  902. X    .                              .                      .
  903. X
  904. X   ' 12/9 => RET                   p 4 RET                m f
  905. @end smallexample
  906. @end group
  907. X
  908. @noindent
  909. In this example, the righthand side of the @samp{=>} operator
  910. on the stack is recomputed when we change the precision, then
  911. again when we change to fraction mode.  All @samp{=>} expressions
  912. on the stack are recomputed every time you change any mode that
  913. might affect their values.
  914. X
  915. @node Arithmetic Tutorial, Vector/Matrix Tutorial, Basic Tutorial, Tutorial
  916. @section Arithmetic Tutorial
  917. X
  918. @noindent
  919. In this section, we explore the arithmetic and scientific functions
  920. available in the Calculator.
  921. X
  922. The standard arithmetic commands are @kbd{+}, @kbd{-}, @kbd{*}, @kbd{/},
  923. and @kbd{^}.  Each normally takes two numbers from the top of the stack
  924. and pushes back a result.  The @kbd{n} and @kbd{&} keys perform
  925. change-sign and reciprocal operations, respectively.
  926. X
  927. @group
  928. @smallexample
  929. 1:  5          1:  0.2        1:  5.         1:  -5.        1:  5.
  930. X    .              .              .              .              .
  931. X
  932. X    5              &              &              n              n
  933. @end smallexample
  934. @end group
  935. X
  936. @cindex Binary operators
  937. You can apply a ``binary operator'' like @kbd{+} across any number of
  938. stack entries by giving it a numeric prefix.  You can also apply it
  939. pairwise to several stack elements along with the top one if you use
  940. a negative prefix.
  941. X
  942. @group
  943. @smallexample
  944. 3:  2          1:  9          3:  2          4:  2          3:  12
  945. 2:  3              .          2:  3          3:  3          2:  13
  946. 1:  4                         1:  4          2:  4          1:  14
  947. X    .                             .          1:  10             .
  948. X                                                 .
  949. X
  950. 2 RET 3 RET 4     M-3 +           U              10          M-- M-3 +
  951. @end smallexample
  952. @end group
  953. X
  954. @cindex Unary operators
  955. You can apply a ``unary operator'' like @kbd{&} to the top @var{n}
  956. stack entries with a numeric prefix, too.
  957. X
  958. @group
  959. @smallexample
  960. 3:  2          3:  0.5                3:  0.5
  961. 2:  3          2:  0.333333333333     2:  3.
  962. 1:  4          1:  0.25               1:  4.
  963. X    .              .                      .
  964. X
  965. 2 RET 3 RET 4     M-3 &                  M-2 &
  966. @end smallexample
  967. @end group
  968. X
  969. Notice that the results here are left in floating-point form.
  970. We can convert them back to integers by pressing @kbd{F}, the
  971. ``floor'' function.  This function rounds down to the next lower
  972. integer.  There is also @kbd{R}, which rounds to the nearest
  973. integer.
  974. X
  975. @group
  976. @smallexample
  977. 7:  2.         7:  2          7:  2
  978. 6:  2.4        6:  2          6:  2
  979. 5:  2.5        5:  2          5:  3
  980. 4:  2.6        4:  2          4:  3
  981. 3:  -2.        3:  -2         3:  -2
  982. 2:  -2.4       2:  -3         2:  -2
  983. 1:  -2.6       1:  -3         1:  -3
  984. X    .              .              .
  985. X
  986. X                  M-7 F        U M-7 R
  987. @end smallexample
  988. @end group
  989. X
  990. Since dividing-and-flooring (i.e., ``integer quotient'') is such a
  991. common operation, Calc provides a special command for that purpose, the
  992. backslash @kbd{\}.  Another common arithmetic operator is @kbd{%}, which
  993. computes the remainder that would arise from a @kbd{\} operation, i.e.,
  994. the ``modulo'' of two numbers.  For example,
  995. X
  996. @group
  997. @smallexample
  998. 2:  1234       1:  12         2:  1234       1:  34
  999. 1:  100            .          1:  100            .
  1000. X    .                             .
  1001. X
  1002. 1234 RET 100       \              U              %
  1003. @end smallexample
  1004. @end group
  1005. X
  1006. These commands actually work for any real numbers, not just integers.
  1007. X
  1008. @group
  1009. @smallexample
  1010. 2:  3.1415     1:  3          2:  3.1415     1:  0.1415
  1011. 1:  1              .          1:  1              .
  1012. X    .                             .
  1013. X
  1014. 3.1415 RET 1       \              U              %
  1015. @end smallexample
  1016. @end group
  1017. X
  1018. (@bullet{}) @strong{Exercise 1.}  The @kbd{\} command would appear to be a
  1019. frill, since you could always do the same thing with @kbd{/ F}.  Think
  1020. of a situation where this is not true---@kbd{/ F} would be inadequate.
  1021. Now think of a way you could get around the problem if Calc didn't
  1022. provide a @kbd{\} command.  @xref{Arithmetic Answer 1, 1}. (@bullet{})
  1023. X
  1024. We've already seen the @kbd{Q} (square root) and @kbd{S} (sine)
  1025. commands.  Other commands along those lines are @kbd{C} (cosine),
  1026. @kbd{T} (tangent), @kbd{E} (@cite{e^x}) and @kbd{L} (natural
  1027. logarithm).  These can be modified by the @kbd{I} (inverse) and
  1028. @kbd{H} (hyperbolic) prefix keys.
  1029. X
  1030. Let's compute the sine and cosine of an angle, and verify the
  1031. identity @c{$\sin^2x + \cos^2x = 1$}
  1032. @cite{sin(x)^2 + cos(x)^2 = 1}.  We'll
  1033. arbitrarily pick @i{-64} degrees as a good value for @cite{x}.  With
  1034. the angular mode set to degrees (type @w{@kbd{m d}}), do:
  1035. X
  1036. @group
  1037. @smallexample
  1038. 2:  -64        2:  -64        2:  -0.89879   2:  -0.89879   1:  1.
  1039. 1:  -64        1:  -0.89879   1:  -64        1:  0.43837        .
  1040. X    .              .              .              .
  1041. X
  1042. X 64 n RET RET      S             TAB             C             f h
  1043. @end smallexample
  1044. @end group
  1045. X
  1046. @noindent
  1047. (For brevity, we're showing only five digits of the results here.
  1048. You can of course do these calculations to any precision you like.)
  1049. X
  1050. Remember, @kbd{f h} is the @code{calc-hypot}, or square-root of sum
  1051. of squares, command.
  1052. X
  1053. Another identity is @c{$\displaystyle\tan x = {\sin x \over \cos x}$}
  1054. @cite{tan(x) = sin(x) / cos(x)}.
  1055. @group
  1056. @smallexample
  1057. X
  1058. 2:  -0.89879   1:  -2.0503    1:  -64.
  1059. 1:  0.43837        .              .
  1060. X    .
  1061. X
  1062. X    U              /             I T
  1063. @end smallexample
  1064. @end group
  1065. X
  1066. A physical interpretation of this calculation is that if you move
  1067. @cite{0.89879} units downward and @cite{0.43837} units to the right,
  1068. your direction of motion is @i{-64} degrees from horizontal.  Suppose
  1069. we move in the opposite direction, up and to the left:
  1070. X
  1071. @group
  1072. @smallexample
  1073. 2:  -0.89879   2:  0.89879    1:  -2.0503    1:  -64.
  1074. 1:  0.43837    1:  -0.43837       .              .
  1075. X    .              .
  1076. X
  1077. X   U U           M-2 n            /             I T
  1078. @end smallexample
  1079. @end group
  1080. X
  1081. @noindent
  1082. How can the angle be the same?  The answer is that the @kbd{/} operation
  1083. loses information about the signs of its inputs.  Because the quotient
  1084. is negative, we know exactly one of the inputs was negative, but we
  1085. can't tell which one.  There is an @kbd{f T} [@code{arctan2}] function which
  1086. computes the inverse tangent of the quotient of a pair of numbers.
  1087. Since you feed it the two original numbers, it has enough information
  1088. to give you a full 360-degree answer.
  1089. X
  1090. @group
  1091. @smallexample
  1092. 2:  0.89879    1:  116.       3:  116.       2:  116.       1:  180.
  1093. 1:  -0.43837       .          2:  -0.89879   1:  -64.           .
  1094. X    .                         1:  0.43837        .
  1095. X                                  .
  1096. X
  1097. X   U U            f T         M-RET M-2 n       f T             -
  1098. @end smallexample
  1099. @end group
  1100. X
  1101. @noindent
  1102. The resulting angles differ by 180 degrees; in other words, they
  1103. point in opposite directions, just as we would expect.
  1104. X
  1105. The @key{META}-@key{RET} we used in the third step is the
  1106. ``last-arguments'' command.  It is sort of like Undo, except that it
  1107. restores the arguments of the last command to the stack without removing
  1108. the command's result.  It is useful in situations like this one,
  1109. where we need to do several operations on the same inputs.  We could
  1110. have accomplished the same thing by using @kbd{M-2 @key{RET}} to duplicate
  1111. the top two stack elements right after the @kbd{U U}, then a pair of
  1112. @kbd{M-@key{TAB}} commands to cycle the 116 up around the duplicates.
  1113. X
  1114. A similar identity is supposed to hold for hyperbolic sines and cosines,
  1115. except that it is the @emph{difference}
  1116. @c{$\cosh^2x - \sinh^2x$}
  1117. @cite{cosh(x)^2 - sinh(x)^2} that always equals one.
  1118. Let's try to verify this identity.@refill
  1119. X
  1120. @group
  1121. @smallexample
  1122. 2:  -64        2:  -64        2:  -64        2:  9.7192e54  2:  9.7192e54
  1123. 1:  -64        1:  -3.1175e27 1:  9.7192e54  1:  -64        1:  9.7192e54
  1124. X    .              .              .              .              .
  1125. X
  1126. X 64 n RET RET      H C           2 ^            TAB           H S 2 ^
  1127. @end smallexample
  1128. @end group
  1129. X
  1130. @noindent
  1131. Something's obviously wrong, because when we subtract these numbers
  1132. the answer will clearly be zero!  But if you think about it, if these
  1133. numbers @emph{did} differ by one, it would be in the 55th decimal
  1134. place.  The difference we seek has been lost entirely to roundoff
  1135. error.
  1136. X
  1137. We could verify this hypothesis by doing the actual calculation with,
  1138. say, 60 decimal places of precision.  This will be slow, but not
  1139. enormously so.  Try it if you wish; sure enough, the answer is
  1140. 0.99999, reasonably close to 1.
  1141. X
  1142. Of course, a more reasonable way to verify the identity is to use
  1143. a more reasonable value for @cite{x}!
  1144. X
  1145. Some Calculator commands use the Hyperbolic prefix for other purposes.
  1146. The logarithm and exponential functions, for example, work to the base
  1147. @cite{e} normally but use base-10 instead if you use the Hyperbolic
  1148. prefix.
  1149. X
  1150. @group
  1151. @smallexample
  1152. 1:  1000       1:  6.9077     1:  1000       1:  3
  1153. X    .              .              .              .
  1154. X
  1155. X    1000           L              U              H L
  1156. @end smallexample
  1157. @end group
  1158. X
  1159. @noindent
  1160. First, we mistakenly compute a natural logarithm.  Then we undo
  1161. and compute a common logarithm instead.
  1162. X
  1163. The @kbd{B} key computes a general base-@var{b} logarithm for any
  1164. value of @var{b}.
  1165. X
  1166. @group
  1167. @smallexample
  1168. 2:  1000       1:  3          1:  1000.      2:  1000.      1:  6.9077
  1169. 1:  10             .              .          1:  2.71828        .
  1170. X    .                                            .
  1171. X
  1172. X 1000 RET 10       B              H E            H P            B
  1173. @end smallexample
  1174. @end group
  1175. X
  1176. @noindent
  1177. Here we first use @kbd{B} to compute the base-10 logarithm, then use
  1178. the ``hyperbolic'' exponential as a cheap hack to recover the number
  1179. 1000, then use @kbd{B} again to compute the natural logarithm.  Note
  1180. that @kbd{P} with the hyperbolic prefix pushes the constant @cite{e}
  1181. onto the stack.
  1182. X
  1183. You may have noticed that both times we took the base-10 logarithm
  1184. of 1000, we got an exact integer result.  Calc always tries to give
  1185. an exact rational result for calculations involving rational numbers
  1186. where possible.  But when we used @kbd{H E}, the result was a
  1187. floating-point number for no apparent reason.  In fact, if we had
  1188. computed @kbd{10 @key{RET} 3 ^} we @emph{would} have gotten an
  1189. exact integer 1000.  But the @kbd{H E} command is rigged to generate
  1190. a floating-point result all of the time so that @kbd{1000 H E} will
  1191. not waste time computing a thousand-digit integer when all you
  1192. probably wanted was @samp{1e1000}.
  1193. X
  1194. (@bullet{}) @strong{Exercise 2.}  Find a pair of integer inputs to
  1195. the @kbd{B} command for which Calc could find an exact rational
  1196. result but doesn't.  @xref{Arithmetic Answer 2, 2}. (@bullet{})
  1197. X
  1198. The Calculator also has a set of functions relating to combinatorics
  1199. and statistics.  You may be familiar with the @dfn{factorial} function,
  1200. which computes the product of all the integers up to a given number.
  1201. X
  1202. @group
  1203. @smallexample
  1204. 1:  100        1:  93326215443...    1:  100.       1:  9.3326e157
  1205. X    .              .                     .              .
  1206. X
  1207. X    100            !                     U c f          !
  1208. @end smallexample
  1209. @end group
  1210. X
  1211. @noindent
  1212. The @kbd{c f} command converts the integer or fraction at the top
  1213. of the stack to floating-point format.  If you take the factorial
  1214. of a floating-point number, you get a floating-point result
  1215. accurate to the current precision.  But if you give @kbd{!} an
  1216. exact integer, you get an exact integer result (158 digits long
  1217. in this case).
  1218. X
  1219. If you take the factorial of a non-integer, Calc uses a generalized
  1220. factorial function defined in terms of Euler's Gamma function
  1221. @c{$\Gamma(n)$}
  1222. @cite{gamma(n)}
  1223. (which is itself available as the @kbd{f g} command).
  1224. X
  1225. @group
  1226. @smallexample
  1227. 3:  4.         3:  24.               1:  5.5        1:  52.342777847
  1228. 2:  4.5        2:  52.3427777847         .              .
  1229. 1:  5.         1:  120.
  1230. X    .              .
  1231. X
  1232. X                   M-3 !              M-0 DEL 5.5       f g
  1233. @end smallexample
  1234. @end group
  1235. X
  1236. @noindent
  1237. Here we verify the identity @c{$n! = \Gamma(n+1)$}
  1238. @cite{@var{n}!@: = gamma(@var{n}+1)}.
  1239. X
  1240. The binomial coefficient @var{n}-choose-@var{m}@c{ or $\displaystyle {n \choose m}$}
  1241. @asis{} is defined by
  1242. @c{$\displaystyle {n! \over m! \, (n-m)!}$}
  1243. @cite{n!@: / m!@: (n-m)!} for all reals @cite{n} and
  1244. @cite{m}.  The intermediate results in this formula can become quite
  1245. large even if the final result is small; the @kbd{k c} command computes
  1246. a binomial coefficient in a way that avoids large intermediate
  1247. values.
  1248. X
  1249. The @kbd{k} prefix key defines several common functions out of
  1250. combinatorics and number theory.  Here we compute the binomial
  1251. coefficient 30-choose-20, then determine its prime factorization.
  1252. X
  1253. @group
  1254. @smallexample
  1255. 2:  30         1:  30045015   1:  [3, 3, 5, 7, 11, 13, 23, 29]
  1256. 1:  20             .              .
  1257. X    .
  1258. X
  1259. X 30 RET 20         k c            k f
  1260. @end smallexample
  1261. @end group
  1262. X
  1263. @noindent
  1264. You can verify these prime factors by using @kbd{v u} to ``unpack''
  1265. this vector into 8 separate stack entries, then @kbd{M-8 *} to
  1266. multiply them back together.  The result is the original number,
  1267. 30045015.
  1268. X
  1269. @cindex Hash tables
  1270. Suppose a program you are writing needs a hash table with at least
  1271. 10000 entries.  It's best to use a prime number as the actual size
  1272. of a hash table.  Calc can compute the next prime number after 10000:
  1273. X
  1274. @group
  1275. @smallexample
  1276. 1:  10000      1:  10007      1:  9973
  1277. X    .              .              .
  1278. X
  1279. X    10000          k n            I k n
  1280. @end smallexample
  1281. @end group
  1282. X
  1283. @noindent
  1284. Just for kicks we've also computed the next prime @emph{less} than
  1285. 10000.
  1286. X
  1287. @c [fix-ref Financial Functions]
  1288. @xref{Financial Functions}, for a description of the Calculator
  1289. commands that deal with business and financial calculations (functions
  1290. like @code{pv}, @code{rate}, and @code{sln}).
  1291. X
  1292. @c [fix-ref Binary Number Functions]
  1293. @xref{Binary Functions}, to read about the commands for operating
  1294. on binary numbers (like @code{and}, @code{xor}, and @code{lsh}).
  1295. X
  1296. @node Vector/Matrix Tutorial, Types Tutorial, Arithmetic Tutorial, Tutorial
  1297. @section Vector/Matrix Tutorial
  1298. X
  1299. @noindent
  1300. A @dfn{vector} is a list of numbers or other Calc data objects.
  1301. Calc provides a large set of commands that operate on vectors.  Some
  1302. are familiar operations from vector analysis.  Others simply treat
  1303. a vector as a list of objects.
  1304. X
  1305. @menu
  1306. * Vector Analysis Tutorial::
  1307. * Matrix Tutorial::
  1308. * List Tutorial::
  1309. @end menu
  1310. X
  1311. @node Vector Analysis Tutorial, Matrix Tutorial, Vector/Matrix Tutorial, Vector/Matrix Tutorial
  1312. @subsection Vector Analysis
  1313. X
  1314. @noindent
  1315. If you add two vectors, the result is a vector of the sums of the
  1316. elements, taken pairwise.
  1317. X
  1318. @group
  1319. @smallexample
  1320. 1:  [1, 2, 3]     2:  [1, 2, 3]     1:  [8, 8, 3]
  1321. X    .             1:  [7, 6, 0]         .
  1322. X                      .
  1323. X
  1324. X    [1,2,3]  s 1      [7 6 0]  s 2      +
  1325. @end smallexample
  1326. @end group
  1327. X
  1328. @noindent
  1329. Note that we can separate the vector elements with either commas or
  1330. spaces.  This is true whether we are using incomplete vectors or
  1331. algebraic entry.  The @kbd{s 1} and @kbd{s 2} commands save these
  1332. vectors so we can easily reuse them later.
  1333. X
  1334. If you multiply two vectors, the result is the sum of the products
  1335. of the elements taken pairwise.  This is called the @dfn{dot product}
  1336. of the vectors.
  1337. X
  1338. @group
  1339. @smallexample
  1340. 2:  [1, 2, 3]     1:  19
  1341. 1:  [7, 6, 0]         .
  1342. X    .
  1343. X
  1344. X    r 1 r 2           *
  1345. @end smallexample
  1346. @end group
  1347. X
  1348. @cindex Dot product
  1349. The dot product of two vectors is equal to the product of their
  1350. lengths times the cosine of the angle between them.  (Here the vector
  1351. is interpreted as a line from the origin @cite{(0,0,0)} to the
  1352. specified point in three-dimensional space.)  The @kbd{A}
  1353. (absolute value) command can be used to compute the length of a
  1354. vector.
  1355. X
  1356. @group
  1357. @smallexample
  1358. 3:  19            3:  19          1:  0.550782    1:  56.579
  1359. 2:  [1, 2, 3]     2:  3.741657        .               .
  1360. 1:  [7, 6, 0]     1:  9.219544
  1361. X    .                 .
  1362. X
  1363. X    M-RET             M-2 A          * /             I C
  1364. @end smallexample
  1365. @end group
  1366. X
  1367. @noindent
  1368. First we recall the arguments to the dot product command, then
  1369. we compute the absolute values of the top two stack entries to
  1370. obtain the lengths of the vectors, then we divide the dot product
  1371. by the product of the lengths to get the cosine of the angle.
  1372. The inverse cosine finds that the angle between the vectors
  1373. is about 56 degrees.
  1374. X
  1375. @cindex Cross product
  1376. @cindex Perpendicular vectors
  1377. The @dfn{cross product} of two vectors is a vector whose length
  1378. is the product of the lengths of the inputs times the sine of the
  1379. angle between them, and whose direction is perpendicular to both
  1380. input vectors.  Unlike the dot product, the cross product is
  1381. defined only for three-dimensional vectors.  Let's double-check
  1382. our computation of the angle using the cross product.
  1383. X
  1384. @group
  1385. @smallexample
  1386. 2:  [1, 2, 3]  3:  [-18, 21, -8]  1:  [-0.52, 0.61, -0.23]  1:  56.579
  1387. 1:  [7, 6, 0]  2:  [1, 2, 3]          .                         .
  1388. X    .          1:  [7, 6, 0]
  1389. X                   .
  1390. X
  1391. X    r 1 r 2        V C  s 3  M-RET    M-2 A * /                 A I S
  1392. @end smallexample
  1393. @end group
  1394. X
  1395. @noindent
  1396. First we recall the original vectors and compute their cross product,
  1397. which we also store for later reference.  Now we divide the vector
  1398. by the product of the lengths of the original vectors.  The length of
  1399. this vector should be the sine of the angle; sure enough, it is!
  1400. X
  1401. @c [fix-ref General Mode Commands]
  1402. Vector-related commands generally begin with the @kbd{v} prefix key.
  1403. Some are uppercase letters and some are lowercase.  To make it easier
  1404. to type these commands, the shift-@kbd{V} prefix key acts the same as
  1405. the @kbd{v} key.  (@xref{General Mode Commands}, for a way to make all
  1406. prefix keys have this property.)
  1407. X
  1408. If we take the dot product of two perpendicular vectors we expect
  1409. to get zero, since the cosine of 90 degrees is zero.  Let's check
  1410. that the cross product is indeed perpendicular to both inputs:
  1411. X
  1412. @group
  1413. @smallexample
  1414. 2:  [1, 2, 3]      1:  0          2:  [7, 6, 0]      1:  0
  1415. 1:  [-18, 21, -8]      .          1:  [-18, 21, -8]      .
  1416. X    .                                 .
  1417. X
  1418. X    r 1 r 3            *          DEL r 2 r 3            *
  1419. @end smallexample
  1420. @end group
  1421. X
  1422. @cindex Normalizing a vector
  1423. @cindex Unit vectors
  1424. (@bullet{}) @strong{Exercise 1.}  Given a vector on the top of the
  1425. stack, what keystrokes would you use to @dfn{normalize} the
  1426. vector, i.e., to reduce its length to one without changing its
  1427. direction?  @xref{Vector Answer 1, 1}. (@bullet{})
  1428. X
  1429. (@bullet{}) @strong{Exercise 2.}  Suppose a certain particle can be
  1430. at any of several positions along a ruler.  You have a list of
  1431. those positions in the form of a vector, and another list of the
  1432. probabilities for the particle to be at the corresponding positions.
  1433. Find the average position of the particle.
  1434. @xref{Vector Answer 2, 2}. (@bullet{})
  1435. X
  1436. @node Matrix Tutorial, List Tutorial, Vector Analysis Tutorial, Vector/Matrix Tutorial
  1437. @subsection Matrices
  1438. X
  1439. @noindent
  1440. A @dfn{matrix} is just a vector of vectors, all the same length.
  1441. This means you can enter a matrix using nested brackets.  You can
  1442. also use the semicolon character to enter a matrix.  We'll show
  1443. both methods here:
  1444. X
  1445. @group
  1446. @smallexample
  1447. 1:  [ [ 1, 2, 3 ]             1:  [ [ 1, 2, 3 ]
  1448. X      [ 4, 5, 6 ] ]                 [ 4, 5, 6 ] ]
  1449. X    .                             .
  1450. X
  1451. X  [[1 2 3] [4 5 6]]             ' [1 2 3; 4 5 6] RET
  1452. @end smallexample
  1453. @end group
  1454. X
  1455. @noindent
  1456. We'll be using this matrix again, so type @kbd{s 4} to save it now.
  1457. X
  1458. Note that semicolons work with incomplete vectors, but they work
  1459. better in algebraic entry.  That's why we use the apostrophe in
  1460. the second example.
  1461. X
  1462. When two matrices are multiplied, the lefthand matrix must have
  1463. the same number of columns as the righthand matrix has rows.
  1464. Row @cite{i}, column @cite{j} of the result is effectively the
  1465. dot product of row @cite{i} of the left matrix by column @cite{j}
  1466. of the right matrix.
  1467. X
  1468. If we try to duplicate this matrix and multiply it by itself,
  1469. the dimensions are wrong and the multiplication cannot take place:
  1470. X
  1471. @group
  1472. @smallexample
  1473. 1:  [ [ 1, 2, 3 ]   * [ [ 1, 2, 3 ]
  1474. X      [ 4, 5, 6 ] ]     [ 4, 5, 6 ] ]
  1475. X    .
  1476. X
  1477. X    RET *
  1478. @end smallexample
  1479. @end group
  1480. X
  1481. @noindent
  1482. Though rather hard to read, this is a formula which shows the product
  1483. of two matrices.  The @samp{*} function, having invalid arguments, has
  1484. been left in symbolic form.
  1485. X
  1486. We can multiply the matrices if we @dfn{transpose} one of them first.
  1487. X
  1488. @group
  1489. @smallexample
  1490. 2:  [ [ 1, 2, 3 ]       1:  [ [ 14, 32 ]      1:  [ [ 17, 22, 27 ]
  1491. X      [ 4, 5, 6 ] ]           [ 32, 77 ] ]          [ 22, 29, 36 ]
  1492. 1:  [ [ 1, 4 ]              .                       [ 27, 36, 45 ] ]
  1493. X      [ 2, 5 ]                                    .
  1494. X      [ 3, 6 ] ]
  1495. X    .
  1496. X
  1497. X    U v t                   *                     U TAB *
  1498. @end smallexample
  1499. @end group
  1500. X
  1501. Matrix multiplication is not commutative; indeed, switching the
  1502. order of the operands can even change the dimensions of the result
  1503. matrix, as happened here!
  1504. X
  1505. If you multiply a plain vector by a matrix, it is treated as a
  1506. single row or column depending on which side of the matrix it is
  1507. on.  The result is a plain vector which should also be interpreted
  1508. as a row or column as appropriate.
  1509. X
  1510. @group
  1511. @smallexample
  1512. 2:  [ [ 1, 2, 3 ]      1:  [14, 32]
  1513. X      [ 4, 5, 6 ] ]        .
  1514. 1:  [1, 2, 3]
  1515. X    .
  1516. X
  1517. X    r 4 r 1                *
  1518. @end smallexample
  1519. @end group
  1520. X
  1521. Multiplying in the other order wouldn't work because the number of
  1522. rows in the matrix is different from the number of elements in the
  1523. SHAR_EOF
  1524. true || echo 'restore of calc.texinfo failed'
  1525. fi
  1526. echo 'End of  part 32'
  1527. echo 'File calc.texinfo is continued in part 33'
  1528. echo 33 > _shar_seq_.tmp
  1529. exit 0
  1530. exit 0 # Just in case...
  1531. -- 
  1532. Kent Landfield                   INTERNET: kent@sparky.IMD.Sterling.COM
  1533. Sterling Software, IMD           UUCP:     uunet!sparky!kent
  1534. Phone:    (402) 291-8300         FAX:      (402) 291-4362
  1535. Please send comp.sources.misc-related mail to kent@uunet.uu.net.
  1536.