Posts: 66
Threads: 32
Joined: Dec 2002
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..
ttp://hem.passagen.se/qb.basta
Posts: 3,368
Threads: 195
Joined: Jan 2003
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...
Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."
Visit www.neobasic.net to see rubbish in all its finest.
Posts: 358
Threads: 15
Joined: May 2003
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"
Posts: 1,956
Threads: 65
Joined: Jun 2003
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"
*****
Posts: 2,020
Threads: 24
Joined: Jun 2002
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]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry
Posts: 358
Threads: 15
Joined: May 2003
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 ;-)
Posts: 6,419
Threads: 74
Joined: Mar 2002
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...
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")
Posts: 2,020
Threads: 24
Joined: Jun 2002
okay, okay, mod is most used for finding the remainder of a division, but it's second greatest use is for cycling integers.
i]"I know what you're thinking. Did he fire six shots or only five? Well, to tell you the truth, in all this excitement, I've kinda lost track myself. But being as this is a .44 Magnum ... you've got to ask yourself one question: 'Do I feel lucky?' Well, do ya punk?"[/i] - Dirty Harry