Skip navigation
Currently Being Moderated

Javascript math.max bug in AE cs5

Apr 3, 2012 12:06 PM

Sombody please explain to me why the following script:

 

{

var test = 0;

test = Math.max (8,26,22);

alert (test);

}

 

 

outputs "22" in after effects CS5.

 

am i just missing something really simple or is this a bug?

the same code in a browser works fine.

 
Replies
  • Currently Being Moderated
    Apr 3, 2012 10:55 PM   in reply to joeydanna1
    am i just missing something really simple

     

    Yes, you are. You are simply assigning the value 3 times over rather than declaring an array and naturally the variable only holds the last assigned value. Arrays belong in rectangular brackets, so your declaration needs to look like Math.max([8,26,22]). Read up on JavaScript fundamentals rather than prematurely calling things a bug when they aren't.

     

    Mylenium

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 4, 2012 10:25 AM   in reply to joeydanna1

    Sorry, my bad. One of those early morning posts, including the fact that I simply assumed it would pertain to expressions... *oops* Should have waited after my first tea... Anyway, why not ask in the scripting forum?

     

    Mylenium

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 4, 2012 8:23 PM   in reply to joeydanna1

    moved

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 4, 2012 8:45 PM   in reply to Todd_Kopriva

    That is really strange. It works in CS4, but not in CS5 or CS5.5. My JavaScript reference says that Math.max() only takes two parameters, but w3schools clearly shows it taking multiple parameters. One workaround of course, would be Math.max(Math.max(8,26),22), which seems to work.

     

    Dan

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 26, 2013 6:09 AM   in reply to Todd_Kopriva

    moved.... but where?

    A solution for this bug?

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 12, 2013 4:54 PM   in reply to joeydanna1

    I just tested this in After Effects CS6, and it works fine there with multiple parameters (I'm testing with six).

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 13, 2013 4:17 AM   in reply to Todd_Kopriva

    Yes, but it doesn't work in CS5.

    I noticed that in CS5 the result of the Math.max function depends on the order of the arguments as well as their values, but I always only use it with 2 arguments so I never really cared.

    It looks like each value is compared to the first one, and the value returned is the last argument found that is greater than the first one.

     

    So Math.max(1.5, 4, 3, 2, 1) will return 2 because the arguments following are lower than 1.5

    So Math.max(2.5, 4, 3, 2, 1) will return 3 because the arguments following are lower than 2.5

    and Math.max(2.5, 3, 4, 2, 1) will return 4 because the arguments following are lower than 2.5

     

    So it's safer to do what Dan suggested and break the max calculations with a loop. something like:

     

    var values = [ ... an array of values ... ];

    var maxValue = values[0];

    for (var i=1; i<values.length; i++)

    {

        maxValue = Math.max(maxValue, values[i]);

    }

     
    |
    Mark as:
  • Currently Being Moderated
    Mar 13, 2013 12:03 PM   in reply to maxweel

    > Yes, but it doesn't work in CS5.

     

    My point was just to inform you that the bug has been fixed in CS6 and later.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points