KScript docs
EN

KScript.Math

Roots, powers, trigonometry, logarithms, random numbers and statistics.

Import it at the top of a script:

<import package="KScript.Math" />
import KScript.Math

Commands

Commands are called with @, in any expression: @name(arguments).

#@abs(number) → number

The number without its sign.
Example
<import package="KScript.Math" />
<echo>{ @abs(-4.5) }</echo>
import KScript.Math
echo @abs(-4.5)

#@acos(number) → number

The angle in radians whose cosine is the number.
Example
<import package="KScript.Math" />
<echo>{ @degrees(@acos(0)) }</echo>
import KScript.Math
echo @degrees(@acos(0))

#@asin(number) → number

The angle in radians whose sine is the number.
Example
<import package="KScript.Math" />
<echo>{ @degrees(@asin(1)) }</echo>
import KScript.Math
echo @degrees(@asin(1))

#@atan(number) → number

The angle in radians whose tangent is the number.
Example
<import package="KScript.Math" />
<echo>{ @degrees(@atan(1)) }</echo>
import KScript.Math
echo @degrees(@atan(1))

#@atan2(y, x) → number

The angle in radians of the point (x, y).

Arguments can be named: @atan2(…, x: …).

Example
<import package="KScript.Math" />
<echo>{ @degrees(@atan2(1, 1)) }</echo>
import KScript.Math
echo @degrees(@atan2(1, 1))

#@average(values...) → number

The mean of the numbers (or of one list).
Example
<import package="KScript.Math" />
<echo>{ @average(2, 4, 9) }</echo>
import KScript.Math
echo @average(2, 4, 9)

#@cbrt(number) → number

The cube root.
Example
<import package="KScript.Math" />
<echo>{ @cbrt(27) }</echo>
import KScript.Math
echo @cbrt(27)

#@ceil(number) → number

Rounded up.
Example
<import package="KScript.Math" />
<echo>{ @ceil(7.1) }</echo>
import KScript.Math
echo @ceil(7.1)

#@clamp(number, min, max) → number

The number kept between min and max.

Arguments can be named: @clamp(…, min: …, max: …).

Example
<import package="KScript.Math" />
<echo>{ @clamp(120, min: 0, max: 100) }</echo>
import KScript.Math
echo @clamp(120, min: 0, max: 100)

#@convert_length(value, from, to) → number

A length in other units: @convert_length(12, in, cm) is 30.48. Units: mm, cm, m, km, in, ft, yd, mi, pt and px (at 96 per inch).

Arguments can be named: @convert_length(…, from: …, to: …).

Example
<import package="KScript.Math" />
<echo>{ @convert_length(12, 'in', 'cm') }</echo>
import KScript.Math
echo @convert_length(12, 'in', 'cm')

#@cos(number) → number

The cosine of an angle in radians.
Example
<import package="KScript.Math" />
<echo>{ @cos(0) }</echo>
import KScript.Math
echo @cos(0)

#@degrees(number) → number

Radians to degrees.
Example
<import package="KScript.Math" />
<echo>{ @degrees($pi) }</echo>
import KScript.Math
echo @degrees($pi)

#@exp(number) → number

e to the power of the number.
Example
<import package="KScript.Math" />
<echo>{ @exp(1)->round(digits: 5) }</echo>
import KScript.Math
echo @exp(1)->round(digits: 5)

#@factorial(n) → number

n! for a whole number from 0 to 5000, exact.
Example
<import package="KScript.Math" />
<echo>{ @factorial(5) }</echo>
import KScript.Math
echo @factorial(5)

#@floor(number) → number

Rounded down.
Example
<import package="KScript.Math" />
<echo>{ @floor(7.9) }</echo>
import KScript.Math
echo @floor(7.9)

#@gcd(a, b) → number

The greatest common divisor of two whole numbers, exact at any size.

Arguments can be named: @gcd(…, b: …).

Example
<import package="KScript.Math" />
<echo>{ @gcd(12, 18) }</echo>
import KScript.Math
echo @gcd(12, 18)

#@hypot(x, y) → number

The length of the hypotenuse: sqrt(x² + y²).

Arguments can be named: @hypot(…, y: …).

Example
<import package="KScript.Math" />
<echo>{ @hypot(3, 4) }</echo>
import KScript.Math
echo @hypot(3, 4)

#@lcm(a, b) → number

The least common multiple of two whole numbers, exact at any size: @lcm(4, 6) is 12.

Arguments can be named: @lcm(…, b: …).

Example
<import package="KScript.Math" />
<echo>{ @lcm(4, 6) }</echo>
import KScript.Math
echo @lcm(4, 6)

#@log10(number) → number

The base 10 logarithm.
Example
<import package="KScript.Math" />
<echo>{ @log10(1000) }</echo>
import KScript.Math
echo @log10(1000)

#@log2(number) → number

The base 2 logarithm.
Example
<import package="KScript.Math" />
<echo>{ @log2(1024) }</echo>
import KScript.Math
echo @log2(1024)

#@logarithm(number, base?) → number

The logarithm: natural (base e) unless a base is given.

Arguments can be named: @logarithm(…, base: …).

Example
<import package="KScript.Math" />
<echo>{ @logarithm(8, base: 2) }</echo>
import KScript.Math
echo @logarithm(8, base: 2)

#@median(values...) → number

The middle number (or the mean of the two middle numbers).
Example
<import package="KScript.Math" />
<echo>{ @median(3, 1, 4, 1, 5) }</echo>
import KScript.Math
echo @median(3, 1, 4, 1, 5)

#@mode(values...) → number

The number that occurs most often. When several tie, the smallest of them.
Example
<import package="KScript.Math" />
<echo>{ @mode(3, 1, 4, 1, 5) }</echo>
import KScript.Math
echo @mode(3, 1, 4, 1, 5)

#@percentile(values: list, p: number) → number

The value below which p percent of the numbers fall, from 0 to 100, between neighbours when it falls between two: @percentile($times, 95).
ParameterTakes
values requiredlist
p requirednumber

Arguments can be named: @percentile(…, p: …).

Example
<import package="KScript.Math" />
<echo>{ @percentile([12, 15, 20, 22, 30, 41, 95], 90) }</echo>
import KScript.Math
echo @percentile([12, 15, 20, 22, 30, 41, 95], 90)

#@pick(list) → any

A random item from a list.
Example
<import package="KScript.Math" />
<echo>{ @pick(['rock', 'paper', 'scissors']) }</echo>
import KScript.Math
echo @pick(['rock', 'paper', 'scissors'])

#@radians(number) → number

Degrees to radians.
Example
<import package="KScript.Math" />
<echo>{ @radians(180)->round(digits: 4) }</echo>
import KScript.Math
echo @radians(180)->round(digits: 4)

#@random(low?, high?) → number

A random number: from 0 up to 1 with no arguments, or a whole number from low to high (both included).

Arguments can be named: @random(…, high: …).

Example
<import package="KScript.Math" />
<let name="roll" value="@random(1, 6)" />
import KScript.Math
$roll = @random(1, 6)

#@random_bytes(count: number, format?: 'hex'|base64url) → string

Generates 1 to 65536 bytes with the operating system's cryptographic random generator. Returns lowercase hex by default, or unpadded base64url.
ParameterTakes
count requirednumber
format optionalone of these words: 'hex'|base64url

Arguments can be named: @random_bytes(…, format: …).

Example
<import package="KScript.Math" />
<echo>{ @random_bytes(16, format: hex)->count() }</echo>
import KScript.Math
echo @random_bytes(16, format: hex)->count()

#@random_seed(seed)

Makes @random(), @pick() and @shuffle() repeat the same sequence, for tests.
Example
<import package="KScript.Math" />
<do>@random_seed(42)</do>
<echo>{ @random(low: 1, high: 100) } (the same every run, with seed 42)</echo>
import KScript.Math
@random_seed(42)
echo "{ @random(low: 1, high: 100) } (the same every run, with seed 42)"

#@round(number, digits?) → number

Rounded to a number of decimal places (0 by default). Halves round away from zero.

Arguments can be named: @round(…, digits: …).

Example
<import package="KScript.Math" />
<echo>{ @round(2.675, digits: 2) }</echo>
import KScript.Math
echo @round(2.675, digits: 2)

#@shuffle(list) → list

A copy of a list in random order.
Example
<import package="KScript.Math" />
<echo>{ @shuffle([1, 2, 3, 4, 5]) }</echo>
import KScript.Math
echo @shuffle([1, 2, 3, 4, 5])

#@sign(number) → number

-1, 0 or 1.
Example
<import package="KScript.Math" />
<echo>{ [@sign(-3), @sign(0), @sign(8)] }</echo>
import KScript.Math
echo [@sign(-3), @sign(0), @sign(8)]

#@sin(number) → number

The sine of an angle in radians.
Example
<import package="KScript.Math" />
<echo>{ @sin(@radians(30))->round(digits: 3) }</echo>
import KScript.Math
echo @sin(@radians(30))->round(digits: 3)

#@sqrt(number) → number

The square root.
Example
<import package="KScript.Math" />
<echo>{ @sqrt(2) }</echo>
import KScript.Math
echo @sqrt(2)

#@stdev(values...) → number

The population standard deviation.
Example
<import package="KScript.Math" />
<echo>{ @stdev(2, 4, 4, 4, 5, 5, 7, 9)->round(digits: 3) }</echo>
import KScript.Math
echo @stdev(2, 4, 4, 4, 5, 5, 7, 9)->round(digits: 3)

#@sum(values...) → number

The total of the numbers (or of one list).
Example
<import package="KScript.Math" />
<echo>{ @sum(1, 2, 3, 4) }</echo>
import KScript.Math
echo @sum(1, 2, 3, 4)

#@tan(number) → number

The tangent of an angle in radians.
Example
<import package="KScript.Math" />
<echo>{ @tan(@radians(45))->round(digits: 3) }</echo>
import KScript.Math
echo @tan(@radians(45))->round(digits: 3)

#@trunc(number) → number

With the fraction removed.
Example
<import package="KScript.Math" />
<echo>{ @trunc(-7.9) }</echo>
import KScript.Math
echo @trunc(-7.9)

#@variance(values...) → number

The population variance: the mean of the squared distances from the mean.
Example
<import package="KScript.Math" />
<echo>{ @variance(2, 4, 4, 4, 5, 5, 7, 9) }</echo>
import KScript.Math
echo @variance(2, 4, 4, 4, 5, 5, 7, 9)

Methods

On number values: ->sqrt()

On list values: ->median(), ->stdev()

Variables

VariableTypeDescription
$enumbere, about 2.71828.
$pinumberπ, about 3.14159.
$taunumberτ (2π), about 6.28318.