Operators

Revision as of 08:04, 16 November 2015 by Bbecane (talk | contribs)


An operator is a symbol, such as a plus sign (+), that represents a computational operation or action such as addition or comparison. Analytica includes the following sets of standard opera- tors.

Arithmetic operators: The arithmetic operators apply to numbers and produce numbers:

Operator Meaning Examples
x+y plus 3+2→5
x-y binary minus 3-2→1
-x unary minus -2→-2
x*y product 3*2→6
x/3 or x÷y division [math]\displaystyle{ 3/2 (=\frac{3}{2})→1.5 }[/math]
x^y to the power of 3^2=32 →9

4^.5 =[math]\displaystyle{ 4^{\frac{1}{2}} →2 }[/math]

Comparison operators: Comparison operators apply to numbers and text values and produce Boolean values.

Operator Meaning Examples → (1=true, 0=false)
< less than 2 < 2

'A'<'B'

→ 0

→ 1

<= less than or equal to 2 <= 2

'ab'<='ab'

→ 1

→ 1

= equal to 100=101

'AB'='ab'

→ 0

→ 0

>= greater than or equal to 100 >= 1

'ab'>='cd'

→ 1

→ 0

> greater than 1 > 2

'A'>'a'

→ 0

→ 1

<> not equal to 1 <> 2

'A'<>'B'

→ 1

→ 1

Alphabetic ordering of text values: When applied to text values, the comparison operators, >, >=, >=, and <, use a region-specific collation order which takes into account conventions used in your own language and country. The specific collation order used is determined by the TextLocale system variable. If you want to change the collation order, bring up the Object Window for TextLocale by selecting it from the Definition →System Variables menu, with nothing selected and while in edit mode.

In general, the ordering of text may be different from the ordering implied by the numeric ASCII codes of the characters. For example, 'de' < 'dé' < 'df', whereas the ASCII code for 'é' comes after the ASCII code for 'f'. If you want text comparisons to use simple ASCII ordering, you can set TextLocale to be ANSI.

Generally, comparisons follow the ordering you would find in a dictionary. For example:

'Analytica' < 'Excel' →1 (True)

In almost all collation orders, digits precede letters:

'9' < 'A' →1 (True)

The comparison operators are case-sensitive, so that uppercase letters precede lowercase letters:

'Analytica' > 'excel' → 0 (False)

If you want to alphabetize without regard to case, use TextUppercase or TextLowerCase to convert letters to the same case.

TextUpperCase('Analytica') < TextUpperCase('excel') →1 (True)

When two text values do not contain identical character sequences, they are considered to be unequal, even if they represent the exact same text. This can happen in Unicode with combining characters. For example, an accented 'é' can be either Chr(233) or Chr(769)&'e', where the Unicode Chr(769) is the combining acute accent, which modifies the character that follows. Although these alternate representations of 'é' are non-equal, they will have the same inequality comparisons with all other text, as if they are as closely adjacent in the sort order as is possible.

SortIndex(d,i), Sort(d,i) and Rank() also use the same collation ordering to determine the ordering of text values. Each also has an optional parameter to compare them in a case-insensitive fashion.

Logical operators: Logical operators apply to Boolean values and produce Boolean values.

Operator Meaning Examples
b1 AND b2 true if both b1 and b2 are true,

otherwise false

5>0 AND 5>10 False
b1 OR b2 true if b1 or b2 or both are true,

otherwise false

5>0 OR 5>10 True
NOT b true if b is false,

otherwise false

NOT (5>0) False

Scoping operator (::): It is possible that a model created in a previous release might contain a variable or function with the same identifier as a new built-in variable or function. In this situation, an identifier name appearing in an expression might be ambiguous.

Prepending :: to the name of a built-in function causes the reference to always refer to the built-in function. Otherwise, the identifier refers to the user’s variable or function. With this convention, existing models are not changed by the introduction of new built-in functions.

Example: Suppose a model from an older release of Analytica contains the user-defined function Irr(Values, I). Then:

Irr(Payments, Time) User’s Irr function
::Irr(Payments, Time) The built-in function

Operator binding precedence

A precedence hierarchy resolves potential ambiguity when evaluating operators and expressions. The precedence for operators, from most tightly bound to least tightly bound is:

  1. parentheses ()
  2. function calls
  3. Not
  4. @I, \A, \[I]A, #R.
  5. A.I
  6. A[I=x]
  7. Attrib of Obj
  8. ^
  9. - (unary, negative)
  10. *, /
  11. +, - (binary, minus)
  12. m..n
  13. <, >, <=, >=, =, <>
  14. And
  15. Or
  16. & (text concatenation)
  17. :=
  18. If … Then … Else, Ifonly … Then … Else, Ifall … Then … Else
  19. Sequence of statements separated by semicolons, sequence of elements or parameters separated by commas

Within each level of this hierarchy, the operators bind from left to right (left associative).

Examples: The following arithmetic expression:

1 / 2 * 3 - 3 ^ 2 + 4

is interpreted as:

((1 / 2) * 3) - (3 ^ 2) + 4

The following logical (Boolean) expression:

IF d + e < f ^ g or a and b > c THEN x ELSE y + z

is interpreted as:

IF (((d + e) < (f ^ g)) or (a and (b > c))) THEN x ELSE (y + z)

See Also

Comments


You are not allowed to post comments.