(As you guys might have noticed, Keyword of the Day has slowed down and hasn't been being updated on a daily basis for the last few days. The reason for this is rather simple -- If you check the QB64-PE repo, you'll see that we've been pushing all sorts of different little batches of work into it -- and if you follow our chat on Discord, you'll see that we BROKE QB64-PE. Matt broke his IDE with some changes... I broke my IDE with some different changes... and sorting out what went wrong where, has been rather <SIGH> to deal with and sort out all the mess. There just hasn't been time to sit down and write up a nice Keyword of the Day article, with all the time and effort spent in undoing the glitches that we oh-so-awesomely did. If anyone else wants to volunteer to do a couple of KotD for us, feel free to speak up and volunteer, and then run with it tomorrow and whenever you get the urge!)
And with that explanation out of the way, let's talk about MOD.
What is it? It's a very common math function that return the remainder from division.
How does one make use of it? It's rather simple to implement, just like addition or multiplication. X = 13 MOD 4..... compare that to.... X = 13 * 4.... Exact same syntax/usage.
So why are we discussing it now? Because of the topic here: Operator MOD (qb64phoenix.com)
Chris, the original poster of the topic link above, insists that MOD is broken and giving the wrong answer.
Now, one would think when dealing with math, the above assumption has to be correct. There can only be one right answer to any mathematical result! Right?
Then what's the SQR(4)??
QB64 will quickly and happily tell us that the answer is 2! My math teacher would count that answer as being half wrong, as the answer is BOTH +2 and -2. (2 * 2 = 4. -2 * -2 = 4) Both are valid square roots for SQR(4). Unfortunately, QB64 only gives us one answer to this function -- the positive value.
By the same token, MOD is one of those operators which can also return different answers. In fact, various programming languages will each handle the result that it gives you, differently. 13 MOD 4 will *always* be 1, but -7 MOD 5 might be either -2 or 3.
Now, how in the heck does one get those various results??
One language might follow the ruleset (our remainder has to be a value from 0 to our number). For the language with this ruleset for mod, the answer for -7 MOD 5 would *have to be* 3. After all, -2 isn't even in the list of possibilities! It only considers 0 to 4 to be valid remainders for any number divided by 5. Basically the way they work is:
1) Find the largest multiple of your denominator that is less than the base number, subtract it, and use it to get the remainder. For 13 MOD 5, it'd find 10 to be the closest multiple of 5 smaller than 13, and then it'd subtract it. 13 - 10 = 3... 3 is the remainder for 13 MOD 5.
Now, in the case of -7 MOD 5, this type of ruleset would choose -10 as the closest multiple of 5, smaller than our number -7, and then it'd subtract it. -7 - -10 = 3. (negative seven minus negative ten = 3, just in case those signs don't show up readable for anyone.)
That's a perfectly valid interpretation of the answer, and it's not wrong at all. Unfortunately, it's also not how QB64 (or C, which we translate to by the way) deals with the math, so that's all the explaination I'm going to go into for the other result.
2) For QB64 (and for C itself), the rule that is in place for finding the remainder with MOD is basically: Find the closest multiple to your denominator, subtract it, and the result is your remainder.
Now, in the case of 13 MOD 5, the answer is exactly the same. 10 is the closest multiple to 5. 13- 10 = 3. 3 is, of course, the remainder.
But, in the case of -7 MOD 5, we see something different. -5 is the closest multiple to 5. -7 - -5 = -2. -2 is now the answer for us. <-- This is basically how QB64 and C find their answer.
To help you guys visualize this result, and to showcase that it IS, indeed, a valid answer, let me channel my old math teacher's spirit:
"OK, guys, the first thing you need to realize is that there is no such thing as a negative numbers!" (I swear, I remember this lecture almost word for word from him, even though I haven't been in his class for over 30 years now.)
"You guys are all broke. Right?" (And of course, we'd all nod affirmative.) "Then let's say I give you guys all $5 each, and you go out and spend it. How much money do you have after that?" (He'd give us a moment to think about that, and then continue.) "You sure as hell don't have NEGATIVE $5 in your pocket. If you do, pull it out and show it to me! What you do have, however, is now $5 in debt! It's a positive number -- just a positive number in a negative direction!!"
"Draw a line from negative 10 to 10 here on the blackboard."
"Now, count the dots from 5 to -7. How many of them are there?"
(12, one of us would answer with glee! Finally a math problem we could know the answer to!)
"And if you make a mark on that graph at every 5 points, how many points are left over between the -5 and the -7?"
(Two! Two! Two! Several of us would now shout the answer to his question.)
"But in what direction is that -7, in relation to your minus 5?" He'd really make a point to stress this part...
(It's to the left of it! We'd answer.)
"And left is what, on this line?" He'd ask, once again giving us a moment to soak in his words. "It's negative," he'd answer for us. "That means the answer has to be negative as well -- which makes it negative two. Remember... Negative is just the direction that you're traveling in -- in this case, it's to the left."
-7 MOD 5 = -2.
Which made perfect sense to me, after he explained it in such a simple manner. The distance between -7 and 5 is 12. 12 MOD 5 is 2... But it's going in a negative direction, so the answer has to be -2.
^ And that's basically the logic behind how QB64 and C both come up with their values for MOD.
If one needs positive values as a result from MOD, simply write a small function to get the answer in a format which you can work with:
All credit for this explaination goes out to the spirit of D.J. Keith -- best math teacher ever! Any lack of understanding, or failure to pass across his teachings is completely the fault of Pete. Everyone feel free to blame him.
And with that explanation out of the way, let's talk about MOD.
What is it? It's a very common math function that return the remainder from division.
How does one make use of it? It's rather simple to implement, just like addition or multiplication. X = 13 MOD 4..... compare that to.... X = 13 * 4.... Exact same syntax/usage.
So why are we discussing it now? Because of the topic here: Operator MOD (qb64phoenix.com)
Chris, the original poster of the topic link above, insists that MOD is broken and giving the wrong answer.
Quote:There is only one correct result.
Now, one would think when dealing with math, the above assumption has to be correct. There can only be one right answer to any mathematical result! Right?
Then what's the SQR(4)??
QB64 will quickly and happily tell us that the answer is 2! My math teacher would count that answer as being half wrong, as the answer is BOTH +2 and -2. (2 * 2 = 4. -2 * -2 = 4) Both are valid square roots for SQR(4). Unfortunately, QB64 only gives us one answer to this function -- the positive value.
By the same token, MOD is one of those operators which can also return different answers. In fact, various programming languages will each handle the result that it gives you, differently. 13 MOD 4 will *always* be 1, but -7 MOD 5 might be either -2 or 3.
Now, how in the heck does one get those various results??
One language might follow the ruleset (our remainder has to be a value from 0 to our number). For the language with this ruleset for mod, the answer for -7 MOD 5 would *have to be* 3. After all, -2 isn't even in the list of possibilities! It only considers 0 to 4 to be valid remainders for any number divided by 5. Basically the way they work is:
1) Find the largest multiple of your denominator that is less than the base number, subtract it, and use it to get the remainder. For 13 MOD 5, it'd find 10 to be the closest multiple of 5 smaller than 13, and then it'd subtract it. 13 - 10 = 3... 3 is the remainder for 13 MOD 5.
Now, in the case of -7 MOD 5, this type of ruleset would choose -10 as the closest multiple of 5, smaller than our number -7, and then it'd subtract it. -7 - -10 = 3. (negative seven minus negative ten = 3, just in case those signs don't show up readable for anyone.)
That's a perfectly valid interpretation of the answer, and it's not wrong at all. Unfortunately, it's also not how QB64 (or C, which we translate to by the way) deals with the math, so that's all the explaination I'm going to go into for the other result.
2) For QB64 (and for C itself), the rule that is in place for finding the remainder with MOD is basically: Find the closest multiple to your denominator, subtract it, and the result is your remainder.
Now, in the case of 13 MOD 5, the answer is exactly the same. 10 is the closest multiple to 5. 13- 10 = 3. 3 is, of course, the remainder.
But, in the case of -7 MOD 5, we see something different. -5 is the closest multiple to 5. -7 - -5 = -2. -2 is now the answer for us. <-- This is basically how QB64 and C find their answer.
To help you guys visualize this result, and to showcase that it IS, indeed, a valid answer, let me channel my old math teacher's spirit:
"OK, guys, the first thing you need to realize is that there is no such thing as a negative numbers!" (I swear, I remember this lecture almost word for word from him, even though I haven't been in his class for over 30 years now.)
"You guys are all broke. Right?" (And of course, we'd all nod affirmative.) "Then let's say I give you guys all $5 each, and you go out and spend it. How much money do you have after that?" (He'd give us a moment to think about that, and then continue.) "You sure as hell don't have NEGATIVE $5 in your pocket. If you do, pull it out and show it to me! What you do have, however, is now $5 in debt! It's a positive number -- just a positive number in a negative direction!!"
"Draw a line from negative 10 to 10 here on the blackboard."
Code: (Select All)
|.........0.........|
-10 10
"Now, count the dots from 5 to -7. How many of them are there?"
(12, one of us would answer with glee! Finally a math problem we could know the answer to!)
"And if you make a mark on that graph at every 5 points, how many points are left over between the -5 and the -7?"
(Two! Two! Two! Several of us would now shout the answer to his question.)
"But in what direction is that -7, in relation to your minus 5?" He'd really make a point to stress this part...
(It's to the left of it! We'd answer.)
"And left is what, on this line?" He'd ask, once again giving us a moment to soak in his words. "It's negative," he'd answer for us. "That means the answer has to be negative as well -- which makes it negative two. Remember... Negative is just the direction that you're traveling in -- in this case, it's to the left."
-7 MOD 5 = -2.
Which made perfect sense to me, after he explained it in such a simple manner. The distance between -7 and 5 is 12. 12 MOD 5 is 2... But it's going in a negative direction, so the answer has to be -2.
^ And that's basically the logic behind how QB64 and C both come up with their values for MOD.
If one needs positive values as a result from MOD, simply write a small function to get the answer in a format which you can work with:
Code: (Select All)
FUNCTION ModPositive&& (number as _INTEGER64, number2 AS LONG)
temp&& = number MOD number2
IF temp&& < 0 THEN temp&& = temp&& + number
ModPositive = temp&&
END FUNCTION
All credit for this explaination goes out to the spirit of D.J. Keith -- best math teacher ever! Any lack of understanding, or failure to pass across his teachings is completely the fault of Pete. Everyone feel free to blame him.