home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l3p130
< prev
next >
Wrap
Text File
|
1990-07-15
|
4KB
|
112 lines
╔════════════════════════════════════════════════════╗
║ Lesson 3 Part 130 F-PC 3.5 Tutorial by Jack Brown ║
╚════════════════════════════════════════════════════╝
┌────────────────────────────────────────────┐
│ Finding a square root by Newton's Method. │
└────────────────────────────────────────────┘
Have you studied any Calculus lately?
Theory: Let f(x) = x^2 - n where the root or zero of this
function is the square root of n.
Newton's Method: use guess xo to get better guess xn
according to: xn = xo - f(xo)/f'(xo)
It can be shown that: xn = ( xo + n/xo )/2
\ Here is the square root program.
: XNEW ( n xold -- n xnew )
2DUP / + 2/ ;
: SQRT ( n -- root )
DUP 0< IF ABORT" Illegal argument" THEN
DUP 1 >
IF DUP 2/ ( n n/2 )
10 0 DO XNEW LOOP NIP \ I say.. about 10 time should do it.
THEN ;
Note: This is not the best or fastest square root algorithm.
Here is a simple program that will help you test the square root
program.
\ Hypotenuse of a right triangle.
: HYPO ( a b -- c )
DUP * SWAP
DUP * +
SQRT ;
: TEST ( -- )
15 1 DO 15 1 DO
CR I J 2DUP 4 .R 4 .R HYPO 4 .R
LOOP KEY DROP CR LOOP ;
╓───────────────╖
║ Problem 3.30 ║
╙───────────────╜
Write a word that calculates the area of a triangle
using HERO's formula.
A = sqrt[ s(s-a)(s-b)(s-c) ]
where a b and c are the sides of the triangle and s is the semi
perimeter. s = (a+b+c)/2
╓───────────────╖
║ Problem 3.31 ║
╙───────────────╜
Identification of user key presses. Write the word IDENTIFY which
takes a key code 0 255 from the data stack and prints one of the
following descriptive phrases identifying the key code. Control
character , Punctuation character , Lower case letter Upper case letter
, Numeric Digit , Extended character.
Hint:
: IDENTIFY ( n -- )
DUP CONTROL? IF ." Control character. " ELSE
DUP PUNCTUATION? IF ." Punctuation character. " ELSE
DUP DIGIT? IF ." Numeric Digit " ELSE
... .. ... .... ...
THEN THEN .... THEN DROP ; \ One THEN for every IF
: DIGIT? ( n flag ) \ Leave true flag if its a digit.
ASCII 0 ASCII 9 [IN] ;
Modify IDENTIFY to respond intelligently for n <0 and n>255 and place
it in a loop similar to KEY_TEST of Lesson 3 Part 5 for testing purposes.
Here is the solution to problem 3.28 of Lesson 2 Part 12
: AVERAGE ( x1 f1 x2 f2 ... xn fn -- )
0 0 DEPTH 2/ 1- 0
?DO 2 PICK +
2SWAP *
ROT + SWAP
LOOP
CR ." The average of the "
DUP . ." numbers is " / . CR ;
Here is the solution to the Problem 3.4 of Lesson 3 Part 020.
Floored symmetric division. Note that q and r must satisfy
the equations: m/n = q + r/n or m = nq + r
/ ( m n -- q ) Leave q , the floor of real quotient.
MOD ( m n -- r ) Leave r , remainder (satisfying above).
/MOD ( m n -- r q ) Leave remainder r and quotient q .
Quiz: m n r q Check: n * q + r = m?
--- --- --- --- --- --- --- ---
13 5 3 2 5 * 2 + 3 = 13
-11 5 4 -3 5 *-3 + 4 = -11
-2 5 3 -1 5 *-1 + 3 = -2
13 -5 -2 -3 -5 *-3 + -2 = 13
-11 -5 -1 2 -5 * 2 + -1 = -11
-2 -5 -2 0 -5 * 0 + -2 = -2
Well that's it... Lesson 3 is complete. Hurry with your problem
solutions.
Files L3P140 ... L3P170 have solutions to the Speed Check Problem.