home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / forth / compiler / fpc / source / p2_17jb.seq < prev    next >
Text File  |  1990-04-08  |  2KB  |  56 lines

  1.  
  2. : TRIANGLE_AREA ( b h --)
  3.        2DUP SWAP
  4.        CR ." Base =   "  .
  5.           ." Height = "  .
  6.           * 2 /MOD
  7.           ." Area = " 5 .R
  8. \         1 BACKSPACES ( 8  EMIT  does not back up cursor in F-PC)
  9.           ASCII . EMIT
  10.           IF    ASCII 5 EMIT
  11.           ELSE  ASCII 0 EMIT
  12.           THEN ;
  13.  
  14. COMMENT:
  15. Problem 2.17.
  16. a) What do you observe when you use 5 and 3 for the base and height?
  17. b) What do you observe when you use 5 and 4 for the base and height?
  18. c) Explain in detail why you get the result you see for question 1.
  19. d) Could you modify the idea contained in the above to compute the Area
  20. of a circle to three decimal places?  Hint:  It can be done quite easily
  21. and if you can do it you have the potential to become a Forth Super Star!
  22.  
  23. Solution to Problem 2.17
  24.  
  25. a) What do you observe for output of triangle program
  26.    with  5 3 AREA?  Answer:
  27. Base =   5 Height = 3 Area = 7.5
  28.  
  29. b) What about  5 4 AREA ?  Answer:
  30. Base =   5 Height = 4 Area = 10.0
  31.  
  32. c) Why the decimal result for question one when we only have
  33.    integer math?  Answer:  Because if the remainder from the
  34.    /MOD operation is 1 we can display the .5, if the remainder
  35.    is 0 then we can display the required .0
  36.  
  37. d) Computer area of a circle to three decimal places.
  38.  
  39. COMMENT;
  40.  
  41. \ Compute area of a circle to three decimal places.
  42. : CIRCLE_AREA ( r -- )
  43.         DUP * 355 * 113 /MOD
  44.         ." Area = " 5 .R \ ( 8 EMIT ) 1 BACKSPACES
  45.         ASCII . EMIT
  46.         5 0  DO
  47.              10 * 113 /MOD 1 .R \ ( 8 EMIT ) 1 BACKSPACES
  48.              LOOP DROP ;
  49.  
  50. : TEST_CIRC ( -- )
  51.        11 1 DO CR I CIRCLE_AREA LOOP ;
  52.  
  53.  
  54.  
  55.  
  56.