Skip to content

How Asterisk Works ​

This guide teaches you how to use expression evaluation in Asterisk, a key tool for evaluating arithmetic and logical expressions. It includes syntax, practical examples, important notes, and combined usage with variables.

Basic Syntax ​

$[expr1 operator expr2]

Logical Operators ​

These operators allow you to combine boolean conditions:

  • expr1 | expr2 (Logical OR)

    • Returns expr1 if it evaluates as non-empty/non-zero ("true").
    • Otherwise, evaluates and returns expr2.
  • expr1 & expr2 (Logical AND)

    • Returns expr1 if both expressions evaluate as non-empty/non-zero ("true").
    • Otherwise, returns zero ("false").
  • !expr (Unary Logical NOT)

    • Note: There must be no space between ! and expr.
    • Not available in Asterisk versions earlier than 1.1.

Using Logical Operators ​

You can combine conditions within GotoIf or similar commands:

$[ $[ "${FOO}" = "1" ] & $[ "${BAR}" = "2" ] ]

In this example, the expression evaluates whether both variables (FOO and BAR) meet their respective values. If they do, the full expression returns 1.

Comparison Operators ​

These allow value comparisons to control the call flow based on conditions:

  • expr1 = expr2
  • expr1 != expr2
  • expr1 < expr2
  • expr1 > expr2
  • expr1 <= expr2
  • expr1 >= expr2

Important Notes ​

  • If both arguments are integers, an integer comparison is performed.
  • Otherwise, a string comparison is performed using the system’s local sort order.
  • Returns 1 if true, 0 if false.

Arithmetic Operators ​

Asterisk also supports basic arithmetic with integers:

  • expr1 + expr2: Addition
  • expr1 - expr2: Subtraction
  • -expr: Negation (Not available in Asterisk < 1.1)
  • expr1 * expr2: Multiplication
  • expr1 / expr2: Integer division
  • expr1 % expr2: Modulo (remainder)

These operators only work with integer values.

Asterisk Arithmetic Functions ​

Functions for rounding and truncation are available and work with integers:

  • FLOOR(x): Rounds down to the nearest integer.
  • CEIL(x): Rounds up to the nearest integer.
  • ROUND(x): Rounds to the nearest integer. If exactly halfway, it rounds away from zero.
  • RINT(x): Rounds to the nearest integer, preferring the even number in case of a tie.
  • TRUNC(x): Removes the decimal part, rounding toward zero.

Clarifications ​

  • Dialplan functions can be called directly within $[...] without ${...}.

    • Example: $[3 + FLOOR(2.9)]
  • Variables, however, must use ${...}.

    • Example: $[${a} + 2]
  • Arguments in functions must be separated by commas , when using $[...].

Available Math Functions ​

Most functions from the C math library are available for more advanced calculations:

  • Trigonometric (input in radians):

    • COS(x)
    • SIN(x)
    • TAN(x)
  • Inverse Trigonometric (input between -1 and 1):

    • ACOS(x)
    • ASIN(x)
  • ATAN(x) (returns radians between -π/2 and π/2)

  • ATAN2(x, y) (returns between -π and π)

  • Exponents and Logarithms:

    • POW(x, y): x to the power of y (x^y)
    • EXP(x): e to the power of x
    • EXP2(x): 2 to the power of x
    • LOG(x): Natural logarithm (base e)
    • LOG2(x): Base 2 logarithm
    • LOG10(x): Base 10 logarithm
  • Others:

    • SQRT(x): Square root
    • REMAINDER(x, y): Returns the mathematical remainder (unlike %). Calculated as x - n*y where n = x/y rounded to the nearest even integer.

uContact by net2phone