> ## Documentation Index
> Fetch the complete documentation index at: https://docs.truemath.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Arithmetic and powers

> Core arithmetic, percent change, modulo, powers and roots, and logarithmic and exponential functions.

These cover core arithmetic, percent change, modulo, powers, roots, logarithms, and exponential functions. Every operation works on a number or a [table](https://docs.truemath.ai/math/types/tables.md), returning the same shape it was given. For operator precedence and the comparison and logical operators, see [Operators and precedence](https://docs.truemath.ai/math/functions/operators-and-precedence.md).

## Core arithmetic

```
value1 + value2     # add
value1 - value2     # subtract
value1 * value2     # multiply
value1 / value2     # divide
```

Each operation accepts two numbers, a number and a table, or two tables — producing a new table when a table is involved. Two tables must be the **same length**, or an `Invalid dimensions` error is returned. Dividing by `0` returns a `Division by 0` error; `1 / value` returns the reciprocal of `value`.

```
3 + 4                   => 7
[3; 4; 5] + 3           => [6; 7; 8]
[6; 9; 12] + [3; 4; 5]  => [9; 13; 17]

3 - 4                   => -1
[6; 9; 12] - [3; 4; 5]  => [3; 5; 7]

3 * 4                   => 12
[3; 4; 5] * 3           => [9; 12; 15]

3 / 4                   => 0.75
[6; 9; 12] / [3; 4; 5]  => [2; 2.25; 2.4]
```

Each of these operations accepts [unit numbers](https://docs.truemath.ai/math/types/unit-numbers.md) as well as plain ones. Compatible units convert before adding, and multiplication and division combine dimensions:

```
1m + 10cm       => 1.1 m
5m * 3          => 15 m
10m / 2s        => 5 m/s
3m * 4m         => 12 m^2
```

## Percent change \[ch]

```
ch(value; change)
ch(value; change; periods)
```

Returns `value` adjusted by a percentage `change` (entered as a decimal): a positive `change` adds, a negative `change` subtracts. This is the financial calculator equivalent of `value + change%` or `value - change%`, which a spreadsheet would write as `value ± (value * change%)`. With `periods`, it compounds the change over that many periods (compound annual growth rate). `value` can be a number or a table.

```
ch(100; 0.25)        => 125
ch(100; -0.25)       => 75
ch(100; 0.25; 3)     => 195.3125
ch(100USD; 0.25)     => 125 USD
```

## Modulo \[mod]

```
mod(value1; value2)
```

The remainder of `value1` divided by `value2`. A `value2` of `0` returns a `Division by 0` error. Either argument can be a number or a table; if both are tables they must be the same length, or `Invalid dimensions` is returned. If `value1` or `value2` is too large to calculate then returns 'Parameter out of range' error. The remainder takes the sign of `value1`.

```
mod(3; 2)              => 1
mod(2; 3)              => 2
mod(3; 3)              => 0
mod(-8; 5)             => -3
mod([3;4;5]; 2)        => [1; 0; 1]
mod([3;7;9]; [2;4;5])  => [1; 3; 4]
```

**With units.** `mod` works on [unit numbers](https://docs.truemath.ai/math/types/unit-numbers.md) as readily as on plain ones. Both arguments must be the same kind of value: two plain numbers, or two units of the same dimension. Compatible units convert before the division, and the remainder is reported in `value1`'s unit. Anything else returns `input.incompatible_units`, including a bare number paired with a unit value.

```
mod(7m; 3m)       => 1 m
mod(90min; 1hr)   => 30 min
```

That makes a time-of-day calculation direct. A [date](https://docs.truemath.ai/math/types/dates.md) is a count of seconds, so the remainder of a date divided by one day is the time elapsed since midnight, with no stripping units and putting them back:

```
mod(appointment; 1 d)     # => the time of day, as a duration from midnight
mod(appointment d; 1 d)   # => the same time of day, as a fraction of a day
```

The second line follows from that rule. [Cast](https://docs.truemath.ai/math/units.md#casting-to-a-specific-unit) the date to days before dividing and the remainder arrives in days, so noon reads as `0.5 d` rather than `43200 s`. That is the fraction-of-a-day figure a spreadsheet keeps time in.

Because the sign follows `value1`, a date before 1970 is a negative value and gives a negative remainder.

## Powers and roots

### Power \[^]

```
value ^ x
```

Raises `value` to the `x` power. `value` can either be a number or table where the same is returned. Several forms are common:

* `value ^ 2`, `value ^ 3` — squared, cubed.
* `value ^ -1` — the reciprocal of `value` (see also division).
* `value ^ (1/2)` — the square root, the same as `sqrt(value)`.
* `value ^ (1/x)` — the `x`th root, the same as `root(value; x)`.

```
4^2          => 16
-2^2         => -4
9^(1/2)      => 3
4^-1         => 0.25
[3; 4; 5]^2  => [9; 16; 25]
```

> [!WARNING]
>
> **With a unit, the exponent binds to the unit, not to the value.** `m^2` is the unit *square metres*, so `2m^2` is two square metres — not `(2m)` squared. Parenthesize the value to raise it:
>
> ```
> 2m^2       => 2 m^2
> (2m)^2     => 4 m^2
> ```

### Square root \[sqrt]

```
sqrt(value)
```

The square root of `value`. `value` can either be a number or table where the same is returned.

A root reduces the dimension along with the value: the square root of an area is a length.

```
sqrt(16)            => 4
sqrt([9; 16; 25])   => [3; 4; 5]
sqrt(16m^2)         => 4 m
```

### Root \[root]

```
root(value; x)
```

The `x`th root of `value`. `root(value; 2)` is the same as `sqrt(value)`. `value` can either be a number or table where the same is returned.

```
root(16; 2)          => 4
root([9; 16; 25]; 2) => [3; 4; 5]
root(27m^3; 3)       => 3 m
```

## Logarithms and exponentials

These take a **dimensionless** value. An argument carrying a unit returns `input.incompatible_type` — convert it to a bare number first with [`unitvalue`](https://docs.truemath.ai/math/functions/units#unit-value-unitvalue).

### Natural logarithm \[ln]

```
ln(value)
```

The natural logarithm (base *e*) of `value`. `value` can either be a number or table where the same is returned.

```
ln(10)            => 2.3026
ln([2; 3; 4])     => [0.6931; 1.099; 1.3863]
```

### Exponential \[exp]

```
exp(value)
```

The constant *e* (2.718281828459045) raised to `value`. `value` can either be a number or table where the same is returned. There is no `e` constant; write `exp(1)` when you need *e* itself.

```
exp(10)              => 22026.4658
exp([0.5; 0.75; 2])  => [1.6487; 2.117; 7.3891]
```

### Logarithm \[log]

```
log(value)
log(value; base)
```

The logarithm of `value` in base 10, or in `base`. `value` can either be a number or table where the same is returned. `base` must be a whole number ≥ 2; real numbers are truncated, and values outside the range return a `Parameter out of range` error.

```
log(15)              => 1.1761
log(15; 3)           => 2.4650
log([10; 12; 14]; 4) => [1.661; 1.7925; 1.9037]
```

### Antilogarithm \[alog]

```
alog(value)
```

The inverse (base 10) logarithm of `value`. `value` can either be a number or table where the same is returned.

```
alog(1.25)             => 17.7828
alog([0.8; 1.2; 1.4])  => [6.3096; 15.8489; 25.1189]
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.