www.jupiter-ace.co.uk

Previous Page > Listings Index > Floating Point Dictionary


Computing ToDay - Floating Point Dictionary, page 84

Software: Ace arithmetic
Stephen Renals
FLOATING POINT
DICTIONARY
This dictionary for the Jupiter Ace will let you calculate logarithms to the bases e and 10, exponentials, powers, and one root of an equation using the Newton-Raphson method.

added on to the sum at the end. The value of h is then calculated from the mantissa and the above series is summed. The natural logarithm of a number may therefore be found.
LOG finds the log to the base 10 of a number, using the relation:

log10X = ln x
ln 10

EXP calculates the exponential function (exp x ex) of a floating point number. This is achieved by summing the Taylor Approximation to exp x:

exp h = l + h + h2 + h3 + h4 + h5 + . . .
21    31  41    51
where

nl = n * (n - 1) * (n - 2) * . . . * 1

LN is accurate to around 3 significant figures (SF). The size of the operand is not important, as LN splits the number, as mentioned earlier.
EXP is accurate to 5 or 6 SF on numbers between about - 2 and 12. It is accurate to 3 or 4 SF when the operand is between about 12 and 16, or - 2 and - 4. Above 22 of 23, or below - 6 it is not very accurate.
From these definitions, F may be defined, which finds the power of any number. This word is based upon the relation:

ab = exp(b * ln a)

F is accurate to between 4 and 6 SF on answers ranging from 0.05 to 1.25 million, and accurate to 2 or 3 SF on answers up to 500 million or between 0.05 and 0.007.
Finally SOLVE will find the root of an equation using 26 iterations of the Newton-Raphson method. In this word, the function to be equated to 0 is contained in the word FX. F'X contains the derived function of FX. The derived functions of some common functions are given,in Table 1.
Hence to use SOLVE to solve the following equation, for example:

9x5 - 4x4 + 2x = 3, ie
9x5 - 4x4 + 2x - 3 = 0

type in:
: FX (9x5 - 4x4 + 2x - 3)
  2DUP 5. FI^ 9. F*
  20VER 4. FI^ 4. F* F-
  2SWAP 2DUP F+ F+ 3. F-
;
REDEFINE X
: F'X (45x4 - 16x3 + 2)
  2DUP 4. FI^ 45. F*
  2SWAP 3. FI^ 16. F*
  F- 2. F+
;
REDEFINE F'X
T
he Jupiter Ace has only the most basic of floating point words defined in ROM. However, with a little effort, a fairly acceptable package of floating point words may be defined. Indeed, this process is started by the inclusion of floating point stack manipulation - words and square root and trigonometric functions in the Jupiter Ace manual.
The words 2* and 2/ are machine coded words that perform the operations suggested by their names, included because they increase speed of operation. 2ROLL, 2PICK, 2?DUP,
2VARIABLE and 2CONSTANT are floating point variations of words that operate on ordinary 16-bit numbers (they can be used on double length integers, also). FLOAT has been defined so that signed 16-bit numbers may be transferred to floating point:
- 32 UFLOAT would leave 65504. on top of the stack
- 32 FLOAT will leave - 32. on top of the stack
FO< is the same as DO< given in the ACE manual, since it also works on floating point numbers.
FINPUT will wait for a floating point number to be entered, whereas INPUT (only included for completeness) will wait for a 16-bit number to be entered.
FI will calculate powers of floating point numbers, so long as the index is an integer (in floating point form) not less than 0. It is similar to a 16-bit definition given in the ACE manual.
LN calculates the natural logarithm (log to the base e) of a floating point number. It does this by using the series:

ln m = 2(h + h3 + h5 + h7+ h9 + . . .)
35 79
where

h = m - l m + l


This series gives a good ap-proximation of ln m, when m is less than 10.
LN first splits the floating point number into mantissa and exponent. For example, 984.51
(= 9.8451E2) has a mantissa of
9.8451 and an exponent of 2. This is useful, since:

ln 9.84E2 = 2*ln 10 + ln 9.84

Hence the exponent is multiplied by ln 10 (2.30528) to be

Computing ToDay - Foating Point Dictionary, page 85

FLOATING POINT DICTIONARY
Typng SOLVE will leave the solution to 9x5 - 4x4+ 2x - 3 = 0 0n top of the stack (in fact SOLVE will leave .807163 on the stack).
These words occupy around 1100 bytes, with FX and F'X left 'empty', but including SIN, COS,TAN and SQRT: hence the Jupiter Ace will require memory expansion to run it all. The words may be cleared by typing FORGET IT-PAL.
FXF'X
axm + bxn maxm-l + nbxn-l
sin(ax + b)a*sin (ax + b)
cos(ax + b)-a*cos (ax + b)
tan(ax + b)a / cos2(ax + b)
axax * ln a

exp xexp x
ln xl / x
Table 4 Some common functions and their derivatives
Listing 1. The floating point dictionary for the Ace.

: IT-PAL ;

DEFINER CODE(Page 147 Ace manual)
DOES>
  CALL ;

: HEX 16 BASE C! ;


HEX


CODE 2* (N--N*2)
  DF C, (RST 24)
  CB C, 23 C, (SLA E  )
  CB C, 12 C, (RL D    )
  D7 C, (RST 16)
  FD C, E9 C, (JP(IY)  )

CODE 2/ (N--N/2)
  DF C, (RST 24)
  CB C, 2A C, (SRA D )
  CB C, 1B C, (RR E )
  D7 C, (RST 16)
  FD C, E9 C, (JP(IY)  )


DECIMAL

Enter floating point stack rearrangement words -
page 91 Ace manual


: 2ROLL (N--)
  DUP 2* ROLL
  SWAP 2* ROLL SWAP
;

: 2PICK (N1--FP2)
  DUP 2* PICK SWAP
  2* 1+ PICK SWAP
;

: 2?DUP (FP--FP,FP if FP=0, FP--FP if FP=0)
  2DUP OR
  IF
    2DUP
  THEN
;

DEFINER 2VARIABLE (4 byte variable)
  SWAP , ,
DOES>
;

DEFINER 2CONSTANT (4 byte constant)
  SWAP , ,
DOES>
  2@
;

: FLOAT (N--FP signed UFLOAT)
  DUP 0< SWAP ABS
  UFLOAT ROT
  IF
    FNEGATE
  THEN
;

: FO< (FP--FLAG page 114 Ace manual)
  SWAP DROP 0<
;

: FINPUT (--FP)
  BEGIN
    RETYPE NUMBER
    4102 =
  WHILE
    DROP
  REPEAT
;

: INPUT (--N)
  BEGIN
    RETYPE NUMBER
    4181 =
  WHILE
    2DROP
  REPEAT
;

: FERROR? (FLAG--)
  IF
    8 15421 C! ABORT (ERRNO = 8)
  THEN
;

: FI^ (FP1,FP2--FPIFP2; FP2=integer
(not less than zero)
  2DUP INT UFLOAT 20VER F-
  OR FERROR?
  1. 2SWAP 2DUP OR
  IF
    INT 0
    DO
      2OVER F*
    LOOP
  THEN
  2SWAP 2DROP
;

: LN (FP--ln(FP))
  ?DUP 0< FERROR? (can't have In of a
negative number)
  DUP 256 / UFLOAT 65. F- (extract the exponent)
  2.30528 F* 2SWAP
  255 AND 16640 + (a = exponent.1n10, m = mantissa)
  2DUP 1. F- 2SWAP 1. F+ F/ (a, h = m-1/m+1)
  2DUP 2DUP F* 2OVER
  31 3
  DO (a, sum, h2, hi-2)
    2OVER F. 2ROT 2OVER (a, h2, hi, sum, hi)
    I UFLOAT F/ F+ (a, h2, hi, new sum)
    2ROT 2ROT 2
  +LOOP
  2DROP 2DROP 2. F* F+
;

: LOG (FP--log10FP)
  LN 2.30258 F/
;

: EXP (FP--exp(fP))
  1. 2SWAP 1. (1., h, 1.)
  31 1
  DO (sum, h, last term)
    2OVER I UFLOAT F/ F*
(sum, h, this term = last term*h/i)
    2ROT 2OVER F+ 2ROT 2ROT
  LOOP
  2DROP 2DROP
;

: F^ (FPl,FP2--FPl,FP2)
  2SWAP LN F* EXP
;

Enter SIN COS TAN SQRT from page 92 of Ace manual

: FX (function equated to 0 by SOLVE)
(redefine to desired function)
(before executing SOLVE)
;

: F'X (derived function of FX for SOLVE)
(redefine before executing SOLVE)
;

: SOLVE (--FP Equates function FX to 0)
  1. 26 0 (F`X is derived function of FX)
  DO
    2DUP FX 20VER F'X F/ F-
  LOOP
;






Floating Point Dictionary listing - page 84    Floating Point Dictionary listing - page 85