ADOBE AFTER EFFECTS: Forum Expressions Tutorials Creative Cloud

Tell if a value is positive or negative without IF statement.

COW Forums : Adobe After Effects Expressions

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Marc Rühl
Tell if a value is positive or negative without IF statement.
on Mar 28, 2017 at 5:24:30 pm

I'm trying to determine if my value is positive or negative. Of course I could easily do an IF ELSE statement, but I need this expression to work on 400 layers simultaneously and IF ELSE has proven to slow things down immensely, so I try to find a way without using it.
In an ideal world I would get 1 if my value is positive and -1 if it's negative, but of course anything else like 1 and 0 or something like this would be great too.
I wrote this tiny expression, which works exactly like I want it to, but fails if the value is 0. Do anybody here know a better way?

PosNeg = Math.abs(value)/value;

http://www.schwarzgrau.com


Return to posts index

Kevin Camp
Re: Tell if a value is positive or negative without IF statement.
on Mar 28, 2017 at 6:43:27 pm

PosNeg = normalize(value);

wouldn't give an error with 0, but would return NaN ('not a number').

maybe try and catch....
try {
PosNeg = Math.abs(value)/value;
} catch (err) {
0;
}


Kevin Camp
Art Director
KCPQ, KZJO & KRCW


Return to posts index

Marc Rühl
Re: Tell if a value is positive or negative without IF statement.
on Mar 29, 2017 at 1:31:54 pm

Perfect! Didn't know try and catch before. I guess I should read into it. Thank you a lot Kevin.

http://www.schwarzgrau.com


Return to posts index


Dan Ebberts
Re: Tell if a value is positive or negative without IF statement.
on Mar 28, 2017 at 7:05:17 pm

value >= 0

should give you 0 if negative, 1 if positive (or zero)

Dan



Return to posts index

Marc Rühl
Re: Tell if a value is positive or negative without IF statement.
on Mar 29, 2017 at 1:33:29 pm

Sorry Dan, I don't get it. I only see how I could determine if a number is positive or negative using this code in an IF statement, but I didn't want to use an IF statement.

http://www.schwarzgrau.com


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2017 CreativeCOW.net All Rights Reserved
[TOP]