|
-
Aug 29th, 2002, 04:36 PM
#1
How does the MOD function work.
I am looking for a mathematical function which can duplicate what the MOD function can do in Visual Basic. Basically I want to a function which on supplying two numbewrs will return just the remainder as an integer. Such that:
17 / 3 = 5 r2
(3 * 5) + 2 = 17
2 = 17 - (3 * 5)
a = Integer Part Of Answer
d = Denominator
n = Numerator
r = Remainder
r = n - (a * d)
Does anyone have any ideas?
-
Aug 29th, 2002, 05:05 PM
#2
Hyperactive Member
a = Int(n/d)
r = n - Int(n/d)
If I agree with you today, don't get used to it.
-
Aug 29th, 2002, 05:07 PM
#3
Addicted Member
I don't think there could be a pure mathematical function to do what you want, because what you're asking for is abstract (such as there not being a formula for the greatest common divisor of a number)... I'll look into this, sounds interesting.
Not at all related to sheep...
-
Aug 29th, 2002, 06:51 PM
#4
Addicted Member
Closest thing is like you have:
Snakeeyes was close. 
Destined
-
Aug 29th, 2002, 07:37 PM
#5
Is this what you want?
VB Code:
Private Function MY_MOD(ByVal My_Before As Double, ByVal ByWhat As Double) As Double
While My_Before > 0
My_Before = My_Before - ByWhat
Wend
While My_Before < 0
My_Before = My_Before + ByWhat
Wend
MY_MOD = My_Before
End Function
-
Aug 29th, 2002, 09:39 PM
#6
Fanatic Member
are you guys telling me that there is no mod function in VB? btw if you want a perfect mod function, a note that mod function attends to real numbers and negative numbers so your algorithms may break down when you do stuff like -1.23 mod 2.7 or something. and watch out mod 0 (doesn't work).
Massey RuleZ! ^-^__  Cheers!  __^-^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!
-
Aug 29th, 2002, 09:57 PM
#7
Hyperactive Member
you can't get a remainder after floating point division... well I suppose you could, but what exactly would be the point?
If I agree with you today, don't get used to it.
-
Aug 30th, 2002, 02:02 AM
#8
I'm curiouse as to how the computer actually does this- If there is no pure mathematical solution. Does it use bitwise or does it change the result to a string and slice off the everything to the right of the decimal point.
I know that using the >> bitwise operator (which shifts bits in a number a specified number of places to the right) in C ++ returns the integer portion of 2^n of the number shifted such that:
15 >> 2 = 3 equivelent to Int(15/2^2)
this shifts the bits two places to the right:
1111 >> 2 = 11
in general:
x >> n = Int(x / 2^n)
The question is, can this be dupilcated in pure maths?
-
Aug 30th, 2002, 04:04 AM
#9
Addicted Member
Earlier I was saying there is no purely mathematical function for what you want, I never said there was no VB code for it...
Anyway, the way the computer does it is through trial division.
Say you want the remainder when you divide x by y. The computer will subtract y from x as many times as it can before result of a subtraction is negative. In the case of 20 divided by 3, 3 can be subtracted 6 times before a negative number is created. Then, the computer takes the 3 x 6 away from the 20, to get a remainder of 2. That's essentially how the Mod function works. There is NO mathematical equivalent, I checked.
Division is done in the same way, except after the computer has subtracted 6 3s from 20, it takes the remainder, divides that by 3 (making 0.666666666666...), and adds that to the 6, leaving 20 / 3 = 6.666666666666...
If that's not what you were asking for then sorry
Not at all related to sheep...
-
Aug 30th, 2002, 04:31 AM
#10
Addicted Member
Originally posted by visualAd
The question is, can this be dupilcated in pure maths?
Okay, I guess my question is what you define as pure math. If you want mathematical functions only (no Int(n/d) stuff), then I belive NotLKH's code is what you want.
If this isn't, then I'm really wondering what you mean by your statement, since multiplication (and division) doesn't really count - it all comes down to adding postive and negative numbers in the end. 
Destined
-
Aug 30th, 2002, 07:18 AM
#11
Fanatic Member
Originally posted by snakeeyes1000
you can't get a remainder after floating point division... well I suppose you could, but what exactly would be the point?
this is much like NotLKH's way, you just keep on subtracting and adding until you get an answer thats smaller/bigger than the divisor and then that is the reminder. don't ask me what it is for, hey i didn't invent mod. but i am sure its good for somethin' -- integers isn't everything, i am sure somewhere in mechanics they uses this.
by the way, mod is already a math function, so why the fuss about finding the mod equilvalent?
by the way the Int(n/d) equilvalent in math is [n/d], where [x] denotes the largest integer less than or equal to x. [x] is called the largest integer function.
Massey RuleZ! ^-^__  Cheers!  __^-^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!
-
Aug 30th, 2002, 07:42 AM
#12
The mod function is actually used to quite a large extent in everyday life. Take your weekly shopping trip for example. Every time a barcode is scanned at the check out the MOD function is used to calculate the check digit i.e. the last digit of the barcode.
Heres an example. This barcode is from a can of baked beans:
2500 0153
3 is the check digit
[list=1][*]Find the sum of all the numbers but the check digit. Alternate numbers must be multiplied by 3. If the barcode has an even number of digits the first number must be multiplied by 3.
(3 * 2) + 5 + (3 * 0) + 0 + (3* 0) + 1 + (3 * 5) = 27
[*] Find the remainder when divided by 10
27 Mod 10 = 7
[*] Take this number from 10. If the result is 0 this is your check digit.
10 - 7 = 3[/list=1]
If the check digit does not agree with the result of this method the barcode is invalid.
-
Aug 30th, 2002, 09:59 AM
#13
Fanatic Member
we are discussing mod for real and negative numbers, such as 5.4 mod -3.2 and its uses in real applications.
Massey RuleZ! ^-^__  Cheers!  __^-^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!
-
Aug 30th, 2002, 10:14 AM
#14
I guess what I am looking for is a mathematical function which I can plug the numerator and denomintor into and get the raminder. I am only at colleg so I'm not much of a mathmetician. Some things like infinity, infatesimal, recurring decimals and irrational numbers confuse me. As they don't seem that logical.
I don't really care what its uses are. I'm just curious.
-
Aug 30th, 2002, 10:26 AM
#15
Fanatic Member
sure, let f(A,B) be reminder, A be dividend and B be divisor, then
f(A,B)=A-B*[A/B]
and [x] denotes the largest integer less than or equal to x
or since mod is a valid mathematical function, much easier:
f(A,B)=A mod B
Massey RuleZ! ^-^__  Cheers!  __^-^ Massey RuleZ!
Did you know that...
The probability that a random rational number has an even denominator is 1/3 (Salamin and Gosper 1972)? This result is independently verified by me (2002)!
-
Aug 30th, 2002, 03:49 PM
#16
Addicted Member
Originally posted by bugzpodder
sure, let f(A,B) be reminder, A be dividend and B be divisor, then
f(A,B)=A-B*[A/B]
That's the form they write it in number theory. That was such a cool class. 
Destined
-
Aug 31st, 2002, 05:55 AM
#17
Frenzied Member
What you are really doing is repeated subtraction - that's what division is.
Loop while (numerator - denominator) > denominator
in c:
Code:
int mod(int n, int d){
while( (n-=d)>d); // iterate subtraction
return n;
}
-
Aug 31st, 2002, 06:08 AM
#18
Jim,
You define n, and d as Integer. What Should be the standard for Mod?
VB Mod can handle Decimal, ie... 19 Mod 6.7,
But it returns a somewhat haphazard rounded result,
ie 5 = 19 Mod 6.7
{ 6.7 Times 2 = 13.4, Rounded UP to 14, so 19 - 14 = 5, or viewed from another angle
5 = 19 Mod 6.7 = Int(19 - 13.4)}
BTW,
In Your Code, What happens when N < 0?
-Lou
-
Sep 3rd, 2002, 03:03 AM
#19
I'm slightly confused by this whole thread. mod is a mathematical operator. (I hesitate to use the word function since it's an infix operator, making it more like + than f, even though it's just convention that stops us writing +(a,b) instead of a+b)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|