Advertisement

Modulus

Started by October 09, 2002 07:33 AM
8 comments, last by Puzzler183 22 years, 4 months ago
How can I calculate the modulus of two numbers using only +, -, *, /, sqrt(), sin(), cos(), tan(), log(), and ln()?
How about:

modulus = a - (a / b) * b

Has to be integer division in order to work...
Advertisement
Nope... Regular division only.
modulo simply is the last part you can not divide any more.

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 )
Advertisement
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<
    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
Yes Beerhunter, I'll try that... Anyway, I don't have int() function.

About Beerhunter's function, it works for the numerator of 2 and 1 but not for 3.

[edited by - Puzzler183 on October 11, 2002 7:49:05 AM]

This topic is closed to new replies.

Advertisement