# Math

> Arithmetic on data values, with the same operations as signal Math. Numbers in, numbers out; lists are worked element by element.

Source: https://docs.tune.toys/nodes/data.math/

Data Math does arithmetic on data values: single numbers, or every element of a list.

## What it does

Operation chooses the arithmetic, in the same order and with the same names as signal [Math](https://docs.tune.toys/nodes/std.math/): Add, Subtract, Multiply, Divide, Modulo, Power, Minimum, Maximum, Average, Absolute difference, then the one-operand operations Negate, Absolute, Floor, Ceiling, Round, Square root and Sign.

Both inputs are read as numbers: `true` is 1, `false` is 0 and text such as `" 42 "` becomes 42. When B is not connected it is B when unconnected. If either input is a list, the operation runs on every element: a list and a number applies the number to each element, and two lists pair their elements up to the shorter length.

Results that have no number send no value (null) instead of infinity: dividing by 0, Modulo by 0, the square root of a negative number and text that does not read as a number. The inspector says which input caused it. Math runs whenever an input receives a value and sends one result.

## When to use it

Use it for arithmetic on values that do not need to run at audio or control rate: scale a setting, convert units, offset every note in a list or average two readings. For audio and control signals use signal [Math](https://docs.tune.toys/nodes/std.math/).

## Ports

<!-- ports:start -->

| Port    | Direction | Signal     | Lanes |
| ------- | --------- | ---------- | ----- |
| `a`     | Input     | Data (any) | 1     |
| `b`     | Input     | Data (any) | 1     |
| `value` | Output    | Data (any) | 1     |

<!-- ports:end -->

## Parameters

<!-- parameters:start -->

| Parameter   | Label              | Range                     | Default | Unit | Scale  | Notes                                                                                                                                                                                                                                                                                        |
| ----------- | ------------------ | ------------------------- | ------- | ---- | ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `op`        | Operation          | 0 to 16                   | 0       |      | linear | Operation applied to a and b; operations after Absolute difference use a only. Choices: 0 Add, 1 Subtract, 2 Multiply, 3 Divide, 4 Modulo, 5 Power, 6 Minimum, 7 Maximum, 8 Average, 9 Absolute difference, 10 Negate, 11 Absolute, 12 Floor, 13 Ceiling, 14 Round, 15 Square root, 16 Sign. |
| `b_default` | B when unconnected | -1000000000 to 1000000000 | 0       |      | linear | Value of b while its port is unconnected. Shown when `op` is 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 or 8 or 9.                                                                                                                                                                                 |

<!-- parameters:end -->

### Operation (`op`)

Which arithmetic to apply, a discrete choice shared with signal Math. Divide and Modulo by 0, and Square root of a negative number, send no value rather than 0.

### B when unconnected (`b_default`)

The number used for B when nothing is connected to B, from -1 000 000 000 to 1 000 000 000. Hidden for the one-operand operations.

## Tips

- Feed a JSON list from [Value](https://docs.tune.toys/nodes/data.value/) and Add 12 to transpose every note by an octave.
- Two lists of different lengths give a list as long as the shorter one.
- Chain two Math nodes to offset and then scale, as in the help patch.

## See also

[Value](https://docs.tune.toys/nodes/data.value/), [Compare](https://docs.tune.toys/nodes/data.compare/), [List](https://docs.tune.toys/nodes/data.list/), [Math (signal)](https://docs.tune.toys/nodes/std.math/).
