Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions Tell if a value is positive or negative without IF statement.

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

    Posted by Marc Rühl on March 28, 2017 at 5:24 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

    Marc Rühl replied 9 years, 1 month ago 3 Members · 4 Replies
  • 4 Replies
  • Kevin Camp

    March 28, 2017 at 6:43 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

  • Dan Ebberts

    March 28, 2017 at 7:05 pm

    value >= 0

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

    Dan

  • Marc Rühl

    March 29, 2017 at 1:31 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

  • Marc Rühl

    March 29, 2017 at 1:33 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

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy