# Formulas

## PV formulas syntax

PV formulas are read only expressions that allow to combine readings from multiple pvs. The syntax is a tradeoff between convenience and what can possibly be achieved giving that datasources themselves (ca, sim, ...) may have pvs with a syntax that clashes with the formula syntax.

## Disambiguating pvs and expression

The syntax tries to allow unquoted pvs as much as possible so that a user is not forced to change format between pvs and formulas in most cases (see the reference for details). Here are a list of examples and how they are recognized.

    mypv123    : the pv called "mypv123"
    mypv+123   : the pv called "mypv" added to 123
    'mypv+123' : a pv called "mypv2+123"
    "mypv+123" : the string "mypv+123"

The parser will always give precedence to an expression than to pvs. If a pv looks like an expression, it should be escaped by single quotes.

    loc://log(1) : the pv from loc datasource called "log(1)"
    log(1)       : the function log applied to the number 1
    log          : the pv called log

An identifier followed by parenthesis will be interpreted as a function, and if not followed by parenthesis is interpreted as unquoted pv. If the pv includes the datasource, then the following parenthesis are considered part of the pv name.

## Reference

A formula can be one of the following:

Syntax | Description
--- | ---
`Numeric constant` `42` `3.14` `1e5` | A number can either be an integer, or a floating point in either decimal or exponential notation.
`"String constant"` | String constants are opened and closed by double quotes. TODO escape characteres.
`'an+escaped/pv'` `simpleUnescapedPv` `loc://complexPvWithSource("Hello!")` | A string surrounded by single quotes represents a pv. If the pvname does not clash with another expression, you can omit the quotes (i.e. it must be made only of the following characters `0-9a-zA-Z.$_:{}` and must contain one letter). If the syntax contain the source, these restriction are somewhat lifted: you can have a pair of parenthesis and a comma separated list of parameters. This combination should allow to keep most reasonably named pvs without single quotes, while allowing all the rest in single quotes.
`Algebraic operators` `expr + expr: addition` `expr - expr: subtraction` `- expr: negation` `expr * expr: multiplication` `expr / expr: division` `expr % expr: modulo` `expr ^ expr: power` `expr ** expr: power` | Standard algebraic operator between expressions are supported.
`Numeric functions` `abs(expr): absolute value` `acos(expr): arc cosine` `asin(expr): arc sine` `atan(expr): arc tangent` `cbrt(expr): cubic root` `ceil(expr): round up` `cos(expr): cosine` `cosh(expr): hyperbolic cosine` `exp(expr): exponential` `floor(expr): round down` `log(expr): natural logarithm` `log10(expr): base 10 logarithm` `signum(expr): sign function` `sin(expr): sine` `sinh(expr): hyperbolic sine` `sqrt(expr): square root` `tan(expr): tangent` `tanh(expr): hyperbolic tangent` | Some common numeric functions are also supported. For implementation details refer to the corresponding java.lang.Math method.