Math
import "std/core" (included in import "std") · namespace std.core
Mathematical functions and constants, from C's <math.h>. Floating-point functions take and return float (64-bit). abs, min, max, clamp and sign also have int and long versions, so Math.max(3, 7) stays an int. Angles are in radians.
import "std"
fn main() {
print(Math.sqrt(16.0))
print(Math.pow(2.0, 10.0))
print(Math.max(3, 7))
print(Math.clamp(150, 0, 100))
print(Math.roundTo(Math.PI, 3))
print(Math.round(2.5))
print(Math.floor(-1.5))
print(Math.hypot(3.0, 4.0))
print(Math.gcd(84, 36))
print(Math.isPrime(97))
print(Math.factorial(10))
print(Math.toDegrees(Math.atan2(1.0, 1.0)))
}4
1024
7
100
3.142
3
-2
5
12
true
3628800
45roundrounds halves away from zero;trunccuts toward zero;floorandceilgo down and up.isNaN,isInfiniteandisFinitecheck float results:0.0 / 0.0is NaN,1.0 / 0.0is infinite.factorialoverflows past 20!.
For random numbers see Random.
More functions
logBase, root, integer ipow and isqrt, floorDiv/floorMod (which round toward negative infinity, so floorMod(-7, 3) is 2), lerp and mapRange for interpolation, isClose for comparing computed floats, binomial, fib, primeFactors, isPowerOfTwo/nextPowerOfTwo, and the constants SQRT2, LN2, LN10 and PHI.
import "std"
fn main() {
print(Math.logBase(1024.0, 2.0))
print(Math.floorMod(-7, 3))
print(Math.mapRange(25.0, 0.0, 100.0, 0.0, 255.0))
print(Math.isClose(0.1 + 0.2, 0.3, 1e-9))
print(Math.binomial(52, 5))
print(Math.primeFactors(360))
}10
2
63.75
true
2598960
[2, 2, 2, 3, 3, 5]Stats
Stats summarises a list of numbers (Array<float> or Array<int>): sum, mean, median, min, max, variance, sampleVariance, stddev, percentile, mode and correlation. An empty list throws ValueError.
import "std"
fn main() {
let times: Array<float> = [12.0, 15.0, 11.0, 30.0, 14.0, 13.0]
print(Stats.mean(times))
print(Stats.median(times))
print(Math.roundTo(Stats.stddev(times), 2))
print(Stats.percentile(times, 90.0))
let rolls: Array<int> = [3, 6, 3, 1, 3, 6]
print(Stats.mode(rolls))
}15.833333333333334
13.5
6.47
22.5
3API
class Math
Static members
static readonly PI: float = 3.141592653589793static readonly E: float = 2.718281828459045static readonly TAU: float = 6.283185307179586static readonly SQRT2: float = 1.4142135623730951static readonly LN2: float = 0.6931471805599453static readonly LN10: float = 2.302585092994046static readonly PHI: float = 1.618033988749895static fn pi() -> float— π (3.14159...).static fn e() -> float— Euler's number e (2.71828...).static fn sqrt(x: float) -> float— The square root (NaN for negative x).static fn cbrt(x: float) -> float— The cube root.static fn pow(base: float, exp: float) -> float—baseraised to the powerexp.static fn hypot(x: float, y: float) -> float— sqrt(x*x + y*y), without overflow in between.static fn exp(x: float) -> float— e raised to the power x.static fn log(x: float) -> float— The natural logarithm (base e).static fn log2(x: float) -> float— The base-2 logarithm.static fn log10(x: float) -> float— The base-10 logarithm.static fn sin(x: float) -> float— The sine of x (in radians).static fn cos(x: float) -> float— The cosine of x (in radians).static fn tan(x: float) -> float— The tangent of x (in radians).static fn asin(x: float) -> float— The arcsine, in radians (-π/2..π/2).static fn acos(x: float) -> float— The arccosine, in radians (0..π).static fn atan(x: float) -> float— The arctangent, in radians (-π/2..π/2).static fn atan2(y: float, x: float) -> float— The angle of the point (x, y), in radians (-π..π).static fn sinh(x: float) -> float— The hyperbolic sine.static fn cosh(x: float) -> float— The hyperbolic cosine.static fn tanh(x: float) -> float— The hyperbolic tangent.static fn toRadians(deg: float) -> float— Degrees converted to radians.static fn toDegrees(rad: float) -> float— Radians converted to degrees.static fn floor(x: float) -> float— The largest whole number <= x.static fn ceil(x: float) -> float— The smallest whole number >= x.static fn round(x: float) -> float— The nearest whole number (halves round away from zero).static fn trunc(x: float) -> float— x without its fractional part (towards zero).static fn roundTo(x: float, digits: int) -> float— Round todigitsdecimal places.static fn abs(x: float) -> float— The absolute value.static fn abs(x: int) -> int— The absolute value.static fn abs(x: long) -> long— The absolute value.static fn min(a: float, b: float) -> float— The smaller of a and b.static fn min(a: int, b: int) -> int— The smaller of a and b.static fn min(a: long, b: long) -> long— The smaller of a and b.static fn max(a: float, b: float) -> float— The larger of a and b.static fn max(a: int, b: int) -> int— The larger of a and b.static fn max(a: long, b: long) -> long— The larger of a and b.static fn clamp(x: float, lo: float, hi: float) -> float— x limited to the range lo..hi.static fn clamp(x: int, lo: int, hi: int) -> int— x limited to the range lo..hi.static fn sign(x: float) -> int— -1, 0 or 1: the sign of x.static fn sign(x: int) -> int— -1, 0 or 1: the sign of x.static fn isNaN(x: float) -> bool— Whether x is NaN (not a number).static fn isInfinite(x: float) -> bool— Whether x is +∞ or -∞.static fn isFinite(x: float) -> bool— Whether x is neither infinite nor NaN.static fn gcd(a: long, b: long) -> long— The greatest common divisor (always >= 0).static fn lcm(a: long, b: long) -> long— The least common multiple (0 when either is 0).static fn isPrime(n: long) -> bool— Whether n is a prime number.static fn factorial(n: int) -> long— n! (1 for n <= 1; overflows past 20!).static fn infinity() -> float— Positive infinity.static fn nan() -> float— A NaN ("not a number") value.static fn logBase(x: float, base: float) -> float— The logarithm of x in the given base: Math.logBase(8.0, 2.0) == 3.static fn root(x: float, n: int) -> float— The n-th root of x: Math.root(27.0, 3) == 3.static fn fmod(x: float, y: float) -> float— The remainder of x / y with the sign of x (C fmod).static fn ipow(base: long, exp: int) -> long—baseto a whole power, exactly: Math.ipow(3, 4) == 81 (overflow is not checked).static fn isqrt(n: long) -> long— The integer square root: the largest r with r*r <= n (ValueError for negative n).static fn floorDiv(a: long, b: long) -> long— Division rounding toward negative infinity: Math.floorDiv(-7, 2) == -4.static fn floorMod(a: long, b: long) -> long— The remainder with the sign of b, so it is never negative for positive b: Math.floorMod(-7, 3) == 2 (unlike -7 % 3 == -1).static fn lerp(a: float, b: float, t: float) -> float— The point a fraction t of the way from a to b: lerp(0, 10, 0.25) == 2.5.static fn mapRange(x: float, inLo: float, inHi: float, outLo: float, outHi: float) -> float— x scaled from the range inLo..inHi to outLo..outHi.static fn isClose(a: float, b: float, tolerance: float) -> bool— Whether a and b differ by at mosttolerance(for comparing computed floats).static fn isPowerOfTwo(n: long) -> bool— Whether n is a power of two (1, 2, 4, 8, ...).static fn nextPowerOfTwo(n: long) -> long— The smallest power of two >= n (1 for n <= 1).static fn binomial(n: int, k: int) -> long— The number of ways to choose k items from n ("n choose k"); 0 when k is out of range.static fn fib(n: int) -> long— The n-th Fibonacci number (fib(0) == 0, fib(1) == 1; overflows past fib(92)).static fn primeFactors(n: long) -> Array<long>— The prime factors of n in increasing order, with repeats: factors(12) == [2, 2, 3].
class Stats
Summaries of a list of numbers. Every function takes an Array<float> or an Array<int>; an empty array throws ValueError (except sum, which is 0).
Static members
static fn sum(xs: Array<float>) -> float— The total.static fn sum(xs: Array<int>) -> long— The total.static fn mean(xs: Array<float>) -> float— The arithmetic mean (average).static fn mean(xs: Array<int>) -> float— The arithmetic mean (average).static fn median(xs: Array<float>) -> float— The middle value of the sorted numbers (the mean of the two middle ones for an even count).static fn median(xs: Array<int>) -> float— The middle value of the sorted numbers.static fn min(xs: Array<float>) -> float— The smallest value.static fn min(xs: Array<int>) -> int— The smallest value.static fn max(xs: Array<float>) -> float— The largest value.static fn max(xs: Array<int>) -> int— The largest value.static fn variance(xs: Array<float>) -> float— The population variance: the mean of the squared distances from the mean.static fn variance(xs: Array<int>) -> float— The population variance.static fn sampleVariance(xs: Array<float>) -> float— The sample variance (dividing by n - 1), for estimating from a sample; needs 2 values.static fn stddev(xs: Array<float>) -> float— The population standard deviation (the square root of the variance).static fn stddev(xs: Array<int>) -> float— The population standard deviation.static fn percentile(xs: Array<float>, p: float) -> float— The p-th percentile (0..100), interpolating between neighbours: percentile(xs, 90.0).static fn percentile(xs: Array<int>, p: float) -> float— The p-th percentile (0..100).static fn mode(xs: Array<int>) -> int— The most common value (the smallest one on a tie).static fn correlation(xs: Array<float>, ys: Array<float>) -> float— The Pearson correlation of two equally long lists (-1..1).