Operators
(not) |
Raffriff42 (Talk | contribs) (note comparison chaining) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | As in all programming and scripting languages, operators in AviSynth script language allow the performance of actions (operations) | + | <div style="max-width:62em" > |
+ | As in all programming and scripting languages, '''operators''' in AviSynth script language allow the performance of ''actions'' (operations) on [[Script_variables|'''variables''']]. Operators form the basis for building ''expressions'', the building blocks of AviSynth [[Grammar|'''grammar''']]. | ||
− | AviSynth operators follow loosely the same rules as C operators, regarding meaning, precedence and associativity. By loosely we mean that there are some exceptions, indicated | + | AviSynth operators follow loosely the same rules as [[wikipedia:C_(programming_language)|'''C''']] operators, regarding meaning, precedence and associativity. By 'loosely', we mean that there are some exceptions, indicated below. |
− | Note that when a binary operator is applied to an int and a float, the int is | + | Note that as in '''C''', when a [[wikipedia:Binary_operation|binary operator]] is applied to an {{FuncArg|{{FuncArg|int}}}} and a {{FuncArg|float}}, the {{FuncArg|int}} is automatically converted to {{FuncArg|float}} prior to the operation. |
− | ==== | + | ==== For All Variable Types ==== |
− | For '''all types''' of operands (clip, int, float, string, bool) you can use the following operators: | + | For '''all types''' of operands ({{FuncArg|clip}}, {{FuncArg|int}}, {{FuncArg|float}}, {{FuncArg|string}}, {{FuncArg|bool}}) you can use the following operators: |
− | {| | + | :{|class="wikitable" |
+ | ! Operator | ||
+ | !style="width:23em;text-align:left"| Meaning | ||
+ | |- | ||
| == | | == | ||
| is equal | | is equal | ||
Line 17: | Line 21: | ||
|- | |- | ||
| <> | | <> | ||
− | | not equal | + | | not equal |
|} | |} | ||
− | + | {{FuncArg|string}} comparisons are not case-sensitive, so "abc" == "ABC" returns ''true''. | |
− | + | '''Note''' comparisons may be ''chained'': | |
+ | :<code>(3 <= x <= 5)</code> can be used where normally something like | ||
+ | :<code>(3 <= x && x <= 5)</code> would be required.<sup>[http://forum.doom9.org/showthread.php?p=1783132#post1783132 (doom9)] | ||
− | {| | + | ==== For Numbers ==== |
+ | |||
+ | For '''numeric''' types ({{FuncArg|int}}, {{FuncArg|float}}) you can use the following numeric-specific operators: | ||
+ | |||
+ | :{|class="wikitable" | ||
+ | !style="width:5em"| Operator | ||
+ | !style="width:23em;text-align:left"| Meaning | ||
+ | |- | ||
| + | | + | ||
| add | | add | ||
Line 53: | Line 66: | ||
|} | |} | ||
− | + | ==== For Strings ==== | |
− | + | For {{FuncArg|string}} type you can use the following {{FuncArg|string}}-specific operators: | |
− | + | ||
− | + | :{|class="wikitable" | |
− | + | !style="width:5em"| Operator | |
− | + | !style="width:23em;text-align:left"| Meaning | |
− | + | |- | |
− | + | ||
| + | | + | ||
| concatenate | | concatenate | ||
|- | |- | ||
| >= | | >= | ||
− | | greater or equal than | + | | greater or equal than |
|- | |- | ||
| <= | | <= | ||
− | | less or equal than | + | | less or equal than |
|- | |- | ||
| < | | < | ||
− | | less than | + | | less than |
|- | |- | ||
| > | | > | ||
− | | greater than | + | | greater than |
|} | |} | ||
− | Like the equality operator, these string comparisons are case-insensitive. | + | Like the equality operator, these {{FuncArg|string}} comparisons are case-insensitive. |
− | For | + | ==== For Clips ==== |
− | {| | + | For {{FuncArg|clip}} type you can use the following {{FuncArg|clip}}-specific operators: |
+ | |||
+ | :{|class="wikitable" | ||
+ | !style="width:5em"| Operator | ||
+ | !style="width:23em;text-align:left"| Meaning | ||
+ | |- | ||
| + | | + | ||
− | | | + | | same as the function [[Splice|UnalignedSplice]] |
|- | |- | ||
| ++ | | ++ | ||
− | | | + | | same as the function [[Splice|AlignedSplice]] |
|} | |} | ||
− | For | + | ==== For Booleans ==== |
− | {| | + | For {{FuncArg|bool}} type (true/false) you can use the following {{FuncArg|bool}}-specific operators: |
+ | |||
+ | :{|class="wikitable" | ||
+ | !style="width:5em"| Operator | ||
+ | !style="width:23em;text-align:left"| Meaning | ||
+ | |- | ||
| <nowiki>||</nowiki> | | <nowiki>||</nowiki> | ||
| or | | or | ||
Line 101: | Line 122: | ||
|- | |- | ||
| ?: | | ?: | ||
− | | | + | | ternary (conditional execution) operator |
|- | |- | ||
| ! | | ! | ||
Line 107: | Line 128: | ||
|} | |} | ||
− | The conditional execution operator is used as in the following example: | + | The [[wikipedia:Ternary_operation|'''ternary''']] (conditional execution) operator is used as in the following example: |
− | + | <div {{BoxWidthIndent|29|2}} > | |
b = (a==true) ? 1 : 2 | b = (a==true) ? 1 : 2 | ||
− | + | </div> | |
− | This means in | + | This means, in [[wikipedia:Pseudocode|'''pseudocode''']]: |
− | + | <div {{BoxWidthIndent|29|2}} > | |
− | if (a=true) then b=1 else b=2 | + | '''''if''''' (a==true) '''''then''''' b=1 '''''else''''' b=2 |
− | + | </div> | |
− | + | AviSynth provides a [[Internal_functions#NOP|NOP]] function for cases where the ''else'' clause is not needed (such as a conditional [[Import]] or [[Plugins|LoadPlugin]]). | |
==== Operator Precedence ==== | ==== Operator Precedence ==== | ||
− | The precedence of AviSynth operators is presented at the table below. Operators | + | The [[wikipedia:Order_of_operations|precedence]] of AviSynth operators is presented at the table below. Operators in the same row have the same precedence. |
− | {| | + | :{|class="wikitable" |
− | | * | + | |style="width:5em"| '''highest:''' |
− | | / | + | | * |
− | | % | + | | / |
+ | | % | ||
|colspan=4| | |colspan=4| | ||
|- | |- | ||
− | | + | + | | |
+ | | + | ||
| ++ | | ++ | ||
− | | - | + | | - |
|colspan=4| | |colspan=4| | ||
|- | |- | ||
− | | < | + | | |
− | | > | + | |style="width:1.5em"| < |
− | | <= | + | |style="width:1.5em"| > |
− | | >= | + | |style="width:1.5em"| <= |
− | | != | + | |style="width:1.5em"| >= |
− | | <> | + | |style="width:1.5em"| != |
− | | == | + | |style="width:1.5em"| <> |
+ | |style="width:1.5em"| == | ||
|- | |- | ||
+ | | | ||
| && | | && | ||
|colspan=6| | |colspan=6| | ||
|- | |- | ||
− | | <nowiki>||</nowiki> | + | | |
+ | | <nowiki>||</nowiki> | ||
|colspan=6| | |colspan=6| | ||
|- | |- | ||
+ | | '''lowest:''' | ||
| ?: | | ?: | ||
|colspan=6| | |colspan=6| | ||
|} | |} | ||
− | The dot | + | The dot ['''.'''] symbol ([[Grammar|"OOP notation"]] for a function call, where <code>a'''.'''function(b)</code> is equivalent to <code>function(a, b)</code>) has a higher precedence than any operator – for example, |
− | + | <div {{BoxWidthIndent|20|2}} > | |
+ | a*b.function(c) | ||
+ | </div> | ||
+ | is equivalent to | ||
+ | <div {{BoxWidthIndent|20|2}} > | ||
+ | a*function(b, c) | ||
+ | </div> | ||
+ | but since '''parentheses''' override normal precedence, | ||
+ | <div {{BoxWidthIndent|20|2}} > | ||
+ | (a*b).function(c) | ||
+ | </div> | ||
+ | is equivalent to | ||
+ | <div {{BoxWidthIndent|20|2}} > | ||
+ | function(a*b, c) | ||
+ | </div> | ||
+ | </div> | ||
+ | |||
+ | |||
---- | ---- | ||
Back to [[AviSynth Syntax]]. | Back to [[AviSynth Syntax]]. |
Latest revision as of 16:27, 16 November 2017
As in all programming and scripting languages, operators in AviSynth script language allow the performance of actions (operations) on variables. Operators form the basis for building expressions, the building blocks of AviSynth grammar.
AviSynth operators follow loosely the same rules as C operators, regarding meaning, precedence and associativity. By 'loosely', we mean that there are some exceptions, indicated below.
Note that as in C, when a binary operator is applied to an int and a float, the int is automatically converted to float prior to the operation.
Contents |
[edit] For All Variable Types
For all types of operands (clip, int, float, string, bool) you can use the following operators:
Operator Meaning == is equal != not equal <> not equal
string comparisons are not case-sensitive, so "abc" == "ABC" returns true.
Note comparisons may be chained:
(3 <= x <= 5)
can be used where normally something like(3 <= x && x <= 5)
would be required.(doom9)
[edit] For Numbers
For numeric types (int, float) you can use the following numeric-specific operators:
Operator Meaning + add - subtract * multiply / divide % mod >= greater or equal than <= less or equal than < less than > greater than
[edit] For Strings
For string type you can use the following string-specific operators:
Operator Meaning + concatenate >= greater or equal than <= less or equal than < less than > greater than
Like the equality operator, these string comparisons are case-insensitive.
[edit] For Clips
For clip type you can use the following clip-specific operators:
Operator Meaning + same as the function UnalignedSplice ++ same as the function AlignedSplice
[edit] For Booleans
For bool type (true/false) you can use the following bool-specific operators:
Operator Meaning || or && and ?: ternary (conditional execution) operator ! not
The ternary (conditional execution) operator is used as in the following example:
b = (a==true) ? 1 : 2
This means, in pseudocode:
if (a==true) then b=1 else b=2
AviSynth provides a NOP function for cases where the else clause is not needed (such as a conditional Import or LoadPlugin).
[edit] Operator Precedence
The precedence of AviSynth operators is presented at the table below. Operators in the same row have the same precedence.
highest: * / % + ++ - < > <= >= != <> == && || lowest: ?:
The dot [.] symbol ("OOP notation" for a function call, where a.function(b)
is equivalent to function(a, b)
) has a higher precedence than any operator – for example,
a*b.function(c)
is equivalent to
a*function(b, c)
but since parentheses override normal precedence,
(a*b).function(c)
is equivalent to
function(a*b, c)
</div>
Back to AviSynth Syntax.