Tech Tip: Arithmetic expressions in 4D
PRODUCT: 4D | VERSION: 2004 | PLATFORM: Mac & Win
Published On: February 14, 2007
4th Dimension follows a strict left-to-right evaluation of operator precedence, regardless of algebraic order.
If you need to enforce operator precedence, use parenthesis.
For example, the following code would probably not work as the developer expected:
Here is a simplified version:
Using parentheses for emphasis, here is how this expression is evaluated:
This code will not even compile because it is a syntax error to add a text value to a number value.
Generally when you are unsure of precedence, or in this case, associativity, it is best to make the code clear by using parenthesis.
So instead of using this:
Write it this way. It will compile and read cleaner:
For more information on operator precedence in 4D see:
http://www.4d.com/docs/CMU/CMU02014.HTM
If you need to enforce operator precedence, use parenthesis.
For example, the following code would probably not work as the developer expected:
[Test]ModeDb:=Num(Compiled application) * "Comp" + Num(Not(Compiled application)) * "Int."
Here is a simplified version:
[Test]ModeDb:= Num * Text + Num * Text
Using parentheses for emphasis, here is how this expression is evaluated:
[Test]ModeDb:= ( ( Num * Text ) + Num ) * Text
This code will not even compile because it is a syntax error to add a text value to a number value.
Generally when you are unsure of precedence, or in this case, associativity, it is best to make the code clear by using parenthesis.
So instead of using this:
[Test]ModeDb:= Num * Text + Num * Text
Write it this way. It will compile and read cleaner:
[Test]ModeDb:= ( Num * Text ) + ( Num * Text )
For more information on operator precedence in 4D see:
http://www.4d.com/docs/CMU/CMU02014.HTM