magus v0.4.3 is out. See what's new
¶ View generated markdown
3 min read

math

Rounding to a decimal place, clamping, and aggregation over a list of numbers.

Naming convention: import the module under its bare name (import "math"), reach members with a backslash, and call methods in camelCase: math\someMethod.

Methods

round

Round x to places decimal places, half away from zero. places defaults to 0 (the nearest whole number) and may be negative to round to tens, hundreds and so on. Rendering a coverage percentage or a duration is the usual reason; Buzz's floor and ceil cannot express it.

Signature: math\round(x, [places]) -> float64 - source

Parameter Type Optional Description
x float64
places int yes

Returns: float64

trunc

Discard x's fractional part, rounding TOWARD ZERO - so -1.7 is -1, where floor gives -2. The difference matters whenever a value can be negative and you meant "drop the decimals".

Signature: math\trunc(x) -> float64 - source

Parameter Type Optional Description
x float64

Returns: float64

clamp

Constrain x to the range lo..hi, returning lo when x is below it and hi when above. Sizing parallelism is the usual reason - clamp(cpus / 2, 1, 8) never yields zero workers. Raises when lo is greater than hi, which is a caller bug rather than an empty range.

Signature: math\clamp(x, lo, hi) -> float64 - source

Parameter Type Optional Description
x float64
lo float64
hi float64

Returns: float64

sum

Add every number in the list; an empty list sums to 0. Non-numeric items are skipped rather than counted as zero.

Signature: math\sum(nums) -> float64 - source

Parameter Type Optional Description
nums []float64

Returns: float64

mean

The arithmetic mean. Raises on an empty list rather than returning 0, because 0 is a real average and "there was nothing to average" is not - returning it would let an empty set silently pass a floor check.

Signature: math\mean(nums) -> float64 - source

Parameter Type Optional Description
nums []float64

Returns: float64

median

The middle value, averaging the two middle values for an even count. Prefer it to mean when reporting what a TYPICAL run costs: one pathological outlier moves a mean and barely moves a median. Raises on an empty list, like mean.

Signature: math\median(nums) -> float64 - source

Parameter Type Optional Description
nums []float64

Returns: float64

min

The smallest number in the list. Distinct from Buzz's own minInt/minDouble, which compare exactly two values. Raises on an empty list.

Signature: math\min(nums) -> float64 - source

Parameter Type Optional Description
nums []float64

Returns: float64

max

The largest number in the list. Distinct from Buzz's own maxInt/maxDouble, which compare exactly two values. Raises on an empty list.

Signature: math\max(nums) -> float64 - source

Parameter Type Optional Description
nums []float64

Returns: float64

generatedreference/buzz/mathmodulestdlibmagusfile
Last updated (29d8b3de)
Earlier changes on this page (4)

Full history ↗ · Blame source ↗

Glossary

Magusfile

The magusfile.buzz that declares a project's targets (as export funs) and binds its spells. Magusfile in full.

Op

A single tool-native command a target composes (long form: operation); the middle of the work hierarchy (Spell to Op to Target). Op in full.

Ward

A coded diagnostic that inspects a resolved op and nudges or blocks an anti-pattern before it runs. Ward in full.

Module

A magus stdlib namespace a magusfile imports for host capabilities: filesystem, exec, vcs, and more. Module in full.

Buzz

The language magusfiles are written in (the .buzz engine). Buzz in full.

CI

An ordinary target you compose yourself with ctx.needs; magus does not hardcode its stages. It is the anchor magus affected ci keys off, and ci strips the rw charm. CI in full.

Run

One target executing under one magus invocation, such as magus run test web or magus affected ci. A run keeps its captured output behind an output reference. Every magus run is a run whether or not any job asked for it; see Job for how the two relate. Run in full.

Conventions

This page uses none of the site's convention markers. The full set is on the conventions page.