Qbasicnews.com

Full Version: MOD?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Can someone explain and give some example of what mod is good for?
I haven't used it or got the hang of how it works, and the qb help file don't make much sense to me..
Code:
SUB print2 (x%, y%, string1$)
len.string1% = LEN(string1$)
temp% = len.string1% MOD 50
FOR j% = 1 TO (len.string1% - temp%) / 50
FOR i% = 1 TO 50
LOCATE x1%, y%: PRINT CHR$(ASC(MID$(string1$, j2% + i%, 1)))
x1% = x1% + 1
NEXT i%: y% = y% + 18: x1% = x%: j2%=j2%+50:NEXT j%
FOR i% = 1 TO temp%
LOCATE x1%, y%: PRINT CHR$(ASC(MID$(string1$, j2% + i%, 1)))
x1% = x1% + 1
NEXT i%
END SUB

er well it's buggy... ah well...
Quote:Can someone explain and give some example of what mod is good for?
I haven't used it or got the hang of how it works, and the qb help file don't make much sense to me..

MOD simply returns the remainder after division. integer division "\" and MOD allow integers to be divided without resorting to floating point numbers. The integer division functions are much faster than the floating point math functions. MOD can be used for all sorts of little tricks.

The following code demonstrates integer division, MOD, and "regular" division. Use just one of the IF statements...the output is the same no matter which one you choose. The logic basically says that if the result of the division has no remainder, then a factor has been found and therefore the prime candidate is not prime.
Code:
DEFINT A-Z
PRINT "This program prints odd primes less than 100"

FOR IsaPrime = 3 TO 100 STEP 2
  
   FOR TestIt = 3 TO SQR(IsaPrime) STEP 2
      'IF (INT(IsaPrime / TestIt)) = (IsaPrime / TestIt) THEN GOTO NotPrime
      'IF (IsaPrime MOD TestIt) = 0 THEN GOTO NotPrime
      IF IsaPrime MOD TestIt THEN  ELSE GOTO NotPrime
      'IF IsaPrime / TestIt = IsaPrime \ TestIt THEN GOTO NotPrime
   NEXT TestIt
  
   PRINT IsaPrime;

NotPrime:

NEXT IsaPrime
PRINT
END

Here's another example...simply tests if a number is odd or even

Code:
INPUT "give me a number"; x
IF x MOD 2 THEN PRINT "odd" ELSE PRINT "even"
Although correct, I think the example
if x mod 2 then print "odd" else print "even"
needs a little more explanation for someone learning MOD.

If the result of "x mod 2" is "true", that's because there was a remainder and therefore the number in x is odd.

If the result of "x mod 2" is "false", that is zero, that's because there was NO remainder and therefore the number in x is even.

An easier to understand version of the same line of code is:
if (x mod 2) = 0 then print "even" else print "odd"
*****
well, "if x and 1" works better for that, but mod's most used use, so to speak is just for cycling integers.

Code:
x = (x + 1) mod (max - 1)
Quote:well, "if x and 1" works better for that, but mod's most used use, so to speak is just for cycling integers.

Code:
x = (x + 1) mod (max - 1)

I agree that the AND solution is better...Man...what a revelation when I realized the usefulness of AND to get at the bits!!! :-)

I think that saying that a math operator is "most used...for cycling integers" is a bit presumptious ;-)
Quote:well, "if x and 1" works better for that, but mod's most used use, so to speak is just for cycling integers.

Code:
x = (x + 1) mod (max - 1)

That would cycle from zero to max-2, which is (max-1) seps. To do max steps you have to...

Code:
x = (x + 1) mod max

it is the "and" mode which has the "-1" stuff, but only if max is a power of two:

Code:
x = (x + 1) and (max - 1)

(ie to count 8 times you use "x=(x+1) mod 8" or "x=(x+1) and 7")
okay, okay, mod is most used for finding the remainder of a division, but it's second greatest use is for cycling integers.