Difference between revisions of "Operators"

m
Line 2: Line 2:
 
<breadcrumbs>Analytica User Guide > Using Expressions > {{PAGENAME}}</breadcrumbs>
 
<breadcrumbs>Analytica User Guide > Using Expressions > {{PAGENAME}}</breadcrumbs>
  
 +
An infix '''''operator '''''is a symbol, such as a plus sign (1+2) or product (10*2), that operates on the values before and after it. A prefix operator, such as unary minus (-100) operates only on the value after it. Analytica includes these fairly standard opera tors, including arithmetic, comparison, and logical operators:
  
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:
'''Arithmetic operators''': The arithmetic operators apply to numbers and produce numbers:
 
 
{| class="wikitable"
 
{| class="wikitable"
 
!Operator
 
!Operator
Line 37: Line 37:
 
|}
 
|}
  
'''Comparison operators''': Comparison operators apply to numbers and text values and produce Boolean values.
+
=== Comparison operators ===
 +
 
 +
Comparison operators apply to numbers and text values and produce Boolean values.
 
{| class="wikitable"
 
{| class="wikitable"
 
!Operator
 
!Operator
Line 87: Line 89:
 
|}
 
|}
  
'''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 <code>TextLocale</code> system variable. If you want to change the collation order, bring up the '''Object Window '''for <code>TextLocale</code> by selecting it from the '''Definition &rarr; System Variables menu''', with nothing selected and while in edit mode.
+
=== Alphabetic ordering of text values ===
 
 
In general, the ordering of text may be different from the ordering implied by the numeric ASCII codes of the characters. For example, <code>'de' < 'dé' < 'df'</code>, whereas the ASCII code for <code>'é'</code> comes after the ASCII code for <code>'f'</code>. If you want text comparisons to use simple ASCII ordering, you can set '''TextLocale''' to be <code>ANSI</code>.
 
  
Generally, comparisons follow the ordering you would find in a dictionary. For example:
+
You can apply the comparison operators, <.  >, >=, <=, or sorting functions, [[SortIndex]](d, i), [[Sort]](d, i) and [[Rank]](), to [[Text values|text values]]. They generally use alphabetical order as in a dictionary, so:
 
:<code>'Analytica' < 'Excel' &rarr; 1 (True)</code>
 
:<code>'Analytica' < 'Excel' &rarr; 1 (True)</code>
 +
These operators and functions are normally case sensitive, so uppercase letters precede lowercase letters:
 +
:<code>'Analytica' > 'excel' &rarr; 0 (False)</code>
 +
If you want alphabetic ordering to ignore case, use [[TextUpperCase]]() or [[TextLowerCase]]() to convert all letters to the same case.
 +
:<code>TextUpperCase('Analytica') < TextUpperCase('excel') &rarr; 1 (True)</code>
 +
In functions, [[SortIndex]](d, i), [[Sort]](d, i) and [[Rank]](), you can set their optional parameter «caseInsensitive» to True if you want them to ignore case. 
  
In almost all collation orders, digits precede letters:
+
Usually, digits precede letters:
 
:<code>'9' < 'A' &rarr; 1 (True)</code>
 
:<code>'9' < 'A' &rarr; 1 (True)</code>
  
The comparison operators are case-sensitive, so that uppercase letters precede lowercase letters:
+
The collation order of characters is region-specific collation, according to your language and country. It is determined by the <code>TextLocale</code> system variable. If you want to see or change the collation order, bring up the '''Object Window '''for <code>TextLocale</code> by selecting it from the '''Definition &rarr; System Variables menu''', with nothing selected and while in edit mode.
:<code>'Analytica' > 'excel' &rarr; 0 (False)</code>
 
  
If you want to alphabetize without regard to case, use [[TextUpperCase]]() or [[TextLowerCase]]() to convert letters to the same case.
+
In general, the ordering of text may be different from the ordering implied by the numeric ASCII codes of the characters. For example, <code>'de' < 'dé' < 'df'</code>, whereas the ASCII code for <code>'é'</code> comes after the ASCII code for <code>'f'</code>. If you want text comparisons to use simple ASCII ordering, you can set '''TextLocale''' to be <code>ANSI</code>.
:<code>TextUpperCase('Analytica') < TextUpperCase('excel') &rarr; 1 (True)</code>
 
  
 
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 <code>Chr(233)</code> or <code>Chr(769)&'e'</code>, where the Unicode <code>Chr(769)</code> is the combining acute accent, which modifies the character that follows. Although these alternate representations of <code>'é'</code> 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.
 
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 <code>Chr(233)</code> or <code>Chr(769)&'e'</code>, where the Unicode <code>Chr(769)</code> is the combining acute accent, which modifies the character that follows. Although these alternate representations of <code>'é'</code> 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''': Logical operators apply to Boolean values and produce Boolean values.
+
Logical operators apply to Boolean values and produce Boolean values.
  
 
{| class="wikitable"
 
{| class="wikitable"
Line 134: Line 137:
 
|}
 
|}
  
'''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.
+
=== 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 <code>::</code> 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.
 
Prepending <code>::</code> 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.
Line 142: Line 147:
 
::<code>Irr(Payments, Time)</code>  The built-in function
 
::<code>Irr(Payments, Time)</code>  The built-in function
  
==Operator binding precedence==
+
===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:
+
Analytica uses a standard precedence hierarchy resolves potential ambiguity when evaluating operators in expressions with multiple operators.  For example,
 +
:<code>1/2*3 - 3^2 + 4</code>
 +
is interpreted as:
 +
:<code>(((1/2)*3) - (3^2)) + 4</code>
 +
 
 +
It binds arithmetic operators before comparison operators and comparison operators before logical operators, and all of these before IF...THEN...ELSE ... and other language constructs, so:
 +
<code>IF d + e < f^g OR a AND b > c THEN x ELSE y + z</code>
 +
is interpreted as:
 +
:<code>IF (((d + e) < (f^g)) OR (a AND (b > c))) THEN x ELSE (y + z)</code>
 +
 
 +
The binding precedence for operators, from most tightly bound to least tightly bound, is:
  
 
# parentheses ()
 
# parentheses ()
Line 166: Line 181:
 
# Sequence of statements separated by semicolons, sequence of elements or parameters separated by commas
 
# 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).
+
Within each level of this hierarchy, the operators bind from left to right (left associative). For example,
 
+
:<code>1/2*3</code>
'''Examples''': The following arithmetic expression:
 
:<code>1/2*3 - 3^2 + 4</code>
 
 
 
is interpreted as:
 
:<code>((1/2)*3) - (3^2) + 4</code>
 
 
 
The following logical (Boolean) expression:
 
:<code>IF d + e < f^g or a and b > c THEN x ELSE y + z</code>
 
 
 
 
is interpreted as:
 
is interpreted as:
:<code>IF (((d + e) < (f^g)) or (a and (b > c))) THEN x ELSE (y + z)</code>
+
:<code>(1/2)*3</code>
 +
not as
 +
:<code>1/(2*3)</code>
  
 
==See Also==
 
==See Also==

Revision as of 22:10, 31 January 2016

An infix operator is a symbol, such as a plus sign (1+2) or product (10*2), that operates on the values before and after it. A prefix operator, such as unary minus (-100) operates only on the value after it. Analytica includes these fairly standard opera tors, including arithmetic, comparison, and logical operators:

Arithmetic operators

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

[math]\displaystyle{ 4^.5 = 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

You can apply the comparison operators, <. >, >=, <=, or sorting functions, SortIndex(d, i), Sort(d, i) and Rank(), to text values. They generally use alphabetical order as in a dictionary, so:

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

These operators and functions are normally case sensitive, so uppercase letters precede lowercase letters:

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

If you want alphabetic ordering to ignore case, use TextUpperCase() or TextLowerCase() to convert all letters to the same case.

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

In functions, SortIndex(d, i), Sort(d, i) and Rank(), you can set their optional parameter «caseInsensitive» to True if you want them to ignore case.

Usually, digits precede letters:

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

The collation order of characters is region-specific collation, according to your language and country. It is determined by the TextLocale system variable. If you want to see or 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.

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.

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

Analytica uses a standard precedence hierarchy resolves potential ambiguity when evaluating operators in expressions with multiple operators. For example,

1/2*3 - 3^2 + 4

is interpreted as:

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

It binds arithmetic operators before comparison operators and comparison operators before logical operators, and all of these before IF...THEN...ELSE ... and other language constructs, so: 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)

The binding 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). For example,

1/2*3

is interpreted as:

(1/2)*3

not as

1/(2*3)

See Also

Comments


You are not allowed to post comments.