Mathematical operations and numeric utilities.

Functions

abs core

fn (x: Int | Dec): Int | Dec

Absolute value of x. The smallest Int has no Int absolute value: abs of it is Err("Integer overflow").

Example

abs(-5)     // 5
abs(3)      // 3
abs(-2.5)   // 2.5

add core

fn (): Int
fn (x: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec, z: Int | Dec, ...rest: Int | Dec): Int | Dec

Add the numbers, left to right. add() is 0 and add(x) is x, so add(...xs) sums a Vec of any length. An Int sum that doesn't fit in an Int is Err("Integer overflow").

Example

add(2, 3)        // 5
add(1.5, 2.5)    // 4.0
add(-1, 1)       // 0
add(1, 2, 3, 4)  // 10
add(...[1, 2])   // 3
add()            // 0

ceil core

fn (x: Int | Dec): Int

Round x up to the nearest integer. A Dec too large for an Int gives Err("Integer overflow").

Example

ceil(2.3)   // 3
ceil(2.0)   // 2
ceil(-2.3)  // -2

div core

fn (x: Int | Dec, y: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec, z: Int | Dec, ...rest: Int | Dec): Int | Dec

Divide x by each later number, left to right. Dividing by zero is Err("Division by zero").

Example

div(10, 2)     // 5
div(7, 2)      // 3.5
div(1, 4)      // 0.25
div(100, 2, 5) // 10

floor core

fn (x: Int | Dec): Int

Round x down to the nearest integer. A Dec too large for an Int gives Err("Integer overflow").

Example

floor(2.7)   // 2
floor(2.0)   // 2
floor(-2.3)  // -3

is-neg core

fn (x: Int | Dec): Bool

Return true if x is less than zero.

Example

is-neg(-5)   // true
is-neg(0)    // false
is-neg(3)    // false

is-pos core

fn (x: Int | Dec): Bool

Return true if x is greater than zero.

Example

is-pos(5)    // true
is-pos(0)    // false
is-pos(-3)   // false

is-zero core

fn (x: Int | Dec): Bool

Return true if x is zero.

Example

is-zero(0)    // true
is-zero(1)    // false
is-zero(0.0)  // true

max core

fn (x: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec, z: Int | Dec, ...rest: Int | Dec): Int | Dec

Return the largest of the numbers, so max(...xs) needs no starting value. An empty Vec has no largest number, so max(...[]) is an error.

Example

max(3, 7)       // 7
max(-1, -5)     // -1
max(1.5, 1.4)   // 1.5
max(3, 9, 2)    // 9
max(...[-3, -9]) // -3

min core

fn (x: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec, z: Int | Dec, ...rest: Int | Dec): Int | Dec

Return the smallest of the numbers, so min(...xs) needs no starting value. An empty Vec has no smallest number, so min(...[]) is an error.

Example

min(3, 7)       // 3
min(-1, -5)     // -5
min(1.5, 1.4)   // 1.4
min(3, 9, 2)    // 2
min(...[-3, -9]) // -9

mod core

fn (x: Int | Dec, y: Int | Dec): Int | Dec

Remainder of x divided by y, with the sign of x (the remainder of truncated division, as % in C, Rust and JavaScript), for Ints and Decs alike. The result is an Int when both are Ints, else a Dec. A zero y is Err("Modulo by zero").

Example

mod(10, 3)     // 1
mod(7, 2)      // 1
mod(8, 4)      // 0
mod(2.5, 1)    // 0.5
mod(-7, 2)     // -1
mod(-7.0, 2)   // -1.0
mod(7, -2)     // 1

mul core

fn (): Int
fn (x: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec, z: Int | Dec, ...rest: Int | Dec): Int | Dec

Multiply the numbers, left to right. mul() is 1 and mul(x) is x, so mul(...xs) takes the product of a Vec of any length. An Int product that doesn't fit in an Int is Err("Integer overflow").

Example

mul(3, 4)       // 12
mul(2.5, 4)     // 10.0
mul(-2, 3)      // -6
mul(2, 3, 4)    // 24
mul(...[2, 5])  // 10
mul()           // 1

pow core

fn (base: Int | Dec, exp: Int | Dec): Int | Dec

Raise base to the power exp. An Int power that doesn't fit in an Int is Err("Integer overflow"). A power of 0.5 is the square root. A negative base has no fractional power, so that is an Err too.

Example

pow(2, 3)      // 8
pow(10, 2)     // 100
pow(2, 0)      // 1
pow(1.5, 2)    // 2.25
pow(9, 0.5)    // 3
pow(-8, 0.5)   // Err("pow of a negative number requires a whole exponent")

rand core

fn (): Dec
fn (n: Int | Dec): Dec

Random decimal between 0 and 1, or 0..n when n provided.

Example

rand()     // 0.7234... (random decimal 0-1)
rand(10)   // 7.234...  (random decimal 0-10)
rand(100)  // 72.34...  (random decimal 0-100)

rand-int core

fn (): Int
fn (n: Int): Int

Random integer from 0 to the largest Int, or from 0 to n - 1 when n is given. n must be positive.

Example

rand-int()    // Random integer
rand-int(10)  // Random integer 0-9
rand-int(6)   // Random integer 0-5 (like a die roll minus 1)

round core

fn (x: Int | Dec): Int

Round x to the nearest integer. Halfway values round away from zero. A Dec too large for an Int gives Err("Integer overflow").

Example

round(2.3)   // 2
round(2.5)   // 3
round(2.7)   // 3
round(-2.5)  // -3 (away from zero)
round(-2.4)  // -2

sub core

fn (x: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec): Int | Dec
fn (x: Int | Dec, y: Int | Dec, z: Int | Dec, ...rest: Int | Dec): Int | Dec

Subtract each later number from x, left to right. With one number, negate it. An Int difference that doesn't fit in an Int is Err("Integer overflow").

Example

sub(5, 3)      // 2
sub(1, 5)      // -4
sub(3.5, 1.5)  // 2.0
sub(10, 1, 2)  // 7
sub(5)         // -5