Author Topic: Order of Operations  (Read 952 times)

0 Members and 1 Guest are viewing this topic.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Order of Operations
« on: February 21, 2008, 09:01:34 pm »
I'm wondering what the order of operations are (and what all the built-in operations are), kind of in the style that this is in for Soldat server's pascal

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
Re: Order of Operations
« Reply #1 on: February 22, 2008, 12:35:02 pm »
For all I know the servers built in ooo is just the same as standard pascal. But honestly, do you really need to use it? Most of theese problems can be used with parantesis, and if not, the problem is usually easy to isolate
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Order of Operations
« Reply #2 on: February 22, 2008, 10:07:11 pm »
For all I know the servers built in ooo is just the same as standard pascal. But honestly, do you really need to use it? Most of theese problems can be used with parantesis, and if not, the problem is usually easy to isolate
just want to avoid having like ((((((((( all next to each other and its just nice to know when i ()s are not necessary and when they are

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Order of Operations
« Reply #3 on: February 22, 2008, 11:33:55 pm »
This is for Delphi, but it's similar for the scripting engine:

Quote
In complex expressions, rules of precedence determine the order in which operations are performed.

Precedence of operators
OperatorsPrecedence
@, notfirst (highest)
*, /, div, mod, and, shl, shr, assecond
+, -, or, xorthird
=, <>, <, >, <=, >=, in, isfourth (lowest)
An operator with higher precedence is evaluated before an operator with lower precedence, while operators of equal precedence associate to the left. Hence the expression

X + Y * Z

multiplies Y times Z, then adds X to the result; * is performed first, because is has a higher precedence than +. But

X - Y + Z

first subtracts Y from X, then adds Z to the result; - and + have the same precedence, so the operation on the left is performed first.

You can use parentheses to override these precedence rules. An expression within parentheses is evaluated first, then treated as a single operand. For example,

(X + Y) * Z

multiplies Z times the sum of X and Y.

Parentheses are sometimes needed in situations where, at first glance, they seem not to be. For example, consider the expression

X = Y or X = Z

The intended interpretation of this is obviously

(X = Y) or (X = Z)

Without parentheses, however, the compiler follows operator precedence rules and reads it as

(X = (Y or X)) = Z

which results in a compilation error unless Z is Boolean.

Parentheses often make code easier to write and to read, even when they are, strictly speaking, superfluous. Thus the first example could be written as

X + (Y * Z)

Here the parentheses are unnecessary (to the compiler), but they spare both programmer and reader from having to think about operator precedence.

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: Order of Operations
« Reply #4 on: February 23, 2008, 09:32:21 am »
Do you know if it is possible to create an operator in pascal?
if so, where to place it in the order of operations?
(to make a new := that also returns the new value, so i can use it within a if or anything like that)

or is it possible to rearrange the order of operations at all?
(it would make less ()s in my part to have =, <>, <, >, <=, >= done b4 the and, or, and xor)

(of course, referring to Soldat pascal)
doubtful, but i have to ask :)

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: Order of Operations
« Reply #5 on: February 23, 2008, 06:42:53 pm »
No to all of the above.

If you have issues with keeping things neat, consider doing something like this:

Code: [Select]
if (
  ((a = b) or (c = d)) and
  (d <> a)
) then begin
  //...
end;