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

  1.        ╔════════════════════════════════════════════════════╗
  2.        ║ Lesson 3 Part 030  F-PC 3.5 Tutorial by Jack Brown ║
  3.        ╚════════════════════════════════════════════════════╝
  4.  
  5.               ┌──────────────────────────────┐
  6.               │   Simple Number Formatting.  │
  7.               └──────────────────────────────┘
  8.  
  9. Back in Lesson 2 Part 7 used the word " .R " to help format the output
  10. of our table programs into nicely spaced columns.  There are a number of
  11. related words that can be used to display  signed and unsigned single
  12. and double numbers.   The .R suffix means right justified and when a
  13. number display operator has the .R suffix it is expected that the top of
  14. the stack is the width of the field that will contain the number.
  15.  
  16. Here is a complete list of the " xx.R " number display operators paired
  17. with their simpler versions.
  18.  
  19. \ Single signed 16bit numbers.   -32768 - 32767
  20.   .     ( n     -- )  Display signed 16bit # followed by space.
  21.   .R    ( n w   -- )  Display # right justified in w wide field.
  22. \ Single unsigned 16bit numbers.  0 - 65535
  23.   U.    ( u     -- )  Display unsigned 16bit # followed by space
  24.   U.R   ( u w   -- )  Display # right justified in w wide field.
  25. \ Double signed 32bit numbers   -2,147,483,648 - 2,147,483,647
  26.  D.     ( d     -- )  Display signed 32bit # followed by space.
  27.  D.R    ( d w   -- )  Display # right justified in w wide field.
  28. \ Double unsigned 32bit numbers.  0 - 4,294,967,296
  29.  UD.    ( ud    -- )  Display unsigned 32bit # followed by space
  30.  UD.R   ( ud w  -- )  Display # right justified in w wide field.
  31.  
  32. One of the most amazing things about Forth is its ability to work in any
  33. number base.  In F-PC the words DECIMAL, OCTAL, and HEX are included for
  34. changing the radix of the number system to 10, 8, and 16 respectively.
  35. You should try switching to base 16 (HEX) and base 8 (OCTAL) and doing
  36. some simple arithmetic.
  37.  
  38. Here is a simple table program that illustrates the use of then number
  39. formatting operators and different number bases.
  40.  
  41. \ Illustration of Number bases and formatted display operators.
  42. : NTABLE ( dn -- )
  43.          CR ."      unsigned"  ."        signed" ."      unsigned"
  44.             ."        signed"  ."      unsigned" ."        signed"
  45.          CR ."       decimal"  ."       decimal" ."        octal "
  46.             ."        octal "  ."         hex  " ."         hex  "
  47.          20 0 DO
  48.               CR
  49.               DECIMAL 2DUP 13 UD.R
  50.                       2DUP 13  D.R
  51.               OCTAL   2DUP 13 UD.R
  52.                       2DUP 13  D.R
  53.               HEX     2DUP 13 UD.R
  54.                       2DUP 13  D.R
  55.               1. D+
  56.               LOOP
  57.               2DROP DECIMAL ;
  58. Here is a sample of the output:
  59. -10. NTABLE
  60.      unsigned   signed     unsigned   signed     unsigned   signed
  61.       decimal   decimal       octal    octal         hex      hex
  62.    4294967286      -10  37777777766      -12     FFFFFFF6       -A
  63.    4294967287       -9  37777777767      -11     FFFFFFF7       -9
  64.  ( we have deleted the middle of the table !!! )
  65.             8        8           10       10            8        8
  66.             9        9           11       11            9        9
  67.  
  68. ╓──────────────╖
  69. ║ Problem 3.6  ║
  70. ╙──────────────╜
  71. Make a new version NTABLE that outputs a similar table for single signed and
  72. unsigned integers.
  73.  
  74. HIDE and REVEAL  or dealing with Word Definitions containing errors.
  75.  
  76. If a word definition does not compile correctly you will not see it
  77. in the dictionary even though part of the word was compiled.  The reason
  78. for this is to keep you from executing a word containing an error.
  79. For example consider the following incomplete word definition:
  80. : TEST   10 0 DO I .   ;  Stack Changed
  81.   ok
  82. WORDS   <enter>     ( TEST is not there!! )
  83. EMPTY      .....  ok
  84. REVEAL  <enter> ok
  85. WORDS   <enter>     ( Now you see it!! )
  86. TEST           EMPTY      .....  ok
  87. HIDE    <enter> ok
  88. WORDS   <enter>     ( Now you don't !! )
  89. EMPTY      ......  ok
  90.  
  91. You should be aware that an incorrect or incomplete definition will
  92. leave junk compiled in the dictionary even though WORDS does not show
  93. anything.  To remove an incorrectly compiled word TEST you can type:
  94.  
  95. REVEAL  FORGET TEST
  96.  
  97. Use  HELP and VIEW to find out more about HIDE and REVEAL
  98.  
  99. Caution:  Executing an incorrectly compiled word definition like
  100. TEST above, will most likely crash the computer and require a reset!
  101.  
  102. ┌────────────────────────────────────┐
  103. │  Please move to Lesson 3 Part 040  │
  104. └────────────────────────────────────┘
  105.