home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / l2p130.seq < prev    next >
Encoding:
Text File  |  1990-07-15  |  1.7 KB  |  54 lines

  1. \ Lesson 2 Part 130  ( F-PC 3.5 Tutorial by Jack Brown )
  2.  
  3.  
  4. \ Well let's use our division to find the area of a triangle.
  5.  
  6. \ A = bh/2   We'll make a word called  AREA ( b h -- ) that has for its
  7. \ output:   Base = xxx    Height = xxx   Area = xxx
  8.  
  9. : AREA ( b h --)
  10.         2DUP SWAP
  11.         CR ." Base =   " .
  12.            ." Height = " .
  13.            * 2 /
  14.            ." Area =   " . ;
  15.  
  16. COMMENT:
  17. Well.... that was fairly simple...  and guess what?   It wouldn't matter
  18. whether or not we were using Floored or Symmetric Division the results
  19. would always be the same.  The reason is that the dividend bh and the
  20. divisor 2 both have the same sign (positive) and as long as the dividend
  21. and the divisor have the same sign ( both positive or both negative)
  22. both Symmetric and Floored division give the same results!!!
  23.  
  24. Now try this version of the triangle area program.
  25. COMMENT;
  26.  
  27. : TRIANGLE_AREA ( b h --)
  28.        2DUP SWAP
  29.        CR ." Base =   "  .
  30.           ." Height = "  .
  31.           * 2 /MOD
  32.           ." Area = " .
  33.           1 BACKSPACES ( 8  EMIT  does not back up cursor in F-PC)
  34.           ASCII . EMIT
  35.           IF    ASCII 5 EMIT
  36.           ELSE  ASCII 0 EMIT
  37.           THEN ;
  38.  
  39. COMMENT:
  40. Problem 2.17.
  41. a) What do you observe when you use 5 and 3 for the base and height?
  42. b) What do you observe when you use 5 and 4 for the base and height?
  43. c) Explain in detail why you get the result you see for question 1.
  44. d) Could you modify the idea contained in the above to compute the Area
  45. of a circle to three decimal places?  Hint:  It can be done quite easily
  46. and if you can do it you have the potential to become a Forth Super Star!
  47.  
  48. COMMENT;
  49.  
  50.  
  51.  
  52.  
  53.  
  54.