Internal functions/Numeric functions

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
(typo)
Line 277: Line 277:
 
* {{ScriptFunction|BitRRotate|v2.60|BitRRotateL(int, int)}}
 
* {{ScriptFunction|BitRRotate|v2.60|BitRRotateL(int, int)}}
 
* {{ScriptFunction|BitRor|v2.60|BitRor(int, int)}}
 
* {{ScriptFunction|BitRor|v2.60|BitRor(int, int)}}
: Rotates the bits of an integer to the right by the number of bits specified in the second operand. For each rotation specified, the low order bit that exits from the right of the operand returns at the left to become the new high order bit
+
: Rotates the bits of an integer to the right by the number of bits specified in the second operand. For each rotation specified, the low order bit that exits from the right of the operand returns at the left to become the new high order bit.
 
: Examples:
 
: Examples:
 
  Rotates the bits of the number 13 one bit to the right:
 
  Rotates the bits of the number 13 one bit to the right:

Revision as of 14:46, 20 August 2013

Numeric functions

They provide common mathematical operations on numeric variables.

  • Max   |   v2.58   |   Max(float, float [, ...])
Returns the maximum value of a set of numbers.
If all the values are of type Int, the result is an Int. If any of the values are of type Float, the result is a Float.
This may cause an unexpected result when an Int value greater than 16777216 is mixed with Float values.
Examples:
Max (1, 2) = 2
Max (5, 3.0, 2) = 5.0
  • Min   |   v2.58   |   Min(float, float [, ...])
Returns the minimum value of a set of numbers.
Examples:
Min (1, 2) = 1
Min (5, 3.0, 2) = 2.0
  • MulDiv   |   v2.56   |   MulDiv(int, int, int)
Multiplies two ints (m, n) and divides the product by a third (d) in a single operation, with 64 bit intermediate result. The actual equation used is (m * n + d / 2) / d .
Examples:
MulDiv (1, 1, 2) = 1
MulDiv (2, 3, 2) = 3
  • Floor   |     |   Floor(float)
Converts from single-precision, floating-point value to int (round down on any fractional amount).
Examples:
Floor(1.2) = 1
Floor(1.6) = 1
Floor(-1.2) = -2
Floor(-1.6) = -2
  • Ceil   |     |   Ceil(float)
Converts from single-precision, floating-point value to int (round up on any fractional amount).
Examples:
Ceil(1.2) = 2
Ceil(1.6) = 2
Ceil(-1.2) = -1
Ceil(-1.6) = -1
  • Round   |     |   Round(float)
Converts from single-precision, floating-point value to int (round off to nearest integer).
Examples:
Round(1.2) = 1
Round(1.6) = 2
Round(-1.2) = -1
Round(-1.6) = -2
  • Int   |   v2.07   |   Int(float)
Converts from single-precision, floating-point value to int (round towards zero).
Examples:
Int(1.2) = 1
Int(1.6) = 1
Int(-1.2) = -1
Int(-1.6) = -1
  • Float   |   v2.07   |   Float(int)
Converts int to single-precision, floating-point value. Integer values that require more than 24-bits to be represented will have their lower 8-bits truncated yielding unexpected values.
Examples:
Float(4) = 4.0
Float(4) / 3 = 1.333 (while 4 / 3 = 1 , due to integer division)
  • Sin   |   v2   |   Sin(float)
Returns the sine of the argument (assumes it is radians).
Examples:
Sin(Pi()/4) = 0.707
Sin(Pi()/2) = 1.0
  • Cos   |   v2   |   Cos(float)
Returns the cosine of the argument (assumes it is radians).
Examples:
Cos(Pi()/4) = 0.707
Cos(Pi()/2) = 0.0
  • Tan   |   v2.60   |   Tan(float)
Returns the tangent of the argument (assumes it is radians).
Examples:
Tan(Pi()/4) = 1.0
Tan(Pi()/2) = not defined
32 bit ieee floats do not have sufficient resolution to exactly represent
pi/2 so AviSynth returns a large positive number for the value slightly less
than pi/2 and a large negative value for the next possible value which is
slightly greater than pi/2.
  • Asin   |   v2.60   |   Asin(float)
Returns the inverse of the sine of the argument (output is radians).
Examples:
Asin(0.707) = 0.7852471634 (~ Pi/4)
Asin(1.0) = 1.570796327 (~ Pi/2)
  • Acos   |   v2.60   |   Acos(float)
Returns the inverse of the cosine of the argument (output is in radians).
Examples:
Acos(0.707) = 0.7852471634 (~ Pi/4)
Acos(0.0) = 1.570796327 (~ Pi/2)
  • Atan   |   v2.60   |   Atan(float)
Returns the inverse of the tangent of the argument (output is in radians).
Examples:
Atan(0.707) = 0.6154085176
Atan(1.0) = 0.7853981634 (~ Pi/4)
  • Atan2   |   v2.60   |   Atan2(float, float)
Returns the angle between the positive x-axis of a plane and the point given by the coordinates (x, y) on it (output is in radians). See wikipedia for more information.
y is the first argument and x is the second argument.
Examples:
Atan2(1.0, 0) = 1.570796327 (~ Pi/2)
Atan2(1.0, 1.0) = 0.7852471634 (~ Pi/4)
Atan2(−1.0, −1.0) = -2.356194490 (~ −3Pi/4)
  • Sinh   |   v2.60   |   Sinh(float)
Returns the hyperbolic sine of the argument. See wikipedia for more information.
Examples:
Sinh(2.0) = 3.626860408
  • Cosh   |   v2.60   |   Cosh(float)
Returns the hyperbolic cosine of the argument.
Examples:
Cosh(2.0) = 3.762195691
  • Tanh   |   v2.60   |   Tanh(float)
Returns the hyperbolic tangent of the argument.
Examples:
Tanh(2.0) = 0.9640275801
  • Fmod   |   v2.60   |   Fmod(float, float)
Returns the modulo of the argument. Output is float.
Examples:
Fmod(3.5, 0.5) = 0 (since 3.5 - 7*0.5 = 0)
Fmod(3.5, 1.0) = 0.5 (since 3.5 - 3*1.0 = 0.5)
  • Pi   |   v2   |   Pi()
Returns the value of the "pi" constant (the ratio of a circle's circumference to its diameter).
Examples:
d = Pi()    # d == 3.141592653
  • Exp   |   v2   |   Exp(float)
Returns the natural (base-e) exponent of the argument.
Examples:
Exp(1) = 2.7182818
Exp(0) = 1.0
  • Log   |   v2   |   Log(float)
Returns the natural (base-e) logarithm of the argument.
Examples:
Log(1) = 0.0
Log(10) = 2.30259
Log(Exp(1)) = 1.0
  • Log10   |   v2.60   |   Log10(float)
Returns the common logarithm of the argument.
Examples:
Log10(1.0) = 0
Log10(10.0) = 1.0
Log10(2.0) = 0.3010299957
  • Pow   |   v2   |   Pow(float base, float power)
Returns "base" raised to the power indicated by the second argument.
Examples:
Pow(2, 3) = 8
Pow(3, 2) = 9
Pow(3.45, 1.75) = 8.7334
  • Sqrt   |   v2   |   Sqrt(float)
Returns the square root of the argument.
Examples:
Sqrt(1) = 1.0
Sqrt(2) = 1.4142
  • Abs   |   v2.07   |   Abs(float or int)
Returns the absolute value of its argument (returns float for float, integer for integer).
Examples:
Abs(-3.8) = 3.8
Abs(-4) = 4
  • Sign   |   v2.07   |   Sign(float)
Returns the sign of the value passed as argument (1, 0 or -1).
Examples:
Sign(-3.5) = -1
Sign(3.5) = 1
Sign(0) = 0
  • Frac   |   v2.07   |   Frac(float)
Returns the fractional portion of the value provided.
Examples:
Frac(3.7) = 0.7
Frac(-1.8) = -0.8
  • Rand   |   v2.07   |   Rand([int max] [, bool scale] [, bool seed])
Returns a random integer value. All parameters are optional.
  • max sets the maximum value+1 (default 32768) and can be set negative for negative results. It operates either in scaled or modulus mode (default scale=true only if abs(max) > 32768, false otherwise).
  • Scaled mode (scale=true) scales the internal random number generator value to the maximum value, while modulus mode (scale=false) uses the remainder from an integer divide of the random generator value by the maximum. I found modulus mode is best for smaller maximums.
  • Using seed=true seeds the random number generator with the current time. seed defaults to false and probably isn't necessary, although it's there just in case.
Typically, this function would be used with the Select function for random clips.
Examples:
Select(Rand(5), clip1, clip2, clip3, clip4, clip5)
  • Spline   |   v2.51   |   Spline(float X, x1, y1, x2, y2, .... [, bool cubic])
Interpolates the Y value at point X using the control points x1/y1, ... There have to be at least 2 x/y-pairs. The interpolation can be cubic (the result is a spline) or linear (the result is a polygon). Default is cubic.
Examples:
Spline(5, 0, 0, 10, 10, 20, 0, false) = 5
Spline(5, 0, 0, 10, 10, 20, 0, true) = 7
  • ContinuedNumerator   |   v2.60   |   ContinuedNumerator(float, int limit)
  • ContinuedNumerator   |   v2.60   |   ContinuedNumerator(int, int, int limit)
  • ContinuedDenominator   |   v2.60   |   ContinuedDenominator(float, int limit)
  • ContinuedDenominator   |   v2.60   |   ContinuedDenominator(int, int, int limit)
The rational pair (ContinuedNumerator,ContinuedDenominator) returned has the smallest possible denominator such that the absolute error is less than 1/limit. More information can be found on wikipedia.
If limit is not specified in the Float case the rational pair returned is to the limit of the single precision floating point value. Thus (float)((double)Num/(double)Den) == V.
In the Int case if limit is not specified then the normalized original values will be returned, i.e. reduced by the GCD.
Examples:
ContinuedNumerator(PI(), limit=5000]) = 355
ContinuedDenominator(PI(), limit=5000) = 113

ContinuedNumerator(PI(), limit=50]) = 22
ContinuedDenominator(PI(), limit=50) = 7

ContinuedNumerator(355, 113, limit=50]) = 22
ContinuedDenominator(355, 113, limit=50) = 7 
  • BitAnd   |   v2.60   |   BitAnd(int, int)
The functions: BitAnd, BitNot, BitOr and BitXor, etc, are bitwise operators. This means that their arguments (being integers) are converted to binary numbers, the operation is performed on their bits, and the resulting binary number is converted back again.
BitAnd returns the bitwise AND (sets bit to 1 if both bits are 1 and sets bit to 0 otherwise).
Examples:
BitAnd(5, 6) = 4 # since 5 = 101, 6 = 110, and 101&110 = 100
  • BitNot   |   v2.60   |   BitNot(int)
Returns the bit-inversion (sets bit to 1 if bit is 0 and vice-versa).
Examples:
BitNOT(5) = -6 # since 5 = 101, and ~101 = 1111 1111 1111 1111 1111 1111 1111 1010 = -6
Note: 1111 1111 1111 1111 1111 1111 1111 1010 = (2^32-1)-2^0-2^2 = 2^32-(1+2^0+2^2) =(signed) -(1+2^0+2^2) = -6
  • BitOr   |   v2.60   |   BitOr(int, int)
Returns the bitwise inclusive OR (sets bit to 1 if one of the bits (or both) is 1 and sets bit to 0 otherwise).
Examples:
BitOr(5, 6) = 7 # since 5 = 101, 6 = 110, and 101|110 = 111
BitOr(4, 2) = 6 # since 4 = 100, 2 = 010, and 100|010 = 110
  • BitXor   |   v2.60   |   BitXor(int, int)
Returns the bitwise exclusive OR (sets bit to 1 if exactly one of the bits is 1 and sets bit to 0 otherwise).
Examples:
BitXor(5, 6) = 3 # since 5 = 101, 6 = 110, and 101^110 = 011
BitXor(4, 2) = 6 # since 4 = 100, 2 = 010, and 100^010 = 110
  • BitLShift   |   v2.60   |   BitLShift(int, int)
  • BitShl   |   v2.60   |   BitShl(int, int)
  • BitSal   |   v2.60   |   BitSal(int, int)
Shift the bits of a number to the left.
Examples:
Shifts the bits of the number 5 two bits to the left:
BitLShift(5, 2) = 20 (since 101 << 2 = 10100)
  • BitRShiftL   |   v2.60   |   BitRShiftL(int, int)
  • BitRShiftU   |   v2.60   |   BitRShiftU(int, int)
  • BitShr   |   v2.60   |   BitShr(int, int)
Shift the bits of an unsigned integer to the right. (Logical, zero fill, Right Shift)
Examples:
Shifts the bits of the number -42 one bit to the right, treating it as unsigned:
BitRShiftL(-42, 1) = 2147483627 (since 1111 1111 1111 1111 1111 1111 1101 0110 >> 1 = 0111 1111 1111 1111 1111 1111 1110 1011)
Note: -42 = -(1+2^0+2^3+2^5) = (unsigned) (2^32-1)-(2^0+2^3+2^5) = 1111 1111 1111 1111 1111 1111 1101 0110
  • BitRShiftA   |   v2.60   |   BitRShiftA(int, int)
  • BitRShiftS   |   v2.60   |   BitRShiftS(int, int)
  • BitSar   |   v2.60   |   BitSar(int, int)
Shift the bits of an integer to the right. (Arithmetic, Sign bit fill, Right Shift)
Examples:
Shifts the bits of the number -42 one bit to the right, treating it as signed:
BitRShiftA(-42, 1) = -21 (since 1111 1111 1111 1111 1111 1111 1101 0110 >> 1 = 1111 1111 1111 1111 1111 1111 1110 1011)
  • BitLRotate   |   v2.60   |   BitLRotate(int, int)
  • BitRol   |   v2.60   |   BitRol(int, int)
Rotates the bits of an integer to the left by the number of bits specified in the second operand. For each rotation specified, the high order bit that exits from the left of the operand returns at the right to become the new low order bit.
Examples:
Rotates the bits of the number -2147483642 one bit to the left:
BitLRotate(-2147483642, 1) = 13 (since 10000000000000000000000000000110 ROL 1 = 00000000000000000000000000001101)
  • BitRRotate   |   v2.60   |   BitRRotateL(int, int)
  • BitRor   |   v2.60   |   BitRor(int, int)
Rotates the bits of an integer to the right by the number of bits specified in the second operand. For each rotation specified, the low order bit that exits from the right of the operand returns at the left to become the new high order bit.
Examples:
Rotates the bits of the number 13 one bit to the right:
BitRRotate(13, 1) = -2147483642 (since 00000000000000000000000000001101 ROR 1 = 10000000000000000000000000000110)
  • BitTest   |   v2.60   |   BitTest(int, int)
Tests a single bit (that is, it returns true if the bit is one, else it returns false). The second operand denotes the location of the bit which is specified as an offset from the low order end of the operand (starting at zero).
Examples:
Tests the bits of the number 5
BitTest(5, 0) = True (since the first (most right) bit of 5 = 101 is 1)
BitTest(5, 1) = False (since the second bit of 5 = 101 is 0)
BitTest(5, 2) = True (since the third bit of 5 = 101 is 1)
  • BitSet   |   v2.60   |   BitSet(int, int)
Sets a single bit to one (so it sets a bit). The second operand denotes the location of the bit which is specified as an offset from the low order end of the operand (starting at zero).
Examples:
Sets the bits of the number 5
BitSet(5, 0) = 5 (first bit is already one)
BitSet(5, 1) = 7 (second bit is set to 1)
BitSet(5, 2) = 5 (bit is already one)
BitSet(5, 3) = 13 (fourth bit is set to one)
  • BitClear   |   v2.60   |   BitClear(int, int)
Sets a single bit to zero (so it clears a bit). The second operand denotes the location of the bit which is specified as an offset from the low order end of the operand (starting at zero).
Examples:
Clears the bits of the number 5
BitClear(5, 0) = 4 (first bit is set to zero)
BitClear(5, 1) = 5 (second bit is already zero)
BitClear(5, 2) = 1 (third bit is set to zero)
BitClear(5, 3) = 5 (fourth bit is already zero)
  • BitChange   |   v2.60   |   BitChange(int, int)
Sets a single bit to its complement (so 1 becomes 0 and vice versa). The second operand denotes the location of the bit which is specified as an offset from the low order end of the operand (starting at zero).
Examples:
Changes the bits of the number 5
BitChange(5, 0) = 4 (first bit is set to zero)
BitChange(5, 1) = 7 (second bit is set to one)
BitChange(5, 2) = 1 (third bit is set to zero)
BitChange(5, 3) = 13 (fourth bit is set to one)

Back to Internal functions.

Personal tools