       ͻ
        Lesson 4 Part 120  F-PC 3.5 Tutorial by Jack Brown 
       ͼ

              Ŀ
                Double Variables and Constants.  
              

There is also a word set for dealing with double or 32 bit numbers.


2VARIABLE   <name>      Creates a 2 cell ( 4 byte ) variable
                        called <name>.

<name>    ( --   adr )  When <name> is executed it will push the
                        address of the first cell onto the stack

2CONSTANT   <name>      Creates a double constant called <name>
            ( d    -- ) with the initial value of d

<name>      ( --   d  ) When <name> is executed the double
                        number is pushed to the data stack.

2!      ( d  adr   -- ) Store the double number d at adr.

2@      ( adr      d  ) Fetch the double number d from adr.

Below are a few examples.  Don't forget to use a decimal point when
entering double numbers and that you must use D. to display them.

2VARIABLE LEN   <enter> ok
2VARIABLE WTH   <enter> ok
123456. LEN 2!  <enter> ok
12345.  WTH 2!  <enter> ok
LEN 2@ D. <enter> 123456  ok
WTH 2@ D. <enter> 12345  ok
\ Compute perimeter of rectangle
: PERIMETER ( -- p )
       LEN 2@ WTH 2@ D+ 2DUP D+ ;  <enter> ok
PERIMETER D. <enter> 271602  ok

ķ
 Problem 4.20 
Ľ
Write Forth program that finds the volume and surface area of a sphere.
Use three double variables  RADIUS , AREA  and VOLUME and one double
constant  PI .  You may want to investigate some of the double number
arithmetic operators that appear in the next part of lesson 4.
How are you going to handle the inputing of double numbers from the
user?
                  Ŀ
                    Double Number Arrays  
                  

We can create arrays with double numbers by using the operator ,
two times after each double number.

CREATE DTABLE 12345. , , -54321. , , 11111. , , -22222. , , <enter> ok
DTABLE 2@ D. <enter> 12345  ok
DTABLE 4 + 2@ D. <enter> -54321  ok
  ok
: DT@ ( n -- dn )  4 * DTABLE + 2@ ; <enter> ok
: DT! ( dn n -- )  4 * DTABLE + 2! ; <enter> ok
  ok
9999. 3 DT! <enter> ok
3 DT@ D. <enter> 9999  ok

       Ŀ
         New word  */MOD  ( a relative of the */ scalar ) 
       

*/MOD ( a b c --  r q )  were q = a*b/c and r is remainder.

This word computes a full 32 bit intermediate product before the
division takes place.  It operates the same as */ with the addition of
the remainder as a stack output.  As an application of */MOD we
provide an alternate solution of Problem 2.17, which was to compute
the area of a circle to three decimal places.

\ In this solution we use the right justified display operator .R
\ to avoid the use of 1 BACKSPACES which was used to back up over
\ the trailing blank that  .   provides.
\ We have also replaced the phrase  355 * 113 /MOD
\ with the phrase  355 113 */MOD  for a greater range of radii.
\ */MOD   will first multiply by 355 leaving a 32 bit intermediate
\ product and then divide this 32 bit product by 113.


\ Compute area of a circle to three decimal places.
: CIRCLE_AREA ( r -- )
        DUP * 355 113 */MOD
        ." Area = " 5 .R        \ ( 8 EMIT ) 1 BACKSPACES
        ASCII . EMIT
        3 0  DO
             10 * 113 /MOD 1 .R \ ( 8 EMIT ) 1 BACKSPACES
             LOOP DROP ;

: TEST_CIRC ( -- )
       11 1 DO CR I CIRCLE_AREA LOOP ;

ķ
 Problem 4.21 
Ľ
Enter the above definition and verify its operation.  What change
would you have to make to compute the area to 5 decimal places?

Ŀ
   Please move to Lesson 4 Part 130  

