Skip to main content

Number Library (Num)

Linked to every numeric type — int, float, and units. Called as a method on the number itself (val.abs()) or through Num directly (Num.abs(val)) — both reach the same function.

Basics

Num.abs(val: int | float) -> int | float

num-abs.stof
#[main]
fn main() {
  const v = -2;
  pln(v.abs());
}
Output

Num.sqrt(val: int | float) -> float

num-sqrt.stof
#[main]
fn main() {
  const v = 4;
  pln(v.sqrt());
}
Output

Num.cbrt(val: int | float) -> float

num-cbrt.stof
#[main]
fn main() {
  const v = 8;
  pln(v.cbrt());
}
Output

Num.pow(val: int | float, to: int | float = 2) -> float

num-pow.stof
#[main]
fn main() {
  const val = 10;
  pln(val.pow(to = 2), val.pow());
}
Output

Num.round(val: int | float, places: int = 0) -> int | float

num-round.stof
#[main]
fn main() {
  const val = 10.348;
  pln(val.round(2));
}
Output

Num.floor(val: int | float) -> int | float

num-floor.stof
#[main]
fn main() {
  const v = 2.4;
  pln(v.floor());
}
Output

Num.ceil(val: int | float) -> int | float

num-ceil.stof
#[main]
fn main() {
  const v = 2.4;
  pln(v.ceil());
}
Output

Num.trunc(val: int | float) -> int | float

The integer part.

num-trunc.stof
#[main]
fn main() {
  const v = 2.4;
  pln(v.trunc());
}
Output

Num.fract(val: int | float) -> int | float

The fractional part.

num-fract.stof
#[main]
fn main() {
  const v = 2.4;
  pln(v.fract());
}
Output

Num.signum(val: int | float) -> int | float

-1 or 1.

num-signum.stof
#[main]
fn main() {
  pln((42).signum(), (-42).signum());
}
Output

Trigonometry

Angle-returning functions come back with rad units automatically. The examples below lean on identities with clean, predictable results.

Num.sin(val: int | float) -> float

num-sin.stof
#[main]
fn main() {
  pln((0).sin());
}
Output

Num.cos(val: int | float) -> float

num-cos.stof
#[main]
fn main() {
  pln((0).cos());
}
Output

Num.tan(val: int | float) -> float

num-tan.stof
#[main]
fn main() {
  pln((0).tan());
}
Output

Num.asin(val: int | float) -> rad

num-asin.stof
#[main]
fn main() {
  pln((0).asin());
}
Output

Num.acos(val: int | float) -> rad

num-acos.stof
#[main]
fn main() {
  pln((1).acos());
}
Output

Num.atan(val: int | float) -> rad

num-atan.stof
#[main]
fn main() {
  pln((0).atan());
}
Output

Num.atan2(y: int | float, x: int | float) -> rad

The four-quadrant arctangent — cast to deg to read it more naturally.

num-atan2.stof
#[main]
fn main() {
  pln((Num.atan2(1, 2) as deg).round());
}
Output

Num.sinh(val: int | float) -> float

num-sinh.stof
#[main]
fn main() {
  pln((0).sinh());
}
Output

Num.cosh(val: int | float) -> float

num-cosh.stof
#[main]
fn main() {
  pln((0).cosh());
}
Output

Num.tanh(val: int | float) -> float

num-tanh.stof
#[main]
fn main() {
  pln((0).tanh());
}
Output

Num.asinh(val: int | float) -> float

num-asinh.stof
#[main]
fn main() {
  pln((0).asinh());
}
Output

Num.acosh(val: int | float) -> float

num-acosh.stof
#[main]
fn main() {
  pln((1).acosh());
}
Output

Num.atanh(val: int | float) -> float

num-atanh.stof
#[main]
fn main() {
  pln((0).atanh());
}
Output

Logarithms & Exponents

Num.exp(val: int | float) -> float

e^val.

num-exp.stof
#[main]
fn main() {
  pln((1).exp().round(3));
}
Output

Num.exp2(val: int | float) -> float

2^val.

num-exp2.stof
#[main]
fn main() {
  pln((2).exp2());
}
Output

Num.ln(val: int | float) -> float

Natural log.

num-ln.stof
#[main]
fn main() {
  pln((1).ln());
}
Output

Num.log(val: int | float, base: int | float = 10) -> float

num-log.stof
#[main]
fn main() {
  pln((2).log().round(3));
}
Output

Representations

Num.to_string(val: int | float) -> str

Same as Std.str, but a method on the number itself.

num-to-string.stof
#[main]
fn main() {
  pln((10).to_string());
}
Output

Num.bin(val: int) -> str

num-bin.stof
#[main]
fn main() {
  pln((10).bin());
}
Output

Num.hex(val: int) -> str

num-hex.stof
#[main]
fn main() {
  pln((10).hex());
}
Output

Num.oct(val: int) -> str

num-oct.stof
#[main]
fn main() {
  pln((10).oct());
}
Output

Units

Num.has_units(val: int | float) -> bool

num-has-units.stof
#[main]
fn main() {
  const val = 10kg;
  pln(val.has_units());
}
Output

Num.is_length(val: int | float) -> bool

num-is-length.stof
#[main]
fn main() {
  const val = 10m;
  pln(val.is_length());
}
Output

Num.is_mass(val: int | float) -> bool

num-is-mass.stof
#[main]
fn main() {
  const val = 10kg;
  pln(val.is_mass());
}
Output

Num.is_time(val: int | float) -> bool

num-is-time.stof
#[main]
fn main() {
  const val = 10s;
  pln(val.is_time());
}
Output

Num.is_temp(val: int | float) -> bool

num-is-temp.stof
#[main]
fn main() {
  const val = 10F;
  pln(val.is_temp());
}
Output

Num.is_angle(val: int | float) -> bool

num-is-angle.stof
#[main]
fn main() {
  const val = 10deg;
  pln(val.is_angle());
}
Output

Num.is_memory(val: int | float) -> bool

num-is-memory.stof
#[main]
fn main() {
  const val = 10MB;
  pln(val.is_memory());
}
Output

Num.remove_units(val: int | float) -> int | float

num-remove-units.stof
#[main]
fn main() {
  const val = 10kg;
  pln(typeof val.remove_units());
}
Output

Num.to_units(val: int | float, units: str | float) -> units

Doesn't modify the original.

num-to-units.stof
#[main]
fn main() {
  const val = 10kg;
  pln(val.to_units('g'), val);
}
Output

Iteration & Misc

Num.len(val: int | float) -> int

The number itself — makes a plain number iterable the same way a list is.

num-len.stof
#[main]
fn main() {
  pln((10).len());
}
Output

Num.at(val: int | float, index: int) -> int

Clamps to val if index is larger.

num-at.stof
#[main]
fn main() {
  pln((10).at(5), (10).at(20));
}
Output

Num.inf(val: int | float) -> bool

num-inf.stof
#[main]
fn main() {
  pln((14).inf());
}
Output

Num.nan(val: int | float) -> bool

num-nan.stof
#[main]
fn main() {
  pln((14).nan());
}
Output

Num.min(..) -> unknown

Considers units if given.

num-min.stof
#[main]
fn main() {
  pln(Num.min(12, 23, 10, 42, 0));
}
Output

Num.max(..) -> unknown

num-max.stof
#[main]
fn main() {
  pln(Num.max(12, 23, 10, 42, 0));
}
Output