Modulus
How can I calculate the modulus of two numbers using only +, -, *, /, sqrt(), sin(), cos(), tan(), log(), and ln()?
October 09, 2002 07:36 AM
How about:
modulus = a - (a / b) * b
Has to be integer division in order to work...
modulus = a - (a / b) * b
Has to be integer division in order to work...
modulo simply is the last part you can not divide any more.
so
will do it.
so
modulo(int a, int b){ while (a >= b) { a-=b; } return a; }
will do it.
-----The scheduled downtime is omitted cause of technical problems.
unsigned int divide(unsigned int divisor,unsigned int divident,unsigned int* modulus) { unsigned int division = 0; for(int i=31;i>=0;++i) { if(divisor>>i >= divident) { division |= 1<<i; division -= divident<<i; } } if(modulus) { *modulus = divisor; }}
does this work actually? just created it from my freaky brain. i know it doesn''t help you much, but i''m interested:D
"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
no, because
1) it''s an infinate loop
2) it doesnt return anything
3) the algo''s wrong (alright it may not be, but hey - you messed up 1 and 2
)
1) it''s an infinate loop
2) it doesnt return anything
3) the algo''s wrong (alright it may not be, but hey - you messed up 1 and 2

quote:
Original post by Anonymous Poster
How about:
modulus = a - (a / b) * b
Has to be integer division in order to work...
I used this method all of the time in calculator programs...
(modulus of a divided by b) = a - int(a/b)*b
This works on any number.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
well, its not infinite, just nearly (int does overflow at 2billions:D)
it returned the modulus yet:D but yes, the division i forgot to return..
the division should not get divident<
that should work now.. :D
"take a look around" - limp bizkit
www.google.com
it returned the modulus yet:D but yes, the division i forgot to return..
the division should not get divident<
unsigned int divide(unsigned int divisor,unsigned int divident,unsigned int* modulus) { unsigned int division = 0; for(int i=31;i>=0;--i) { if(divisor>>i >= divident) { division |= 1<<i; divisor -= divident<<i; } } if(modulus) { *modulus = divisor; } return division;}
that should work now.. :D
"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Can we use inverse trig functions?
acos(cos(pi*numerator/denominator))*denominator/pi
acos(cos(pi*numerator/denominator))*denominator/pi
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement