Skip to main content
Inspiring
July 16, 2014
Answered

AS3 Math issue

  • July 16, 2014
  • 3 replies
  • 280 views

If i have this code:

var test: Boolean;

test = false;

if (2 == 2 == 1)

{

    test = true;

}

trace (test);

Result: true.

var test: Boolean;

test = false;

if (2 == 2 == 2)

{

    test = true;

}

trace (test);

Result: false

var test: Boolean;

test = false;

if (2 == 2 == 3)

{

    test = true;

}

trace (test);

Result: false.

Why is that?

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer Ned Murphy

This is not a Math issue, it is a matter of how a condfitonal test is carried out.   Conditions are tested as pairs (with a left to right hierarchy unless otherwise arranged using parentheses), meaning for 2 == 2 == whatever, first the test is performed to see if 2 == 2 ?, which is true, meaning the result evaluates as a "true".  Then the next comparison is performed using the result of the first... which would be...  true == whatever ?. which depends on "whatever" is.  A 1 evaluates as true as far as logic goes, but any other number does not, so true == 1 = true, true == 0,2,3,4,5.... = false

You will find nothing passes the test if you reverse the order of the numbers.

If you want to do compound conditional tests you are best to do it in pairs and/or use parentheses to define the order in which evaluations are managed.

3 replies

Ned Murphy
Ned MurphyCorrect answer
Legend
July 16, 2014

This is not a Math issue, it is a matter of how a condfitonal test is carried out.   Conditions are tested as pairs (with a left to right hierarchy unless otherwise arranged using parentheses), meaning for 2 == 2 == whatever, first the test is performed to see if 2 == 2 ?, which is true, meaning the result evaluates as a "true".  Then the next comparison is performed using the result of the first... which would be...  true == whatever ?. which depends on "whatever" is.  A 1 evaluates as true as far as logic goes, but any other number does not, so true == 1 = true, true == 0,2,3,4,5.... = false

You will find nothing passes the test if you reverse the order of the numbers.

If you want to do compound conditional tests you are best to do it in pairs and/or use parentheses to define the order in which evaluations are managed.

gibsyAuthor
Inspiring
July 16, 2014

Thanks.

Ned Murphy
Legend
July 16, 2014

You're welcome