       ͻ
        Lesson 3 Part 010  F-PC 3.5 Tutorial by Jack Brown 
       ͼ

             Ŀ
                Double Numbers or Double Words.  
             

What is a double number or double word?

- Consists of 4 bytes or 32 binary bits.

- The bit pattern in a double word can represent:
  i)   an unsigned decimal number from 0 through 4,294,967,295
  ii)  an unsigned hexadecimal number from $0 through $FFFFFFFF
  iii) any binary number from 00000000000000000000000000000000
                      through 11111111111111111111111111111111

- Often the double word is used as the data structure to represent long
  integers in computer languages such as C, Pascal and Forth. In Forth
  long integers or 32-bit integers are often called double numbers.
  Double numbers can be either signed or unsigned.  Normally when we just
  say "double number" we mean "signed double number", if we want to
  refer to the unsigned variety then we will specifically say "unsigned
  double number" !

- As we saw above, unsigned double numbers range from 0 through
  4,294,967,295 decimal. (Signed) Double numbers range from
  -2,147,483,648 through +2,147,483,647.

- ** We can also place double numbers on Forth's parameter stack. **
  ** To put a double number on the stack it must be entered with  **
  ** a decimal point. The decimal point would normally be placed  **
  ** at the end of the number ( but a decimal point any where     **
  ** in the number will suffice).                                 **

- Most of the single number operators are also available for double
  numbers.  To distinguish between them from their single number counter
  parts they are usually prefixed by "2" or "D" .  Generally speaking
  stack operators are prefixed by "2" and arithmetic operators are
  prefixed by "D".... Thus:

  SWAP becomes 2SWAP
  DUP  becomes 2DUP
  +    becomes D+
  -    becomes D-
  .    becomes D.

Here is some sample output captured directly from Forth:
11. 33. .S  <enter> [4]     11       0      33       0  ok
D+ .S       <enter> [2]     44       0  ok
D.          <enter> 44  ok
11. 33. .S  <enter> [4]     11       0      33       0  ok
2SWAP   .S  <enter> [4]     33       0      11       0  ok
D-  .S      <enter> [2]     22       0  ok
D.          <enter> 22  ok

Notice that when we print the stack what we see are single numbers. Note
also that the high 16 bits of the double number are on top of the stack.
It may confuse you to see double numbers represented as two single
numbers when using negative numbers or numbers larger than 65,535.
For example:

-11. -33. .S       <enter> [4]  65525   65535   65503   65535  ok
D+  .S             <enter> [2]  65492   65535  ok
D.                 <enter> -44  ok
111111. 333333. .S <enter> [4]  45575       1    5653       5  ok
D+ .S              <enter> [2]  51228       6  ok
D.                 <enter> 444444  ok

If you don't like the way the F-PC stack print operator displays double
numbers we could write our own special word to display double numbers.
Try the following:

\ This stack print will print double numbers that are on the stack.
\ If there is an odd number of numbers on the stack or if the stack
\ has a mixed combination of singles and doubles you are likely to
\ get confusing results.
: .SD ( -- )
     DEPTH ?DUP IF
                 0 ?DO DEPTH I - 1- PICK
                       DEPTH I - 2- PICK
                       D.  8 EMIT        \ Back up one space
                       ASCII . EMIT      \ Indicate double with .
                 2 +LOOP
                ELSE ." Empty" THEN ;

Here is a sample of the output produced by .SD
-11. -22.  .SD  <enter> -11.  -22.   ok
D-  .SD         <enter> 11.  ok
D.              <enter> 11  ok
111111. 222222. <enter>  ok
.SD             <enter> 111111.  222222.   ok
D+ .SD          <enter> 333333.   ok
D.              <enter> 333333  ok

Notice that D. does not display a double number with a trailing
decimal point.  We added this feature to .SD to remind us that
we have a stack display of double numbers.

Ŀ
  Please move to Lesson 3 Part 020  

