Faster addition in string math. Now with multiplication!
#3
(08-18-2022, 08:16 PM)SMcNeill Wrote: Change to DIM AS _UNSIGNED _INTEGER64 a, b -- you can add 18 digits at a time.

My goof. I thought _integer64 was not affecting it, but I'm not used to using the new multi-variable DIM method, so I tried...

DIM a, b AS _INTEGER64    

instead of...

DIM AS _INTEGER64 a, b

As you posted. Tried that, and it worked, thanks; however....

I can get 18 digits with that just fine. Hey, interesting about adding the _UNSIGNED. It worked for addition, but blows up for subtraction.

Code: (Select All)
DIM AS _UNSIGNED _INTEGER64 a, b, c

WIDTH 130, 42
_SCREENMOVE 0, 0
a$ = "12345"
b$ = "9999"
'a$ = "986064865096859658629068509685926859068559628695645692662625654605600654654111"
'b$ = "90334052236956578596728693574835692623537583457435345236254653969569966222"
op$ = "-"
DO
    i&& = i&& + 1
    x1$ = MID$(a$, LEN(a$) - i&& + 1, 1)
    x2$ = MID$(b$, LEN(b$) - i&& + 1, 1)
    SELECT CASE op$
        CASE "+"
            a = VAL(x1$) + VAL(x2$) + c
            IF a > 9 THEN a = a - 10: c = 1 ELSE c = 0
            PRINT x1$;: LOCATE , 25: PRINT x2$;: LOCATE , 50: PRINT VAL(x1$) + VAL(x2$);: LOCATE , 75: PRINT c;: LOCATE , 90: PRINT a
        CASE "-"
            a = VAL(x1$) - VAL(x2$) - c
            IF a < 0 THEN a = a + 10: c = 1 ELSE c = 0
            PRINT x1$;: LOCATE , 25: PRINT x2$;: LOCATE , 50: PRINT VAL(x1$) - VAL(x2$);: LOCATE , 75: PRINT c;: LOCATE , 90: PRINT a
    END SELECT

    z$ = LTRIM$(STR$(a)) + z$
LOOP UNTIL i&& >= LEN(a$) AND i&& >= LEN(b$)

' Remove leading zeros for subtraction.
PRINT
IF op$ = "+" THEN PRINT VAL(a$) + VAL(b$), "QB64 VAL()." ELSE PRINT VAL(a$) - VAL(b$), "QB64 VAL()."
PRINT " "; z$, "String Math."

I included the variable c in the DIM statement, but it made no difference. The a variable is the one getting screwed up during subtraction. Switch to addition, op$ = "+" and it works. Remove _UNASSIGNED and it works, too. So something is weird with _UNASSIGNED and subtraction, even when it is one-digit at a time, as in the above example.

Pete
Reply


Messages In This Thread
RE: Faster addition in string math. - by SMcNeill - 08-18-2022, 08:16 PM
RE: Faster addition in string math. - by Pete - 08-18-2022, 09:15 PM
RE: Faster addition in string math. - by SMcNeill - 08-18-2022, 09:59 PM
RE: Faster addition in string math. - by SMcNeill - 08-18-2022, 10:08 PM
RE: Faster addition in string math. - by SMcNeill - 08-18-2022, 11:46 PM
RE: Faster addition in string math. - by Pete - 08-19-2022, 10:07 AM
RE: Faster addition in string math. - by SMcNeill - 08-19-2022, 11:26 AM
RE: Faster addition in string math. - by Pete - 08-19-2022, 04:44 PM
RE: Faster addition in string math. - by SMcNeill - 08-19-2022, 04:58 PM
RE: Faster addition in string math. - by SMcNeill - 08-19-2022, 05:57 PM
RE: Faster addition in string math. - by Jack - 08-19-2022, 09:54 PM
RE: Faster addition in string math. - by SMcNeill - 08-19-2022, 11:48 PM
RE: Faster addition in string math. - by SMcNeill - 08-20-2022, 03:49 AM
RE: Faster addition in string math. - by Pete - 08-20-2022, 06:16 AM
RE: Faster addition in string math. - by SMcNeill - 08-20-2022, 06:41 AM



Users browsing this thread: 4 Guest(s)