Weird Bug

Started by airship, September 28, 2008, 08:14 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

airship

Can anyone load this program and tell me WHY I have to go to the same subroutine TWICE in lines 280-290 to get it to work? Is this a bug in BASIC, or in my machine, or am I just not seeing something?

Thanks.


10 rem scratch "32intadd.bas7"onu8
20 rem
30 rem this program adds two 32-digit integers with carry
40 rem using basic 7 on the c128
50 rem
60 rem dsave "32intadd.bas7"onu8
70 rem
80 rem by mark r. brown 09/2008
90 rem
100 rem print"" : directory"32intadd.bas7"onu8
110 graphic5 : fast
120 print"add 32-digit integers with strings"
130 print"                   12345678901234567890123456789012"
140 print"first  "; : gosub 190 : a$ = q$
150 print"second "; : gosub 190 : b$ = q$
160 gosub 380
170 gosub 250
180 end
190 rem input subroutine
200 q$=""
210 input "integer : ";q$
220 if ((val(q$)=0) or (len(q$)>32) or (val(q$)<0) or (instr(q$,".",1)<>0)) then print "error! "; : goto 210
230 q$=right$(("0000000000000000000000000000000" + q$),32)
240 return
250 rem output subroutine
260 q$ = a$ : gosub 330 : a$ = q$
270 q$ = b$ : gosub 330 : b$ = q$
280 q$ = t$ : gosub 330 : t$ = q$
290 q$ = t$ : gosub 330 : t$ = q$ : rem works if you go twice!! wtf !!!
300 print "total : "  t$
310 print "float : " + str$(val(t$)) + ""
320 return
330 rem strip subroutine
340 for xx = 31 to 1 step -1
350 if mid$(q$,(32-xx),1)="0" then next xx
360 q$ = right$(q$,xx+1)
370 return
380 rem add subroutine
390 t$ = ""
400 for xx = 32 to 1 step -1
410 ax$ = mid$(a$,xx,1) : bx$ = mid$(b$,xx,1)
420 gosub 470
430 t$ = d0$ + t$
440 next xx
450 t$ = str$(d1%) + t$
460 return
470 rem single integer digit add
480 a% = val(ax$) : b% = val(bx$) : sum% = 0
490 sum% = a% + b% + d1% : rem with carry!
500 sum$ = str$(sum%)
510 d0$ = right$(sum$,1)
520 if len(sum$)>2 then d1% = 1 : else d1% = 0
530 return

Serving up content-free posts on the Interwebs since 1983.
History of INFO Magazine

hydrophilic

I believe the problem is because the subroutine expects q$ to be exactly 32 characters, but your add routine produce a string of 34 characters.  At the start of line 450, t$ should be 32 characters, but line 450 adds str$(dl%) which adds a space/cursor-right character and a zero/one character.  Rem out 450 for a quick test.

airship

Yep, that was it. I added a RIGHT$ to strip off the leading space and another variable to track the length of the string and that fixed it. It also made it more generic, in that the strip routine will now handle integer strings of any length, which is what I want to eventually be able to do, anyway.

Thanks!
Serving up content-free posts on the Interwebs since 1983.
History of INFO Magazine