Qbasicnews.com
February 20, 2020, 10:23:29 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: MOD?  (Read 2144 times)
QbBasta
Member
*
Posts: 71


WWW
« on: July 16, 2003, 09:28:55 AM »

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..
Logged

ttp://hem.passagen.se/qb.basta
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #1 on: July 16, 2003, 09:39:38 AM »

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...
Logged

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.
Mango
Wandering Guru
***
Posts: 360



« Reply #2 on: July 16, 2003, 11:27:23 AM »

Quote from: "QbBasta"
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"
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #3 on: July 16, 2003, 03:27:01 PM »

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"
*****
Logged
toonski84
__/--\__
*****
Posts: 2567



« Reply #4 on: July 16, 2003, 03:52:05 PM »

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)
Logged

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
Mango
Wandering Guru
***
Posts: 360



« Reply #5 on: July 17, 2003, 12:46:35 PM »

Quote from: "toonski84"
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 ;-)
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #6 on: July 17, 2003, 10:39:09 PM »

Quote from: "toonski84"
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")
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
toonski84
__/--\__
*****
Posts: 2567



« Reply #7 on: July 17, 2003, 11:00:50 PM »

okay, okay, mod is most used for finding the remainder of a division, but it's second greatest use is for cycling integers.
Logged

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
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!