home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 8
/
FreshFishVol8-CD1.bin
/
gnu
/
info
/
calc.info-8
(
.txt
)
< prev
next >
Wrap
GNU Info File
|
1994-12-22
|
51KB
|
906 lines
This is Info file calc.info, produced by Makeinfo-1.55 from the input
file calc.texinfo.
This file documents Calc, the GNU Emacs calculator.
Copyright (C) 1990, 1991 Free Software Foundation, Inc.
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of
this manual under the conditions for verbatim copying, provided also
that the section entitled "GNU General Public License" is included
exactly as in the original, and provided that the entire resulting
derived work is distributed under the terms of a permission notice
identical to this one.
Permission is granted to copy and distribute translations of this
manual into another language, under the above conditions for modified
versions, except that the section entitled "GNU General Public License"
may be included in a translation approved by the author instead of in
the original English.
File: calc.info, Node: Programming Answer 2, Next: Programming Answer 3, Prev: Programming Answer 1, Up: Answers to Exercises
Programming Tutorial Exercise 2
-------------------------------
One way is to move the number to the top of the stack, operate on it,
then move it back: `C-x ( M-TAB n M-TAB M-TAB C-x )'.
Another way is to negate the top three stack entries, then negate
again the top two stack entries: `C-x ( M-3 n M-2 n C-x )'.
Finally, it turns out that a negative prefix argument causes a
command like `n' to operate on the specified stack entry only, which is
just what we want: `C-x ( M-- 3 n C-x )'.
Just for kicks, let's also do it algebraically:
`C-x ( ' -$$$, $$, $ RET C-x )'.
File: calc.info, Node: Programming Answer 3, Next: Programming Answer 4, Prev: Programming Answer 2, Up: Answers to Exercises
Programming Tutorial Exercise 3
-------------------------------
Each of these functions can be computed using the stack, or using
algebraic entry, whichever way you prefer:
Computing `sin(x) / x':
Using the stack: `C-x ( RET S TAB / C-x )'.
Using algebraic entry: `C-x ( ' sin($)/$ RET C-x )'.
Computing the logarithm:
Using the stack: `C-x ( TAB B C-x )'
Using algebraic entry: `C-x ( ' log($,$$) RET C-x )'.
Computing the vector of integers:
Using the stack: `C-x ( 1 RET 1 C-u v x C-x )'. (Recall that
`C-u v x' takes the vector size, starting value, and increment from the
stack.)
Alternatively: `C-x ( ~ v x C-x )'. (The `~' key pops a number
from the stack and uses it as the prefix argument for the next command.)
Using algebraic entry: `C-x ( ' index($) RET C-x )'.
File: calc.info, Node: Programming Answer 4, Next: Programming Answer 5, Prev: Programming Answer 3, Up: Answers to Exercises
Programming Tutorial Exercise 4
-------------------------------
Here's one way: `C-x ( RET V R + TAB v l / C-x )'.
File: calc.info, Node: Programming Answer 5, Next: Programming Answer 6, Prev: Programming Answer 4, Up: Answers to Exercises
Programming Tutorial Exercise 5
-------------------------------
2: 1 1: 1.61803398502 2: 1.61803398502
1: 20 . 1: 1.61803398875
. .
1 RET 20 Z < & 1 + Z > I H P
This answer is quite accurate.
File: calc.info, Node: Programming Answer 6, Next: Programming Answer 7, Prev: Programming Answer 5, Up: Answers to Exercises
Programming Tutorial Exercise 6
-------------------------------
Here is the matrix:
[ [ 0, 1 ] * [a, b] = [b, a + b]
[ 1, 1 ] ]
Thus `[0, 1; 1, 1]^n * [1, 1]' computes Fibonacci numbers `n+1' and
`n+2'. Here's one program that does the job:
C-x ( ' [0, 1; 1, 1] ^ ($-1) * [1, 1] RET v u DEL C-x )
This program is quite efficient because Calc knows how to raise a
matrix (or other value) to the power `n' in only `log(n,2)' steps. For
example, this program can compute the 1000th Fibonacci number (a
209-digit integer!) in about 10 steps; even though the `Z < ... Z >'
solution had much simpler steps, it would have required so many steps
that it would not have been practical.
File: calc.info, Node: Programming Answer 7, Next: Programming Answer 8, Prev: Programming Answer 6, Up: Answers to Exercises
Programming Tutorial Exercise 7
-------------------------------
The trick here is to compute the harmonic numbers differently, so that
the loop counter itself accumulates the sum of reciprocals. We use a
separate variable to hold the integer counter.
1: 1 2: 1 1: .
. 1: 4
.
1 t 1 1 RET 4 Z ( t 2 r 1 1 + s 1 & Z )
The body of the loop goes as follows: First save the harmonic sum so
far in variable 2. Then delete it from the stack; the for loop itself
will take care of remembering it for us. Next, recall the count from
variable 1, add one to it, and feed its reciprocal to the for loop to
use as the step value. The for loop will increase the "loop counter"
by that amount and keep going until the loop counter exceeds 4.
2: 31 3: 31
1: 3.99498713092 2: 3.99498713092
. 1: 4.02724519544
.
r 1 r 2 RET 31 & +
Thus we find that the 30th harmonic number is 3.99, and the 31st
harmonic number is 4.02.
File: calc.info, Node: Programming Answer 8, Next: Programming Answer 9, Prev: Programming Answer 7, Up: Answers to Exercises
Programming Tutorial Exercise 8
-------------------------------
The first step is to compute the derivative `f'(x)' and thus the
formula `x - f(x)/f'(x)'.
(Because this definition is long, it will be repeated in concise form
below. You can use `M-# m' to load it from there. While you are
entering a `Z ` Z '' body in a macro, Calc simply collects keystrokes
without executing them. In the following diagrams we'll pretend Calc
actually executed the keystrokes as you typed them, just for purposes
of illustration.)
2: sin(cos(x)) - 0.5 3: 4.5
1: 4.5 2: sin(cos(x)) - 0.5
. 1: -(sin(x) cos(cos(x)))
.
' sin(cos(x))-0.5 RET 4.5 m r C-x ( Z ` TAB RET a d x RET
2: 4.5
1: x + (sin(cos(x)) - 0.5) / sin(x) cos(cos(x))
.
/ ' x RET TAB - t 1
Now, we enter the loop. We'll use a repeat loop with a 20-repetition
limit just in case the method fails to converge for some reason.
(Normally, the `Z /' command will stop the loop before all 20
repetitions are done.)
1: 4.5 3: 4.5 2: 4.5
. 2: x + (sin(cos(x)) ... 1: 5.24196456928
1: 4.5 .
.
20 Z < RET r 1 TAB s l x RET
This is the new guess for `x'. Now we compare it with the old one
to see if we've converged.
3: 5.24196 2: 5.24196 1: 5.24196 1: 5.26345856348
2: 5.24196 1: 0 . .
1: 4.5 .
.
RET M-TAB a = Z / Z > Z ' C-x )
The loop converges in just a few steps to this value. To check the
result, we can simply substitute it back into the equation.
2: 5.26345856348
1: 0.499999999997
.
RET ' sin(cos($)) RET
Let's test the new definition again:
2: x^2 - 9 1: 3.
1: 1 .
.
' x^2-9 RET 1 X
Once again, here's the full Newton's Method definition:
C-x ( Z ` TAB RET a d x RET / ' x RET TAB - t 1
20 Z < RET r 1 TAB s l x RET
RET M-TAB a = Z /
Z >
Z '
C-x )
It turns out that Calc has a built-in command for applying a formula
repeatedly until it converges to a number. *Note Nesting and Fixed
Points::, to