Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/elfrask/cls/llms.txt

Use this file to discover all available pages before exploring further.

The math module is part of the CLS core standard library — it is always available regardless of which node (desktop or lightweight) is executing your program. It provides the most commonly needed mathematical operations: absolute value, rounding, power, roots, trigonometry, logarithm, random number generation, a numeric range utility, and the two fundamental constants PI and E. All functions accept both int and float arguments and generally return float, with the rounding functions (floor, ceil, round) returning int.

Import

import "math" as math;
You may also use a bare import, which binds the module to the name math automatically:
import "math";
Or import individual names with from:
from "math" import sqrt, pow, PI;

Constants

NameValueDescription
math.PI3.141592653589793The ratio of a circle’s circumference to its diameter
math.E2.718281828459045Euler’s number, the base of the natural logarithm
import "math" as math;

print(math.PI);   # 3.141592653589793
print(math.E);    # 2.718281828459045

Function Reference

FunctionSignatureReturn typeDescription
absabs(x: float) -> floatint or floatAbsolute value — returns int if the input is int, float otherwise
sqrtsqrt(x: float) -> floatfloatSquare root of x
powpow(base: float, exp: float) -> floatfloatRaises base to the power exp
minmin(a: float, b: float) -> floatfloatThe smaller of a and b
maxmax(a: float, b: float) -> floatfloatThe larger of a and b
floorfloor(x: float) -> intintLargest integer ≤ x
ceilceil(x: float) -> intintSmallest integer ≥ x
roundround(x: float) -> intintNearest integer to x (half-up rounding)
randomrandom() -> floatfloatA pseudo-random float in [0, 1)
sinsin(x: float) -> floatfloatSine of x (radians)
coscos(x: float) -> floatfloatCosine of x (radians)
tantan(x: float) -> floatfloatTangent of x (radians)
loglog(x: float) -> floatfloatNatural logarithm of x
rangerange(start: int, end: int) -> ArrayArray<int>Integer array [start, start+1, ..., end-1]

Examples

Arithmetic

import "math" as math;

print(math.abs(-5));          # 5
print(math.abs(-3.5));        # 3.5
print(math.sqrt(16));         # 4.0
print(math.pow(2, 10));       # 1024.0
print(math.min(3, 7));        # 3.0
print(math.max(3, 7));        # 7.0

Rounding

import "math" as math;

var x = 3.7;
print(math.floor(x));   # 3
print(math.ceil(x));    # 4
print(math.round(x));   # 4

print(math.floor(-1.2));  # -2
print(math.ceil(-1.2));   # -1

Trigonometry

import "math" as math;

var angle = math.PI / 4.0;    # 45 degrees in radians
print(math.sin(angle));       # ~0.7071
print(math.cos(angle));       # ~0.7071
print(math.tan(angle));       # ~1.0

Logarithm

import "math" as math;

print(math.log(math.E));   # 1.0
print(math.log(1.0));      # 0.0

Random numbers

import "math" as math;

var r = math.random();
print(r);   # e.g. 0.742193

# Random integer from 0 to 9
var n = math.floor(math.random() * 10.0);

Range

import "math" as math;

var r = math.range(1, 5);
print(r);   # [1, 2, 3, 4]

# Sum all numbers 0–99
var total = 0;
for each i in (math.range(0, 100)) {
    total = total + i;
};
print(total);   # 4950

Real-world use: distance between two points

import "math" as math;

function distance(x1: float, y1: float, x2: float, y2: float) -> float {
    var dx = x2 - x1;
    var dy = y2 - y1;
    return math.sqrt(math.pow(dx, 2) + math.pow(dy, 2));
};

print(distance(0.0, 0.0, 3.0, 4.0));   # 5.0
math.abs preserves the input type: abs(-5) returns 5 (int), while abs(-3.5) returns 3.5 (float). All other functions always return float, except floor, ceil, round, and range which return int or Array<int>.

Build docs developers (and LLMs) love