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.
IndexProgramming Questions & HelpSyntax Questions › odd and even number
Page Index Toggle Pages: 1
odd and even number (Read 1592 times)
odd and even number
Jun 24th, 2008, 2:17pm
 
Hi,

is there a way that P5 can detect odd and even number?
let's say

if (sec > 1.0){
   x = 25;
 }
 
  if (sec > 2.0){
   x = 50;
   
 }

and I want to control this like, when "sec" variable is odd number function 1, and if it's even number function 2...etc.

If not I have to write the same thing each second...

'hope that my question is clear....
thank you.
Re: odd and even number
Reply #1 - Jun 24th, 2008, 2:52pm
 
is it an int? it looks like a float in your example

for ints, check the lowest bit

if ((sec & 1) == 0) {
 // even
} else {
 // odd
}

http://processing.org/reference/bitwiseAND.html

(if it's a float, cast it to an int before the test)
Re: odd and even number
Reply #2 - Jun 24th, 2008, 2:57pm
 
or you can use modulo functionality as in: http://processing.org/reference/modulo.html

simple code:
if(sec%2 == 0) //even
else //odd

hope it will help you Wink
Re: odd and even number
Reply #3 - Jun 25th, 2008, 3:41pm
 
Thanks for the replies!

actually, it doesn't seem to work out, but I don't believe that it's the even/odd number detect method.

the thing is that I have this impression that the millis() function can't detect the time at integer number.

//this is how I declared sec variables

float sec = millis() /1000.0;

//this will compare the integer numbers like,
//1.0, 2.0, 3.0.....etc.

 if (sec%2 == 0){
   x = 50;
 }else{
   x = 100;
 }

But I think the sec variables will never output 1.0 but rather 1.152749246205....something like that.

am I right?

if so, how can I write this binary function?
in even number second do this
and in odd number second do that,
in following the internal clock of my macBook of course...

thank you so much for helps so far anyway....

Re: odd and even number
Reply #4 - Jun 25th, 2008, 3:52pm
 
You can test the integer version of the value like this:

Code:
float sec = millis() /1000.0; 
if((int)sec%2==0)
{
//even.
}
else
{
//odd
}
Re: odd and even number
Reply #5 - Jun 25th, 2008, 4:43pm
 
it works perfectly,

thanks a lot!
Page Index Toggle Pages: 1