We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
% modulo 4%3 = 1%3 != (Read 3227 times)
% modulo 4%3 = 1%3 !=
Oct 15th, 2007, 12:00pm
 
Mathematically,
the integer sequence
(..,-5,-4,-3,-2,-1, 0, 1, 2, 3, 4, 5,..) modulo 3
becomes
(.., 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2,..)

In Processing,
(..,-5,-4,-3,-2,-1, 0, 1, 2, 3, 4, 5,..) % 3
becomes
(..,-2,-1, 0,-2,-1, 0, 1, 2, 0, 1, 2,..)

This is equivalent (and correct), but a consequence is that:

(5%3)==(2%3) is true,  but (2%3)==(-1%3) is false

It would be nice to have a non-negative version of % .

I built my own method to do this, but it would be nice to have it in the language
(maybe it could be called %%).
Re: % modulo 4%3 = 1%3 !=
Reply #1 - Oct 15th, 2007, 5:18pm
 
The confusion comes from calling "%" the "modulo" operator, which is common but technically incorrect, because it's really the "remainder" operator, such that if x/y=z and x%y=w, then z*y+w=x.  (Java/Processing doesn't have a true modular arithmetic operator - as you've already discovered)
Re: % modulo 4%3 = 1%3 !=
Reply #2 - Feb 10th, 2009, 8:40am
 
can you post your method?  i just tore some hairs out wondering why an equation wasn't working, and this is why...  

or is there another convention people use to wrap numbers around in both directions?
Re: % modulo 4%3 = 1%3 !=
Reply #3 - Feb 10th, 2009, 9:33am
 
i wrote this quickly, just curious how you did it.  mine doesn't handle all cases, just quick fix.

Code:

float mod(float a, float b) {
if (a>=0) {
return a%b;
} else {
return a+b;
}
}


detailed description of the particulars:
http://mindprod.com/jgloss/modulus.html
Re: % modulo 4%3 = 1%3 !=
Reply #4 - Feb 10th, 2009, 6:25pm
 
I think your method is incorrect as written.
"a+b" should be "(a%b)+b"

float mod(float a, float b) {
 if (a>=0) {
   return a%b;  
 } else {
   return a+b; // <<-- (a%b)+b
 }
}

---
For int or long values,
I have been using those methods (below):
---
The 1st one is for integer values.
The 2nd one is for long values.
(long is not properly supported in Processing, so I had to use this modified method)
---
The 1st one could be modified for float values.
---

// return k mod m
public static int mod(int k, int m) {
 k = k % m;
 if(k < 0) k+=m;
 return(k);
}//mod()

// return k mod m
public static long mod(long k, long m) {
 boolean minus=(k<0);
 if (minus) k=-k;
 k = k-((k/m)*m);
 if (minus) k=m-k;
 return(k);
}//mod()

Re: % modulo 4%3 = 1%3 !=
Reply #5 - Feb 10th, 2009, 7:39pm
 
ah yes, good catch.  thanks.  
Re: % modulo 4%3 = 1%3 !=
Reply #6 - Feb 10th, 2009, 9:46pm
 
alphachapmtl wrote on Feb 10th, 2009, 6:25pm:
"a+b" should be "(a%b)+b"

Not really, as the result is 'b' when a%b is 0!
Your function is correct, though.
Re: % modulo 4%3 = 1%3 !=
Reply #7 - Feb 11th, 2009, 12:02am
 
Hey, you`re right!(didn`t check that case...)
Re: % modulo 4%3 = 1%3 !=
Reply #8 - Feb 11th, 2009, 12:06am
 
So, for float values, better use:

// return k mod m
public static float mod(float k, float m) {
 k = k % m;
 if(k < 0) k+=m;
 return(k);
}//mod()

or

// return a mod b
public static float mod(float a, float b) {
 a = a % b;
 if(a < 0) a+=b;
 return(a);
}//mod()
Page Index Toggle Pages: 1