Operators

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
(not)
 
(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) onto [[Script_variables|variables]]. Operators form the basis for building up expressions, the building blocks of AviSynth scripts.
+
<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 in the text below.
+
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 first converted to float, as in C.  
+
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.  
  
==== Available Operators per Type ====
+
==== 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:
  
{|border=1 cellspacing=2
+
:{|class="wikitable"
 +
! Operator
 +
!style="width:23em;text-align:left"| Meaning
 +
|-
 
  | ==  
 
  | ==  
 
  | is equal  
 
  | is equal  
Line 17: Line 21:
 
  |-
 
  |-
 
  | <>  
 
  | <>  
  | not equal (alternative to !=, v2.07)
+
  | not equal
 
  |}
 
  |}
  
String comparisons are not case-sensitive, so "abc" == "ABC" returns ''true''.
+
{{FuncArg|string}} comparisons are not case-sensitive, so "abc" == "ABC" returns ''true''.
  
For '''numeric''' types (int, float) you can use the following int/float-specific operators:
+
'''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)]
  
{|border=1 cellspacing=2
+
==== 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:
 
  |}
 
  |}
  
''AviSynth in former versions parsed expressions from right to left, which gave unexpected results.'' For example:
+
==== For Strings ====
  
* a = 10 - 5 - 5 resulted in 10 - (5 - 5) = 10 instead of (10 - 5) - 5 = 0 !
+
For {{FuncArg|string}} type you can use the following {{FuncArg|string}}-specific operators:
* b = 100. / 2. / 4. resulted in 100. / (2. / 4.) = 200 instead of (100. / 2.) / 4. = 12.5 !
+
  
These "bugs" have been corrected in v2.53!
+
:{|class="wikitable"
 
+
!style="width:5em"| Operator
For '''string''' type you can use the following string-specific operators:
+
!style="width:23em;text-align:left"| Meaning
 
+
|-
{|border=1 cellspacing=2
+
 
  | +  
 
  | +  
 
  | concatenate  
 
  | concatenate  
 
  |-
 
  |-
 
  | >=  
 
  | >=  
  | greater or equal than (v2.07)
+
  | greater or equal than  
 
  |-
 
  |-
 
  | <=  
 
  | <=  
  | less or equal than (v2.07)
+
  | less or equal than
 
  |-
 
  |-
 
  | <  
 
  | <  
  | less than (v2.07)
+
  | less than
 
  |-
 
  |-
 
  | >  
 
  | >  
  | greater than (v2.07)
+
  | greater than  
 
  |}
 
  |}
  
Like the equality operator, these string comparisons are case-insensitive.
+
Like the equality operator, these {{FuncArg|string}} comparisons are case-insensitive.
  
For '''clip''' type you can use the following clip-specific operators:
+
==== For Clips ====
  
{|border=1 cellspacing=2
+
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
 +
|-
 
  | +   
 
  | +   
  | the same as the function [[Splice|UnalignedSplice]]  
+
  | same as the function [[Splice|UnalignedSplice]]  
 
  |-
 
  |-
 
  | ++  
 
  | ++  
  | the same as the function [[Splice|AlignedSplice]]
+
  | same as the function [[Splice|AlignedSplice]]
 
  |}
 
  |}
  
For '''bool''' type (true/false) you can use the following bool-specific operators:
+
==== For Booleans ====
  
{|border=1 cellspacing=2
+
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:
 
  |-
 
  |-
 
  | ?:  
 
  | ?:  
  | execute code conditionally
+
  | 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 pseudo-basic:
+
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>
From version v2.07, AviSynth provides a NOP() function which can be used inside a conditional execution block in cases where "else" may not otherwise be desirable (such as a conditional [[Import]] or LoadPlugin).
+
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 higher to the top of the table have higher precedence. Operators inside the same row have the same order of precedence.
+
The [[wikipedia:Order_of_operations|precedence]] of AviSynth operators is presented at the table below. Operators in the same row have the same precedence.
  
{|border=1 cellspacing=2
+
:{|class="wikitable"
  | *
+
|style="width:5em"| '''highest:'''&nbsp;
  | /
+
  | &nbsp;*
  | %
+
  | &nbsp;/
 +
  | &nbsp;%
 
  |colspan=4|
 
  |colspan=4|
 
  |-
 
  |-
  | +  
+
  |
 +
| &nbsp;+  
 
  | ++
 
  | ++
  | -
+
  | &nbsp;-
 
  |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>
+
  |  
 +
| &nbsp;<nowiki>||</nowiki>
 
  |colspan=6|
 
  |colspan=6|
 
  |-
 
  |-
 +
| '''lowest:'''&nbsp;
 
  | ?:
 
  | ?:
 
  |colspan=6|
 
  |colspan=6|
 
  |}
 
  |}
  
The dot symbol ('''.'''), used in the "OOP notation" for a function call, is not strictly an operator, but effectively has a higher precedence than any operator symbol.
+
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 &ndash; for example,  
So for example, <code>a + b.f(args)</code> means <code>a + f(b, args)</code> and not <code>f(a+b, args)</code> (which could be written as <code>(a+b).f(args)</code>)
+
<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.

Personal tools