# Math

Plain arithmetic helpers, rounding and logic.

Each value is the expression evaluated on its own; a random call draws from a stream seeded 0.

## Typical uses

Use these functions for ordinary arithmetic in any expression: a rounded quantity, a bounded value, a conditional, a safe division. `IF(condition, a, b)` is the way to branch inside an expression. `ZIDZ` and `XIDZ` avoid a division by zero. `MODULO` is the remainder with the sign of the first argument.

## Behaviour and rules

The Python-style functions `abs`, `ceil`, `exp`, `float`, `floor`, `int`, `log`, `log10`, `max`, `min`, `round` and `sqrt` are lower case. Their upper-case spellings do not exist, except `SQRT`. `IF` is upper case and takes exactly three arguments, so write `IF(x > 0, 1, 2)`. The Python form `a if c else b` also works.

`ZIDZ(a, b)` returns 0 when `b` is 0, and `XIDZ(a, b, x)` returns `x` instead. With two arguments, `XIDZ` behaves like `ZIDZ`. `MODULO(-10, 3)` is -1, not 2.

## abs

`abs(x)`: 1 argument.

plain Python.

Example: `abs(-3.5)` gives 3.5.

Used in the bundled models Sugarscape with regrowth (grid, patches).

## ceil

`ceil(x)`: 1 argument.

math.ceil.

Example: `ceil(2.1)` gives 3.

## exp

`exp(x)`: 1 argument.

math.exp.

Example: `exp(1)` gives 2.718.

Used in the bundled models SIR epidemic (agents).

## float

`float(x)`: 1 argument.

plain Python.

Example: `float(3)` gives 3.

## floor

`floor(x)`: 1 argument.

math.floor.

Example: `floor(2.9)` gives 2.

Used in the bundled models Wolf-Sheep predation with grass (grid), Order-driven warehouse (library assets).

## IF

`IF(c, a, b)`: 3 arguments.

a when c else b; only the branch taken is evaluated.

Example: `IF(3 > 2, 10, 20)` gives 10.

Used in the bundled models Boids flock (continuous, heading), Nagel-Schreckenberg traffic (ring), Schelling segregation (grid), Sugarscape with regrowth (grid, patches) and 3 more.

## int

`int(x)`: 1 argument.

plain Python.

Example: `int(7.8)` gives 7.

Used in the bundled models Container terminal: electrification, Container terminal: quay to yard.

## log

`log(x[, y])`: 1 to 2 arguments.

1-2 args, math.log.

Example: `log(100, 10)` gives 2.

## log10

`log10(x)`: 1 argument.

math.log10.

Example: `log10(1000)` gives 3.

## max

`max(x, ...)`: 1 or more arguments.

variadic, plain Python.

Example: `max(3, 8, 5)` gives 8.

Used in the bundled models Sugarscape with regrowth (grid, patches), Berth: shore power, Biogas plant (library assets), Depot: PV, EV charging and heat pump and 9 more.

## min

`min(x, ...)`: 1 or more arguments.

variadic, plain Python.

Example: `min(3, 8, 5)` gives 3.

Used in the bundled models Nagel-Schreckenberg traffic (ring), Berth: shore power, Biogas digester, holder and CHP, Biogas plant (library assets) and 8 more.

## MODULO

`MODULO(a, b)`: 2 arguments.

the remainder of a / b with the sign of a.

Example: `MODULO(-7, 3)` gives -1.

## round

`round(x[, y])`: 1 to 2 arguments.

1-2 args, plain Python.

Example: `round(3.14159, 2)` gives 3.14.

Used in the bundled models Order-driven warehouse (library assets), Emergency department: bedside scan (film template), Two-echelon supply chain (library assets).

## sqrt

`sqrt(x)`: 1 argument.

math.sqrt.

Example: `sqrt(16)` gives 4.

## SQRT

`SQRT(x)`: 1 argument.

math.sqrt (the upper-case twin of sqrt).

Example: `SQRT(16)` gives 4.

Used in the bundled models Boids flock (continuous, heading).

## XIDZ

`XIDZ(a, b[, x])`: 2 to 3 arguments.

a / b, or x (default 0) when b is 0.

Example: `XIDZ(6, 0, 99)` gives 99.

## ZIDZ

`ZIDZ(a, b)`: 2 arguments.

a / b, or 0 when b is 0.

Example: `ZIDZ(6, 0)` gives 0.

Used in the bundled models Emergency department: bedside scan (film template).

## See also

- [All function groups and the grammar](index.md), [Distributions](../distributions/index.md)
- Guides that use them: [Agent-based modelling](../../agent-based-modelling.md), [From an event log to a model in five minutes](../../event-log-tutorial.md), [System dynamics](../../system-dynamics.md), [Coming from SimPy](../../coming-from/simpy.md), [Layout, paths and vehicles](../../modelling-guide/layout-paths-vehicles.md), [How do I combine event flow, system dynamics and agents?](../../goals/hybrid-model.md) and 7 more
