Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
String eval substitute for val in progress...
#1
I'm trying to figure out any pitfalls that would break using the shortcut method in the code, below.

Code: (Select All)
CLS
LINE INPUT "First number  a: "; a$
LINE INPUT "Second number b: "; b$
PRINT

IF MID$(a$, 1, 2) = "-0" THEN a$ = "0"
IF MID$(b$, 1, 2) = "-0" THEN b$ = "0"

' Shortcut routine to evaluate string value comparisons.=====================
PRINT "Shortcut method...": PRINT
IF MID$(a$, 1, 1) = "-" AND MID$(b$, 1, 1) = "-" THEN
    ' Invert results
    REM PRINT "Two negatives adjustment routine..."
    IF MID$(a$, 2) < MID$(b$, 2) THEN
        PRINT "a$ > b$"
    ELSEIF MID$(a$, 2) = MID$(b$, 2) THEN PRINT "a$ = b$"
    ELSEIF MID$(a$, 2) > MID$(b$, 2) THEN PRINT "a$ < b$"
    END IF
ELSE
    IF a$ < b$ THEN
        PRINT "a$ < b$"
    ELSEIF a$ = b$ THEN PRINT "a$ = b$"
    ELSEIF a$ > b$ THEN PRINT "a$ > b$"
    END IF
END IF
PRINT

' Longer routine to evaluate string value comparisons.========================
PRINT "Long method...": PRINT
neg_a = 0: neg_b = 0: dec_a1$ = "": dec_a2$ = "": dec_b1$ = "": dec_b2$ = ""

IF MID$(a$, 1, 1) = "-" THEN neg_a = -1
IF MID$(b$, 1, 1) = "-" THEN neg_b = -1
IF INSTR(a$, ".") THEN
    dec_a1$ = MID$(a$, 1, INSTR(a$, ".") - 1): dec_a2$ = MID$(a$, INSTR(a$, ".") + 1)
ELSE
    dec_a1$ = a$
END IF

IF INSTR(b$, ".") THEN
    dec_b1$ = MID$(b$, 1, INSTR(b$, ".") - 1): dec_b2$ = MID$(b$, INSTR(b$, ".") + 1)
ELSE
    dec_b1$ = b$
END IF

DO
    ' Test for sign.
    SELECT CASE neg_a + neg_b
        CASE 0, -2 ' Both positive or negative
            IF dec_a1$ = dec_b1$ AND dec_a2$ = dec_b2$ THEN a_less_b = 0: EXIT DO ' Same number.
            IF LEN(dec_a1$) AND dec_b1$ = "" THEN a_less_b = 1: EXIT DO ' a >=1 and b is a decimal.
            IF LEN(dec_b1$) AND dec_a1$ = "" THEN a_less_b = -1: EXIT DO ' b >=1 and a is a decimal.
            IF LEN(dec_a1$) AND dec_a1$ <> dec_b1$ OR LEN(dec_b1$) AND dec_a1$ <> dec_b1$ THEN ' One or both >=1 and non-decimal parts are not equal.
                IF LEN(dec_a1$) > LEN(dec_b1$) THEN a_less_b = 1: EXIT DO
                IF LEN(dec_a1$) < LEN(dec_b1$) THEN a_less_b = -1: EXIT DO
                IF LEN(dec_a1$) = LEN(dec_b1$) THEN
                    FOR i = 1 TO LEN(dec_a1$)
                        IF MID$(dec_a1$, i, 1) <> MID$(dec_b1$, i, 1) THEN EXIT FOR
                    NEXT
                    IF MID$(dec_a1$, i, 1) < MID$(dec_b1$, i, 1) THEN a_less_b = -1: EXIT DO ELSE a_less_b = 1: EXIT DO
                END IF
            ELSE ' Both decimals or non-decimal digits are the same and cancel out.
                j = LEN(dec_a2$)
                IF LEN(dec_b2$) > j THEN j = LEN(dec_b2$)
                FOR i = i TO j
                    IF MID$(dec_a2$, i, 1) <> MID$(dec_b2$, i, 1) THEN EXIT FOR
                NEXT
                IF MID$(dec_a2$, i, 1) < MID$(dec_b2$, i, 1) THEN a_less_b = -1: EXIT DO ELSE a_less_b = 1: EXIT DO
            END IF
        CASE -1 ' One is negative.
            j = -999
            IF neg_a THEN a_less_b = -1: EXIT DO ELSE a_less_b = 1: EXIT DO
    END SELECT
    EXIT DO
LOOP
IF neg_a OR neg_b THEN IF j <> -999 THEN a_less_b = a_less_b * -1
IF a_less_b < 0 THEN PRINT "a$ < b$" ELSE IF a_less_b = 0 THEN PRINT "a$ = b$" ELSE PRINT "a$ > b$"
REM PRINT dec_a1$, dec_a2$, dec_b1$, dec_b2$, neg_a, neg_b
PRINT
SLEEP
RUN


The point I'm at now, I can get away with using string comparison as long as I disallow -0 and flip the results for two negatives. A single negative is always the smaller number but as I came to realize in string comparisons from a different tread, when faced with two negatives, a string evaluation will not change the fact that the larger numeric value of string is all that is considered. You need a sub-routine to invert the results.

So, can anyone see anything I missed here in the shortcut routine, or is this cake all backed?

Pete
Reply


Messages In This Thread
String eval substitute for val in progress... - by Pete - 08-05-2022, 07:30 PM



Users browsing this thread: 2 Guest(s)