magus v0.4.2 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 (4f8cc295)
Earlier changes on this page (2)

Full history ↗ · Blame source ↗

Glossary

Magusfile

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

Op

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

Ward

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

Module

A magus stdlib namespace a magusfile imports for host capabilities: filesystem, exec, vcs, and more. See the module reference.

Buzz

The language magusfiles are written in (the .buzz engine). See engines.

CI

An ordinary magusfile-defined target you compose yourself with magus\needs - magus does not hardcode its stages. Magus.RunCI treats it specially only in that it strips the rw charm, it is the anchor magus affected ci keys off, and a selected scope with no project declaring it is a load error rather than a silent no-op. See targets.

Conventions

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