Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
didnt realize there was a seperate scripting forum - maybe we could get the post moved?
Copy link to clipboard
Copied
moved
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
moved.... but where?
A solution for this bug?
Copy link to clipboard
Copied
I just tested this in After Effects CS6, and it works fine there with multiple parameters (I'm testing with six).
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
> 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.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more