• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Order Of Operations

samspade

Member
I've been looking into some of the basics of GML recently and when I was looking at operator precedence I came across this page in the manual: Expressions. It claims that: "When doing multiple operations in a single expression, it is very important that you use brackets () to separate out the order of operation, as different platforms may perform them differently if not explicitly stated in this way."

This makes some sense, however, it goes on to add that:

Code:
a = b == c || d;
a = (b == c || d); //better
a = ((b == c) || d); //best
That seems ridiculous. The only reason for the middle one being better is if this is a possible evaluation of the first line:

Code:
(a = b) == c || d;
Is it really this undefined? I would have assumed there was at least some consistency for order across all platforms on basic stuff like assignment or comparisons. For example:

Code:
//would this ever be evaluated as
if 5 * 2 == 10 * 1 {
}

//this
if 5 * (2 == 10) * 1 {
}
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
The example is meant to illustrate a point, not be taken as a "real world" example. :)

Basically, the manual is saying, "don't rely on order of operations across all platforms as that may not work or may be changed behind the scenes". It's good practice to be explicit and it prevents future potential errors when you compile using YYC or on another target platform.
 
Top