Difference between revisions of "Operators"
Line 11: | Line 11: | ||
!Examples | !Examples | ||
|- | |- | ||
− | |x+y | + | |x + y |
|plus | |plus | ||
− | |3+ | + | |3 + 2 → 5 |
|- | |- | ||
− | |x-y | + | |x - y |
|binary minus | |binary minus | ||
− | |3- | + | |3 - 2 → 1 |
|- | |- | ||
|<nowiki>-x</nowiki> | |<nowiki>-x</nowiki> | ||
|unary minus | |unary minus | ||
− | |<nowiki>- | + | |<nowiki>-2 → -2</nowiki> |
|- | |- | ||
|x*y | |x*y | ||
|product | |product | ||
− | |3* | + | |3*2 → 6 |
|- | |- | ||
|x/3 or x÷y | |x/3 or x÷y | ||
|division | |division | ||
− | |<math>3/2 (=\frac{3}{2}) | + | |<math>3/2 (= \frac{3}{2}) → 1.5</math> |
|- | |- | ||
|x^y | |x^y | ||
|to the power of | |to the power of | ||
− | |3^2=3<sup>2</sup> | + | |3^2 = 3<sup>2</sup> → 9 |
− | 4^.5 =<math>4^{\frac{1}{2}} | + | 4^.5 = <math>4^{\frac{1}{2}} → 2</math> |
|} | |} | ||
Line 42: | Line 42: | ||
!Meaning | !Meaning | ||
!Examples | !Examples | ||
− | !→ (1=true, 0=false) | + | !→ (1 = true, 0 = false) |
|- | |- | ||
|< | |< | ||
|less than | |less than | ||
− | |<code> | + | |<code> 2 < 2 </code> |
− | <code> 'A'<'B' </code> | + | <code> 'A' < 'B' </code> |
|<code>→ 0</code> | |<code>→ 0</code> | ||
<code>→ 1</code> | <code>→ 1</code> | ||
Line 53: | Line 53: | ||
|<= | |<= | ||
|less than or equal to | |less than or equal to | ||
− | |<code> | + | |<code> 2 <= 2 </code> |
− | <code>'ab'<='ab' </code> | + | <code>'ab' <= 'ab' </code> |
|<code>→ 1</code> | |<code>→ 1</code> | ||
<code>→ 1</code> | <code>→ 1</code> | ||
Line 60: | Line 60: | ||
|= | |= | ||
|equal to | |equal to | ||
− | |<code> 100=101 </code> | + | |<code> 100 = 101 </code> |
− | <code> 'AB'='ab' </code> | + | <code> 'AB' = 'ab' </code> |
|<code>→ 0</code> | |<code>→ 0</code> | ||
<code>→ 0</code> | <code>→ 0</code> | ||
Line 68: | Line 68: | ||
|greater than or equal to | |greater than or equal to | ||
|<code> 100 >= 1 </code> | |<code> 100 >= 1 </code> | ||
− | <code>'ab'>='cd' </code> | + | <code>'ab'> = 'cd' </code> |
|<code>→ 1</code> | |<code>→ 1</code> | ||
<code>→ 0</code> | <code>→ 0</code> | ||
Line 75: | Line 75: | ||
|greater than | |greater than | ||
|<code> 1 > 2 </code> | |<code> 1 > 2 </code> | ||
− | <code> 'A'>'a' </code> | + | <code> 'A' > 'a' </code> |
|<code>→ 0</code> | |<code>→ 0</code> | ||
<code>→ 1</code> | <code>→ 1</code> | ||
Line 82: | Line 82: | ||
|not equal to | |not equal to | ||
|<code>1 <> 2 </code> | |<code>1 <> 2 </code> | ||
− | <code>'A'<>'B' </code> | + | <code>'A' <> 'B' </code> |
|<code>→ 1</code> | |<code>→ 1</code> | ||
<code>→ 1</code> | <code>→ 1</code> | ||
Line 92: | Line 92: | ||
Generally, comparisons follow the ordering you would find in a dictionary. For example: | Generally, comparisons follow the ordering you would find in a dictionary. For example: | ||
− | :<code>'Analytica' < 'Excel' →1 (True)</code> | + | :<code>'Analytica' < 'Excel' → 1 (True)</code> |
In almost all collation orders, digits precede letters: | In almost all collation orders, digits precede letters: | ||
− | :<code>'9' < 'A' →1 (True)</code> | + | :<code>'9' < 'A' → 1 (True)</code> |
The comparison operators are case-sensitive, so that uppercase letters precede lowercase letters: | The comparison operators are case-sensitive, so that uppercase letters precede lowercase letters: | ||
Line 101: | Line 101: | ||
If you want to alphabetize without regard to case, use <code>TextUppercase</code> or <code>TextLowerCase</code> to convert letters to the same case. | If you want to alphabetize without regard to case, use <code>TextUppercase</code> or <code>TextLowerCase</code> to convert letters to the same case. | ||
− | :<code>TextUpperCase('Analytica') < TextUpperCase('excel') →1 (True)</code> | + | :<code>TextUpperCase('Analytica') < TextUpperCase('excel') → 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. | + | '''[[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. | '''Logical operators''': Logical operators apply to Boolean values and produce Boolean values. | ||
Line 118: | Line 118: | ||
|true if both ''<code>b1</code>'' and ''<code>b2</code>'' are true, | |true if both ''<code>b1</code>'' and ''<code>b2</code>'' are true, | ||
otherwise false | otherwise false | ||
− | |<code>5>0 AND 5>10</code> | + | |<code>5 > 0 AND 5 > 10</code> |
|<code>→</code> <code>False</code> | |<code>→</code> <code>False</code> | ||
|- | |- | ||
Line 124: | Line 124: | ||
|true if ''<code>b1</code>'' or ''<code>b2</code>'' or both are true, | |true if ''<code>b1</code>'' or ''<code>b2</code>'' or both are true, | ||
otherwise false | otherwise false | ||
− | |<code>5>0 OR 5>10</code> | + | |<code>5 > 0 OR 5 > 10</code> |
|<code>→</code> <code>True</code> | |<code>→</code> <code>True</code> | ||
|- | |- | ||
Line 130: | Line 130: | ||
|true if ''<code>b</code>'' is false, | |true if ''<code>b</code>'' is false, | ||
otherwise false | otherwise false | ||
− | |<code>NOT (5>0)</code> | + | |<code>NOT (5 > 0)</code> |
|<code>→</code> <code>False</code> | |<code>→</code> <code>False</code> | ||
|} | |} | ||
Line 169: | Line 169: | ||
'''Examples''': The following arithmetic expression: | '''Examples''': The following arithmetic expression: | ||
− | :<code>1 / 2 * 3 - 3 ^ 2 + 4</code> | + | :<code>1/2*3 - 3^2 + 4</code> |
is interpreted as: | is interpreted as: | ||
− | :<code>((1 / 2) * 3) - (3 ^ 2) + 4</code> | + | :<code>((1/2)*3) - (3^2) + 4</code> |
The following logical (Boolean) expression: | The following logical (Boolean) expression: | ||
− | :<code>IF d + e < f ^ g or a and b > c THEN x ELSE y + z</code> | + | :<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>IF (((d + e) < (f^g)) or (a and (b > c))) THEN x ELSE (y + z)</code> |
==See Also== | ==See Also== | ||
<footer>Text values / {{PAGENAME}} / IF a THEN b ELSE c</footer> | <footer>Text values / {{PAGENAME}} / IF a THEN b ELSE c</footer> |
Revision as of 23:30, 6 December 2015
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
|
→ 0
|
<= | less than or equal to | 2 <= 2
|
→ 1
|
= | equal to | 100 = 101
|
→ 0
|
>= | greater than or equal to | 100 >= 1
|
→ 1
|
> | greater than | 1 > 2
|
→ 0
|
<> | not equal to | 1 <> 2
|
→ 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’sIrr
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:
- parentheses ()
- function calls
- Not
- @I, \A, \[I]A, #R.
- A.I
- A[I=x]
- Attrib of Obj
- ^
- - (unary, negative)
- *, /
- +, - (binary, minus)
- m..n
- <, >, <=, >=, =, <>
- And
- Or
- & (text concatenation)
- :=
- If … Then … Else, Ifonly … Then … Else, Ifall … Then … Else
- 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)
Enable comment auto-refresher