changeset 92:6ac267da2216

Correctly calculate existing significand digits for fp to ascii conversion You need to start at the end of the digits and work left to find the first nonzero digit if you want the number of real digits in the significand. Going the other way looking for zeroes causes it to bail out too soon and lop off digits at the first zero encountered within the number, even if it isn't a trailing zero.
author William Astle <lost@l-w.ca>
date Tue, 17 Oct 2023 17:12:36 -0600
parents ecca1fcfc34b
children a4db504611e2
files src/fps.s
diffstat 1 files changed, 11 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/fps.s	Tue Oct 17 17:03:37 2023 -0600
+++ b/src/fps.s	Tue Oct 17 17:12:36 2023 -0600
@@ -803,17 +803,17 @@
                 bpl fps_toascii1                ; brif not
                 lda #'-                         ; negative sign
                 sta -1,u
-fps_toascii1    clra                            ; set number of significant digits that exist
-                ldx #fpa0+fpa.sig               ; point to significand
-fps_toascii2    ldb ,x+                         ; get digit pair
-                bitb #0xf0                      ; is left digit set?
-                beq fps_toascii3                ; brif not - we've counted all the significant digits
-                inca                            ; count digit
-                bitb #0x0f                      ; is right digit set?
-                beq fps_toascii3                ; brif not - we've counted all the sigificant digits
-                inca                            ; count the digit
-                cmpx #fpa0+fpa.sig+5            ; done all significant digits?
-                blo fps_toascii2                ; brif not
+fps_toascii1    lda #10                         ; assume 10 significant digits exist
+                ldx #fpa0+fpa.sig+5             ; point to significand
+fps_toascii2    ldb ,-x                         ; get digit pair
+                bitb #0x0f                      ; is right digit zero?
+                bne fps_toascii3                ; brif not - we've counted all the significant digits
+                deca                            ; trailing zero - uncount it
+                bitb #0xf0                      ; is left digit zero?
+                bne fps_toascii3                ; brif not - we've counted all the sigificant digits
+                deca                            ; trailing zero - uncount it
+                cmpx #fpa0+fpa.sig              ; done all significant digits?
+                bhi fps_toascii2                ; brif not
 fps_toascii3    sta fpaextra                    ; save significant digits that exist
                 lda fpa0+fpa.exp                ; fetch exponent
                 suba #64                        ; remove bias