Skip to main content
September 17, 2009
Question

Math functions

  • September 17, 2009
  • 1 reply
  • 515 views

Hello All,

I was working through some tutorials on math equations in Flash and had a question that I was hoping someone could explain. I did a trace on a math equation.

trace(5+5);

Which I got 10, I got that!

trace(5+5-2);

Which I got 8, I got that!

trace(5+5*2);

Which I got 15, I didn't get that, I was expecting 20.

or

trace(5+5/2);

I got 7.5, which I was expecting 5.

Can anyone explain how the math is working in those cases? I know I've been out of school for awhile. lol

Thanks.

This topic has been closed for replies.

1 reply

September 17, 2009

It's called operator precedence. * / have the same precedence, but are higher than + - which also have the same precedence. So, * will happen before +. If you want to control precedence use parenthesis:

5 + 5 * 2 is 10 + 5 = 15

If you want 20:

trace ((5 + 5) * 2);

September 17, 2009

Ahhhh!, I get it now, thanks for the clarification. Preciate it.