# Formulas

Formulas calculate numbers. They can include standard math operators like `+`, `-`, `*`, and `/`, more advanced stuff like `pow`, `sin`, and `round`, and most importantly they can reference the value of specific Beans and other Formulas. The full list of operations can be found below.

To create a Formula simply include the following format in your note:

`\$\$(<FORMULA_NAME>)<FORMULA-BODY>`

The Formula Name portion must conform to the Name Portion rules. The Formula Body uses S-expression notation with the operator in the first position and the parameters listed after i.e. `(+ 1 1)`.

Parameters can be other S-expressions i.e. `(+ 1 (/ 100 10))` or they can be numbers.

Formulas can also reference other Formulas by name. To include one formula in another use the `\$` operator followed by the Formula name i.e. `(* -1 (\$ another-formula))`. Formulas do not allow recursion for now.

## Body Syntax

As stated above, the body portion of a Formula uses S-expressions to denote the Formula procedure. This format can be a little odd at first glance, but it allows all operations to have a similar format, which is useful – there is only one format to learn.

The format for an S-expression is as follows

`(operator parameter parameter...)`

The operator can be any of the operators listed below. The parameters are usually numbers, but in some cases require special values outlined by their corresponding operators below.

## Operators

### +

`(+ x y)`

Add two numbers together, use sum to add a list of numbers.

### -

`(- x y)`

Subtract `y` from `x`.

### *

`(* x y)`

Multiply two numbers.

### /

`(/ x y)`

Divide `x` by `y`.

## /tap Specific Operators

All of the three Bean operators function the same way except `BEAN` returns the net value of the Bean, `BEAN-` returns the outgoing value of the Bean and `BEAN+` returns the incoming value of the Bean.

The `name` parameter is required. `folder`, `tags` and `date` are optional, and, if used, must be surrounded by double quotes. If you wish to select a Bean value by date, but not Folder or Tags put a `-` in place of the unwanted parameter i.e. `(BEAN budget - - "2022")`. Any of the optional parameters can be ignored by using the `-` character.

A few notes on the Tags parameter:

• To specify a list of Tags separate the individual Tags with spaces i.e. `"very special stuff"`
• The `#` character is optional `"#very #special #stuff"` is functionally equivalent to the above.
• To include a tag whose name is itself quoted i.e. `#"Quoted Tag"` simply use a `\` character to escape the nested quotes i.e. `"#\"Quoted Tag\""`

### BEAN

`(BEAN name folder tags date)`

Returns the net value of the specified Bean `name` filtered to the `folder`, `tags` and `date` parameters.

### BEAN+

`(BEAN+ name folder tags date)`

Returns the incoming value of the specified Bean `name` filtered to the `folder`, `tags` and `date` parameters.

### BEAN-

`(BEAN- name folder tags date)`

Returns the outgoing value of the specified Bean `name` filtered to the `folder`, `tags` and `date` parameters.

### \$

`(\$ name)`

Include the computed value of a Formula with the specified `name`.

## More Math Operators

### %

`(% x y)`

`x` mod `y`

### min

`(min x [y ...])`

Takes an arbitrary number of parameters and returns the least number.

### max

`(max x [y ...])`

Takes an arbitrary number of parameters and returns the greatest number.

### sum

`(sum x [y ...])`

### round

`(round x)`

Round decimal to nearest integer value.

### floor

`(floor x)`

Returns the greatest integer less or equal than the input `x`

### ceil

`(ceil x)`

Returns the least integer greater than or equal the input `x`

### pow

`(pow base exponent)`

Computes the power of a number `base` to the `exponent`

### cos

`(cos x)`

Computes cosine of `x`

### sin

`(sin x)`

Computes sine of `x`

### tan

`(tan x)`

Computes tangent of `x`