# Math

> Arithmetic on any signal, lane by lane: add, multiply, divide, power and more on audio or control operands. The output runs at the rate of its fastest input.

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

Math does arithmetic on signals, lane by lane. Feed it audio and it works on every sample; feed it control lanes and it works once per block.

## What it does

Operation chooses how lane n of A and lane n of B become lane n of the output. The two-operand operations are Add, Subtract, Multiply, Divide, Modulo, Power, Minimum, Maximum, Average and Absolute difference; the one-operand operations (Negate, Absolute, Floor, Ceiling, Round, Square root, Sign) use A and ignore B.

The inputs follow their cables. If A or B carries audio, the output is audio; if both carry control, or nothing at all, the output is control. A control operand against an audio operand is read lane by lane, so a one-lane control B applies to every audio channel and a two-lane control B applies one value per channel. When B is not connected, every lane of B is B when unconnected. When A is not connected it is 0.

Every operation is defined for every input: dividing by 0 gives 0, the square root and power of a negative number keep its sign, and any result that would not be a finite number becomes 0. Math never outputs NaN or infinity.

## When to use it

Use it to scale a signal (Multiply), to offset a control (Add), to ring-modulate two oscillators (Multiply on two audio inputs), to keep a level inside a limit (Minimum, Maximum) or to quantise a control (Round, Floor). For a plain level control with a public knob, [Gain](https://docs.tune.toys/nodes/std.gain/) is simpler. To turn a signal into a gate, use [Compare](https://docs.tune.toys/nodes/std.compare/).

## Ports

<!-- ports:start -->

| Port  | Direction | Signal                               | Lanes                   |
| ----- | --------- | ------------------------------------ | ----------------------- |
| `a`   | Input     | Audio or control (follows the cable) | Inferred from the cable |
| `b`   | Input     | Audio or control (follows the cable) | Inferred from the cable |
| `out` | Output    | Audio or control (follows the cable) | Inferred from the cable |

<!-- ports:end -->

## Parameters

<!-- parameters:start -->

| Parameter   | Label              | Range   | Default | Unit | Scale  | Notes                                                                                                                                                                                                                                                                                                                                       |
| ----------- | ------------------ | ------- | ------- | ---- | ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `op`        | Operation          | 0 to 16 | 0       |      | linear | The lane-wise operation. Add through Absolute difference combine A and B; Negate through Sign transform A alone and ignore B. 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 | -8 to 8 | 0       |      | linear | Value used for every lane of B when nothing is connected to it. Multiply with 0.5 halves a signal. 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. Divide and Modulo give 0 when B is 0; Modulo takes the sign of B; Power and Square root keep the sign of A; Round rounds halves away from zero. Changing it takes effect at the next block.

### B when unconnected (`b_default`)

The value used for every lane of B when nothing is connected to B, from -8 to 8. Changes are smoothed over 5 ms so moving it does not click. It is hidden for the one-operand operations, which ignore B.

## Tips

- To halve audio, set Operation to Multiply and B when unconnected to 0.5. Every channel is halved.
- For per-channel gain from a control, connect a control bundle with one lane per channel to B and Multiply: lane 1 sets the first channel, lane 2 the second.
- Multiply two audio oscillators for ring modulation: you hear the sum and difference of their frequencies.
- Add B when unconnected to an LFO to move its centre before it reaches a parameter socket.
- Math does not clamp. Follow a boost with a [Limiter](https://docs.tune.toys/nodes/std.limiter/), or shape a control with [Scale](https://docs.tune.toys/nodes/ctl.scale/).

## See also

[Gain](https://docs.tune.toys/nodes/std.gain/), [Compare](https://docs.tune.toys/nodes/std.compare/), [Scale](https://docs.tune.toys/nodes/ctl.scale/), [Control lanes](https://docs.tune.toys/concepts/control-lanes/), [Multichannel audio](https://docs.tune.toys/concepts/multichannel/).
