home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / tutor / l3p130 < prev    next >
Text File  |  1990-07-15  |  4KB  |  112 lines

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 3 Part 130  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.          ┌────────────────────────────────────────────┐
  6.          │  Finding a square root by Newton's Method. │
  7.          └────────────────────────────────────────────┘
  8.  
  9. Have you studied any Calculus lately?
  10.  
  11. Theory:  Let  f(x) = x^2 - n  where the root or zero of this
  12. function is the square root of n.
  13.  
  14. Newton's Method:   use guess xo to get better guess xn
  15. according to:   xn = xo - f(xo)/f'(xo)
  16.  
  17. It can be shown that:  xn = ( xo + n/xo )/2
  18.  
  19. \ Here is the square root program.
  20. : XNEW  ( n xold -- n xnew )
  21.         2DUP  /  +  2/  ;
  22.  
  23. : SQRT  ( n -- root )
  24.         DUP 0< IF ABORT" Illegal argument" THEN
  25.         DUP 1 >
  26.         IF    DUP 2/  ( n  n/2 )
  27.         10 0 DO XNEW LOOP NIP     \ I say.. about 10 time should do it.
  28.         THEN  ;
  29.  
  30. Note:  This is not the best or fastest square root algorithm.
  31.  
  32. Here is a simple program that will help you test the square root
  33. program.
  34.  
  35. \ Hypotenuse of a right triangle.
  36. : HYPO  ( a b --  c )
  37.         DUP * SWAP
  38.         DUP * +
  39.         SQRT  ;
  40. : TEST  ( -- )
  41.         15 1 DO  15 1 DO
  42.         CR I J 2DUP  4 .R 4 .R  HYPO 4 .R
  43.         LOOP KEY DROP CR LOOP ;
  44.  
  45. ╓───────────────╖
  46. ║ Problem 3.30  ║
  47. ╙───────────────╜
  48. Write a word that calculates the area of a triangle
  49. using HERO's formula.
  50.  
  51.   A = sqrt[ s(s-a)(s-b)(s-c) ]
  52.  
  53. where  a b and c are the sides of the triangle and s is the semi
  54. perimeter.  s = (a+b+c)/2
  55.  
  56. ╓───────────────╖
  57. ║ Problem 3.31  ║
  58. ╙───────────────╜
  59. Identification of user key presses. Write the word  IDENTIFY  which
  60. takes a key code 0 255 from the data stack and prints one of the
  61. following descriptive phrases identifying the key code. Control
  62. character ,  Punctuation character , Lower case letter Upper case letter
  63. , Numeric Digit ,  Extended character.
  64.  
  65. Hint:
  66. : IDENTIFY ( n  -- )
  67.     DUP CONTROL?     IF  ." Control character. "      ELSE
  68.     DUP PUNCTUATION? IF  ." Punctuation character. "  ELSE
  69.     DUP DIGIT?       IF  ." Numeric Digit "           ELSE
  70.          ...         ..   ...       ....               ...
  71.     THEN  THEN ....   THEN  DROP ;   \ One THEN for every IF
  72.  
  73. : DIGIT?  ( n   flag )  \ Leave true flag if its a digit.
  74.      ASCII 0  ASCII 9  [IN]  ;
  75.  
  76. Modify IDENTIFY to respond intelligently for  n <0 and n>255 and place
  77. it in a loop similar to KEY_TEST of Lesson 3 Part 5 for testing purposes.
  78.  
  79. Here is the solution to problem 3.28 of Lesson 2 Part 12
  80. : AVERAGE  ( x1 f1 x2 f2 ... xn fn    -- )
  81.         0 0 DEPTH  2/ 1-  0
  82.         ?DO  2 PICK +
  83.              2SWAP *
  84.              ROT  +  SWAP
  85.         LOOP
  86.         CR ." The average of the "
  87.         DUP .   ." numbers is "  / . CR ;
  88.  
  89. Here is the solution to the Problem 3.4 of Lesson 3 Part 020.
  90. Floored symmetric division.  Note that q and r must satisfy
  91. the equations:   m/n  = q  +  r/n    or  m = nq + r
  92.  
  93.   /     ( m n -- q )     Leave q , the floor of real quotient.
  94.   MOD   ( m n -- r )     Leave r , remainder (satisfying above).
  95.   /MOD  ( m n -- r q )   Leave remainder r and quotient q .
  96.  
  97. Quiz:   m    n      r     q       Check:  n * q   +  r  =  m?
  98.       ---   ---    ---   ---             --- ---    ---   ---
  99.        13    5      3     2               5 * 2   +  3  =  13
  100.       -11    5      4    -3               5 *-3   +  4  = -11
  101.        -2    5      3    -1               5 *-1   +  3  =  -2
  102.        13   -5     -2    -3              -5 *-3   + -2  =  13
  103.       -11   -5     -1     2              -5 * 2   + -1  = -11
  104.        -2   -5     -2     0              -5 * 0   + -2  =  -2
  105.  
  106. Well that's it...  Lesson 3 is complete.  Hurry with your problem
  107. solutions.
  108.  
  109. Files L3P140 ... L3P170  have solutions to the Speed Check Problem.
  110.  
  111.  
  112.