# Mathematical expressions

Since INScore version 1.20, mathematical expressions have been introduced as messages arguments. These expressions allows to compute values at parsing time.

## Operators

Basic mathematical expressions are supported. They are listed below.

Note that *matharg* could be a *mathexpr* as well (see section mathargs).

**1**: addition.**2**: substraction.**3**: negation.**4**: multiplication.**5**: division.**6**: modulo.**7**: parenthesis, to be used for evaluation order.**8**: gives the maximum value.**9**: gives the minimum value.**10**: conditional form: returns the first matharg if mathbool is true, otherwise returns the second one.

Example Some simple mathematical expressions used as message parameters:

```
/ITL/scene/myObject x 0.5 * 0.2;
/ITL/scene/myObject y ($var ? 1 : -1);
```

Boolean operations are the following:

**1**: evaluate the argument as a boolean value.**2**: evaluate the argument as a boolean value and negates the result.**3**: check if the arguments are equal.**4**: check if the first argument is greater than the second one.**5**: check if the first argument is greater or equel to the second one.**6**: check if the first argument is less than the second one.**7**: check if the first argument is less or equal to the second one.

Example Compare two variables:

```
/ITL/scene/myObject x ($var1 > $var2 ? 1 : -1);
```

## Arguments

Arguments of mathematical operations are the following:

**1**: any message parameter.**2**: a variable value.**3**: a variable that is post incremented or post decremented.**4**: a variable that is pre incremented or pre decremented.**5**: a message based variable.**6**: a mathematical expression.

## Polymorphism

Since INScore's parameters are polymorphic, the semantic of the operations are to be defined notably when applied to non numeric arguments. Actually, a basic OSC message parameter type is between int32, float32 and string. However, due to the extension of the scripting language, parameters could also be arrays of any type, including mixed types (e.g. resulting from variable declarations).

### Numeric values

For numeric arguments, automatic type conversion is applied with a precedence of float32 i.e. when one of the argument's type is float32, the result is also float32 (see Table table:mathopnum).

arg1 | arg2 | output |
---|---|---|

int32 | int32 | int32 |

float32 | int32 | float32 |

int32 | float32 | float32 |

float32 | float32 | float32 |

### Strings

For string parameters, operations that have an obvious semantic (like + applied to two strings) are defined (see Table table:mathopstr) , the others are undefined and generate an error (see Table table:mathnoopstr).

operation | evaluates to | comment |
---|---|---|

string + string | string | concatenate the two strings |

string + num | string + \mathstring{num} | num is converted to string |

num + string | \mathstring{num} + string | " |

@max(string string) | string | select the largest string |

@min(string string) | string | select the smallest string |

operation | comment |
---|---|

string \op\ string | where \op\ is in [- * / |

string \op\ num | " |

num \op\ string | " |

-string | |

prefixed or postfixed string |

Boolean operations are supported on string only when both arguments are strings (see Table table:mathboolstr). Other types combination generate an error.

boolean operation | evaluated as |
---|---|

string == string | regular strings comparison |

string > string | alphabetical strings comparison |

string >= string | " |

string < string | " |

string <= string | " |

### Arrays

For arrays, the operation is distributed inside the array elements: or

When both parameters are arrays, the operation is distributed from one array elements to the other array elements when the arrays have the same size and it generates an error when the sizes differ:

Boolean operations on arrays are evaluated as the logical of it's element's boolean values and generate an error when the arrays sizes differ.