A single line function to modify MOD for better pattern recognition....
#1
Code: (Select All)
DIM AS INTEGER i, j
DO
    CLS
    INPUT "Input any integer: "; i: PRINT
    INPUT "Input a modulo as a non-zero integer: "; j
    IF j = 0 THEN _CONTINUE
    i$ = LTRIM$(STR$(i))
    LOCATE 5, 2: PRINT LTRIM$(STR$(i)); " modx"; j; "="; modx(i, j)
    SLEEP
    IF INKEY$ = CHR$(27) THEN SYSTEM
LOOP

FUNCTION modx (i, j)
    modx = (ABS(i) - ABS(j) * (ABS(i \ j) + (1 - SGN(i)) \ 2)) * SGN(i MOD j)
END FUNCTION

So modx is a way we can modify our QB64 MOD operator so we can work with patterns. It conforms with online modulo calculators.

For comparison, see the first result for modx and compare it to the second result of MOD. Note they are the same until the numbers turn negative.

Code: (Select All)
$CONSOLE:ONLY
' Testing modx 5
FOR i = 20 TO -20 STEP -1
    i$ = LTRIM$(STR$(i))
    LOCATE , 4 - LEN(i$): PRINT LTRIM$(STR$(i));: LOCATE , 5: PRINT "modx j ="; modx(i, 5), "QB64 MOD: "; i MOD 5
NEXT

FUNCTION modx (i, j)
    modx = (ABS(i) - ABS(j) * (ABS(i \ j) + (1 - SGN(i)) \ 2)) * SGN(i MOD j)
END FUNCTION

Note that modx also works with negative modulo integers. I'll leave it to the more math proficient if this utility could be extended to floating point operations.

The function can be modified again to change the zero output to the modulo number. See the two modx, modx_p1 and modx_p2 compared below:

Code: (Select All)
' Two pattern formulas with MOD.
$CONSOLE:ONLY
' Testing modx_p1 5
FOR i = 20 TO -20 STEP -1
    i$ = LTRIM$(STR$(i))
    LOCATE , 4 - LEN(i$): PRINT LTRIM$(STR$(i));: LOCATE , 5: PRINT "modx j ="; modx_p1(i, 5), "QB64 MOD: "; i MOD 5
NEXT
PRINT: PRINT "Press a key for next pattern...": SLEEP

' Testing modx_p2 5
FOR i = 20 TO -20 STEP -1
    i$ = LTRIM$(STR$(i))
    LOCATE , 4 - LEN(i$): PRINT LTRIM$(STR$(i));: LOCATE , 5: PRINT "modx j ="; modx_p2(i, 5), "QB64 MOD: "; i MOD 5
NEXT

FUNCTION modx_p1 (i, j)
    modx_p1 = (ABS(i) - ABS(j) * (ABS(i \ j) + (1 - SGN(i)) \ 2)) * SGN(i MOD j)
END FUNCTION

FUNCTION modx_p2 (i, j)
    modx_p2 = (ABS(i) - ABS(j) * (ABS(i \ j) + (1 - SGN(i)) \ 2)) * SGN(i MOD j) + ABS(j) - ABS(j * SGN(i MOD j))
END FUNCTION

So what the second example is useful for is things like file record look up and calendar apps, etc. Here is a quick example of how it could be used for a calendar.

Code: (Select All)
WIDTH 80, 42
_SCREENMOVE 0, 0
FOR i = 1 TO 31
    PRINT "Day"; i, modx(i, 7)
NEXT

FUNCTION modx (i, j)
    modx = (ABS(i) - ABS(j) * (ABS(i \ j) + (1 - SGN(i)) \ 2)) * SGN(i MOD j) + ABS(j) - ABS(j * SGN(i MOD j))
END FUNCTION

Now I put the second pattern function together after I made the first, which makes me wonder if instead of adding the last part of the equation, if I could optimize it by changing the prior existing equation. I won't be looking into it now, as I got side tracked from another project for this, but optimization changes are always welcomed. Just be sure any changes will work for all possible possible negative and positive number and modulo situations.

Also, if you find any holes in the function, please feel free to post your findings. I'm not certifying this as 100%. Steve and Bplus also have working models posted in another thread. Mine is just a one-liner, which totally suits my personality to a tee... Eeew ya carnsarn idiom!

Pete
Reply


Messages In This Thread
A single line function to modify MOD for better pattern recognition.... - by Pete - 11-28-2022, 08:05 PM



Users browsing this thread: 3 Guest(s)