A newer version of Max is available. Click here to access the latest version of this document.

expr

Evaluate a mathematical expression

Description

expr, written by Shahrokh Yadegari, evaluates an expression involving the "variables" $i1-$i9 (the first to ninth inlets, taken as integers), $f1-$f9 (the inlets as floats) and $s1-$s9 (the inlets as symbols), and/or constants of the same types. The operators are +, -, *, /, &, |, &&, ||, !, ~, and ^. You can call a set of predefined mathematical and statistical functions. For instance, midi-to-hertz conversion can be done as:
8.175798 * pow (1.0594633\,$i1)

Arguments

Name Type Opt Description
mathematical-expression list Obligatory. The argument is a mathematical expression, in a format resembling the C programming language. The expression is made up of numbers, arithmetic operators such as + or *, comparisons such as < or >, C functions such as min () or pow (), names of table objects, and changeable arguments ($i, $f, and $s) for ints, floats, and symbols received in the inlets.
constant int or float Numbers can be used as constants in the mathematical expression.
inlet-format $i or $f A changeable int argument is specified by $i or $f and an inlet number (example: $i2). The argument will be replaced by numbers received in the specified inlet.
table-address-info (name and index) $s The argument $s and an inlet number is replaced by the name of a table to be accessed. The argument should be immediately followed by a number in brackets specifying an address in the table. (Examples: $s2[7] or $s3[$i1].)
(other) symbol Arithmetic operators understood by expr are: +, -, *, /, %. Other operators are ~ (one's complement), ^ (bitwise exclusive or), &, &&, |, ||, and ! (not).

Many C language math functions can be understood by expr. A function must be followed immediately by parentheses containing any arguments necessary to the function. If the function requires a comma between arguments, the comma must be preceded by a backslash (\) so that Max will not be confused by it. For example: pow ($i1\,2).

C language functions understood by expr are: abs, min, max, sin, cos, tan, asin, acos, atan, atan2, sinh, cosh, tanh, int (convert to integer), float (convert to float), pow, sqrt, fact (factorial), exp (power of e to x), log10 (log), ln or log (natural log), and random. Additional functions can be added by means of external code resources placed in Max's startup folder.

Messages

bang In left inlet: Evaluates the expression using the values currently stored.
int input [int]
The number received in each inlet will be stored in place of the $i or $f argument associated with it. (Example: The number in the second inlet from the left will be stored in place of the $i2 and $f2 arguments, wherever they appear.)
  (inlet1) input [int]
Performs the same function as the int message. See above.
  (inlet2) input [int]
Performs the same function as the int message. See above.
  (inlet3) input [int]
Performs the same function as the int message. See above.
  (inlet4) input [int]
Performs the same function as the int message. See above.
  (inlet5) input [int]
Performs the same function as the int message. See above.
  (inlet6) input [int]
Performs the same function as the int message. See above.
  (inlet7) input [int]
Performs the same function as the int message. See above.
  (inlet8) input [int]
Performs the same function as the int message. See above.
  (inlet9) input [int]
Performs the same function as the int message. See above.
float input [float]
The number in each inlet will be stored in place of the $f or $i argument associated with it. The number will be truncated by a $i argument.
  (inlet1) input [float]
Performs the same function as the float message. See above.
  (inlet2) input [float]
Performs the same function as the float message. See above.
  (inlet3) input [float]
Performs the same function as the float message. See above.
  (inlet4) input [float]
Performs the same function as the float message. See above.
  (inlet5) input [float]
Performs the same function as the float message. See above.
  (inlet6) input [float]
Performs the same function as the float message. See above.
  (inlet7) input [float]
Performs the same function as the float message. See above.
  (inlet8) input [float]
Performs the same function as the float message. See above.
  (inlet9) input [float]
Performs the same function as the float message. See above.
set input [list]
In left inlet: The word set, followed by one or more numbers, treats those numbers as if each had come in a different inlet, replacing the stored value with the new value, but the expression is not evaluated and nothing is sent out the outlet. If there are fewer numbers in the message than there are inlets, the stored value in each remaining inlet stays unchanged.
sm1 table-name [list]
Performs the same function as the symbol message. See above.
sm2 table-name [list]
Performs the same function as the symbol message. See above.
sm3 table-name [list]
Performs the same function as the symbol message. See above.
sm4 table-name [list]
Performs the same function as the symbol message. See above.
sm5 table-name [list]
Performs the same function as the symbol message. See above.
sm6 table-name [list]
Performs the same function as the symbol message. See above.
sm7 table-name [list]
Performs the same function as the symbol message. See above.
sm8 table-name [list]
Performs the same function as the symbol message. See above.
sm9 table-name [list]
Performs the same function as the symbol message. See above.
symbol table-name [list]
The word symbol, followed by the name of a table, will be stored in place of the $s argument associated with that inlet, for accessing values stored in the table.
list input [list]
In left inlet: The items of the list are treated as if each had come in a different inlet, and the expression is evaluated. If the list contains fewer items than there are inlets, the most recently received value in each remaining inlet is used. Any of the above messages in the left inlet will evaluate the expression and send out the result. If a value has never been received for each changeable argument, that value is considered 0 when the expression is evaluated. The number of inlets is determined by how many changeable arguments are typed in. The maximum number of inlets is 9.

Information for box attributes common to all objects

Output

float: The output is the result of the evaluated expression.
int: The output is the result of the evaluated expression.

Examples

Combine many calculations into one object even using functions not available in other objects

See Also

Name Description
if Conditional statement in if/then/else form
vexpr Evaluate a math expression for a list of different inputs
round Round to an input value
Max Basic Tutorial 22: Designing Equations Max Basic Tutorial 22: Designing Equations