A83: Floating point numbers


[Prev][Next][Index][Thread]

A83: Floating point numbers




check out this program that i made that stores a floating point number
as a .db then copies it to op1 and displays it. the example fp numbers
in this program provide an example, but here is a better explanation.


floating point numbers consist of three parts.

1) the first byte is the mantissa sign byte. this tells if the number is
positive or negative.

2) the second byte is the exponent. $80 is an exponent of 0. if the
exponent is $80 the decimal point is after the first number. if the
exponent is greater than $80 the decimal point moves to the right,
making the number larger (unless it is negative, then it gets smaller).
if the exponent is less than $80 the decimal point moves to the left,
making the number smaller (unless it is negative, then it gets larger).
the exponent is between $00 and $FF.

3) the last 7 bytes are the number data. as you can see in this table
each hex digit, which has to be between 0 and 9, corresponds to the
actual number 0 through 9.

 -----------------------------------------------
| sign | exponent | data                        |
|------|----------|-----------------------------|
| $80  | $82      | $73 $45 $67 $23 $89 $00 $00 |
|------|----------|-----------------------------|
| -    | 2        | 7.345672389                 |
 -----------------------------------------------

now combine the number and the sign to get

 -7.345672389

then shift the decimal point according to the exponent. in this case 2,
or to the right 2.

 -734.5672389

i hope this helps everyone. boy, we have been making huge progress in
the past few weeks!


zfp.z80:

.NOLIST
#define equ .equ
#define EQU .equ
#define end .end
#include "ti83asm.inc"
#include "tokens.inc"
.LIST

.org 9327h

     ld hl,fp1              ; copy 1st fp num to op1
     ld de,op1              ;
     ld bc,9                ;
     ldir                   ;
     call dispfp            ; display it

     ld hl,fp2              ; copy 2nd fp num to op1
     ld de,op1              ;
     ld bc,9                ;
     ldir                   ;
     call dispfp            ; display it
     ret

dispfp:

     ld a,16                ; format and display number
     call _formreal         ;
     ld hl,16               ;
     sbc hl,bc              ;
     ld a,l                 ;
     ld (curcol),a          ;
     ld hl,op3              ;
     call _puts             ;
     ret

fp1:

     .db $00,$80,$75,$00,$00,$00,$00,$00,$00  ; 7.5

fp2:

     .db $80,$82,$73,$45,$67,$23,$89,$00,$00  ; -734.5672389

.end
END


References: