Groups > Multimedia > Adobe After Effects expressions > Re: Detecting Odd or Even numbers




Detecting Odd or Even numbers

Detecting Odd or Even numbers
Sat, 24 Nov 2007 22:53:08 -080
How can I tell if somethings an Odd or Even number? Or, if I divide something by
2, how can I tell if it's an integer?

Looking for something like:
if (x==even) 100; else 0

Thx
Post Reply
Re: Detecting Odd or Even numbers
Sun, 25 Nov 2007 02:00:19 -080
theNumber = effect("Slider Control")("Slider") / 2;
if (theNumber == Math.round(theNumber)) 100; else 0;

Post Reply
Re: Detecting Odd or Even numbers
Sun, 25 Nov 2007 05:45:10 -080
Post Reply
Re: Detecting Odd or Even numbers
Sun, 25 Nov 2007 08:52:18 -080
One caveat: the expression only tests to see if the number is an even integer.
It won't distinguish between odd integers and non-integers. You'd have to do a
second test for that, first adding 1 to your number, then using Paul's
expression as written.

On another note, I ran into what looks like a bug in AE CS3 when testing these
expressions. Here's what I did: I added two slider controls to a layer and added
Paul's expression to one of the sliders, making it reference the other. Then I
clicked on the value of the slider that was being used to drive the expression,
and used the up and down arrow keys to increment the slider value. The
expression works as it should, evaluating to 100 on even values and 0 on odd
values.

Then I held down the command key while using the up and down arrow keys to
increment the slider in tenths. Suddenly the expression evaluated to zero for
any non-zero value, even for even integers. It's as if their is a very tiny
imprecision in the 1/10 increment that isn't reflected in the displayed value.
If I switched back to incrementing by 1, the expression sometimes still
evaluates to zero until I decrement all the way down to zero, at which point it
starts behaving properly again. Even crazier, if I "break" it by
incrementing in tenths, selecting the value and typing in the even integer value
doesn't seem to fix it unless I first enter a different value, then re-enter the
original even integer.

Post Reply
about | contact