Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Javascript math.max bug in AE cs5

New Here ,
Apr 03, 2012 Apr 03, 2012

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.

TOPICS
Scripting
3.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 03, 2012 Apr 03, 2012
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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2012 Apr 04, 2012

Math.max takes a series of integers as arguments, not an array.

If you feed it an array it returns a "NaN" error.

the exact some code evaluated in a browser works fine:

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_max  < change the Math.max to (8,26,22) instead of (5,10)

....

correction - not integers, it should work with floats too - i'm just not using ints in this case

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 04, 2012 Apr 04, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 04, 2012 Apr 04, 2012

didnt realize there was a seperate scripting forum - maybe we could get the post moved?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Apr 04, 2012 Apr 04, 2012

moved

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 04, 2012 Apr 04, 2012

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 05, 2012 Apr 05, 2012

I thought that too, but if its only taking 2 arguments, why would 8,26,22 return 22?

It would have to be taking the first and last arguments, you would think it would take the first 2 and ignore the rest.

For this script i never need to compare anything different then 3 numbers, so i just wrote a function that does that.

But Yea - im thinking something is definitely wrong here, seeing as the exact same code works in every browser ive tested, but not in AE

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 26, 2013 Jan 26, 2013

moved.... but where?

A solution for this bug?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mar 12, 2013 Mar 12, 2013

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 13, 2013 Mar 13, 2013

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);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mar 13, 2013 Mar 13, 2013
LATEST

> 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines