Math

The Math: module provides numeric operations. All methods follow the ByRef convention — pre-declare a receiver variable and pass it as the last argument.


Call Result
Math:Abs(n, out) / Math:Absolute(n, out) Absolute value
Math:Round(n, out) Round to nearest integer
Math:Floor(n, out) Floor
Math:Ceil(n, out) / Math:Ceiling(n, out) Ceiling
Math:Sqrt(n, out) / Math:SquareRoot(n, out) Square root
Math:Sin(n, out) / Math:Sine(n, out) Sine (radians)
Math:Cos(n, out) / Math:Cosine(n, out) Cosine (radians)
Math:Tan(n, out) / Math:Tangent(n, out) Tangent (radians)
Math:Log(n, out) Natural logarithm
Math:Exp(n, out) e raised to the power n
Math:Power(base, exp, out) / Math:Pow base raised to the power exp
Math:Min(list, out) Minimum value in a List:Number
Math:Max(list, out) Maximum value in a List:Number
Math:Clamp(n, min, max, out) Clamp n to the range [min, max]
Math:Lerp(a, b, t, out) Linear interpolation: a + t * (b - a)
Math:Random(out) Random float in [0, 1)
Math:Random(max, out) Random float in [0, max)
Math:Seed(n) Seed the RNG with integer n for reproducible sequences

Math:Random uses srand/rand — not cryptographically secure. Use Math:Power for exponentiation; ^ is bitwise XOR.

Division by zero (via /, %, /=, %=) throws MathException:

try {
    Var:Number x = 10 / 0;
} catch (MathException e) {
    Konsol:Print(e.message);    // Division by zero
}
Var:Number result;
Math:Abs(-7, result);
Konsol:Print(result);       // 7

Math:Power(2, 10, result);
Konsol:Print(result);       // 1024

Math:Sqrt(144, result);
Konsol:Print(result);       // 12

Math:Clamp(150, 0, 100, result);
Konsol:Print(result);       // 100

Math:Seed(42);
Math:Random(result);
Konsol:Print(result);       // reproducible float in [0, 1)

Examples

Math

// 10_math.ks — Math module

Konsol:Print("=== Math Module ===");

Var:Number _ret = 0;

// Abs
Math:Abs(-7.5, _ret);   Konsol:Print("Abs(-7.5)    = ${_ret}");
Math:Abs(3.0, _ret);    Konsol:Print("Abs(3.0)     = ${_ret}");

// Round
Math:Round(2.3, _ret);  Konsol:Print("Round(2.3)   = ${_ret}");
Math:Round(2.7, _ret);  Konsol:Print("Round(2.7)   = ${_ret}");
Math:Round(-1.5, _ret); Konsol:Print("Round(-1.5)  = ${_ret}");

// Floor
Math:Floor(3.9, _ret);  Konsol:Print("Floor(3.9)   = ${_ret}");
Math:Floor(-1.1, _ret); Konsol:Print("Floor(-1.1)  = ${_ret}");

// Ceil
Math:Ceil(3.1, _ret);   Konsol:Print("Ceil(3.1)    = ${_ret}");
Math:Ceil(-2.9, _ret);  Konsol:Print("Ceil(-2.9)   = ${_ret}");

// Sqrt
Math:Sqrt(9, _ret);     Konsol:Print("Sqrt(9)      = ${_ret}");
Math:Sqrt(2, _ret);     Konsol:Print("Sqrt(2)      = ${_ret}");
Math:Sqrt(144, _ret);   Konsol:Print("Sqrt(144)    = ${_ret}");

// Power
Math:Power(2, 10, _ret);  Konsol:Print("Power(2,10)  = ${_ret}");
Math:Power(3, 3, _ret);   Konsol:Print("Power(3,3)   = ${_ret}");
Math:Power(9, 0.5, _ret); Konsol:Print("Power(9,0.5) = ${_ret}");
Math:Power(5, 0, _ret);   Konsol:Print("Power(5,0)   = ${_ret}");

// Trig (radians)
#define PI 3.14159265358979

Math:Sin(0, _ret);      Konsol:Print("Sin(0)       = ${_ret}");
Math:Cos(0, _ret);      Konsol:Print("Cos(0)       = ${_ret}");
Math:Tan(0, _ret);      Konsol:Print("Tan(0)       = ${_ret}");

// Sin(PI/6) ≈ 0.5
Var:Number halfPi = PI / 6;
Math:Sin(halfPi, _ret);
Konsol:Print("Sin(PI/6)    = ${_ret}");

// Log
Math:Log(1, _ret);       Konsol:Print("Log(1)       = ${_ret}");
Math:Log(2.71828, _ret); Konsol:Print("Log(e)       = ${_ret}");

// Random
Konsol:Print("--- Random ---");
Math:Seed(42);
Math:Random(_ret);
Konsol:Print("Random()     = ${_ret}");

Math:Random(100, _ret);
Konsol:Print("Random(100)  = ${_ret}");

Math:Random(100, _ret);
Konsol:Print("Random(100)  = ${_ret}");

// Chaining: use result in expression
Math:Sqrt(25, _ret);
Var:Number side = _ret;
Konsol:Print("area of square (side=5): ${side * side}");

// Hypotenuse
Var:Number dx = 3;
Var:Number dy = 4;
Math:Sqrt(dx * dx + dy * dy, _ret);
Konsol:Print("hypotenuse(3,4) = ${_ret}");

Math additions

// 45_math_additions.ks — Math:Exp, Min, Max, Clamp, Lerp

Konsol:Print("=== Math Additions ===");

Var:Number _ret = 0;

// Power / Pow alias
Math:Power(2, 0, _ret);    Konsol:Print("Power(2,0)   = ${_ret}");
Math:Power(2, 1, _ret);    Konsol:Print("Power(2,1)   = ${_ret}");
Math:Power(2, 8, _ret);    Konsol:Print("Power(2,8)   = ${_ret}");
Math:Power(10, 3, _ret);   Konsol:Print("Power(10,3)  = ${_ret}");
Math:Power(2, -1, _ret);   Konsol:Print("Power(2,-1)  = ${_ret}");
Math:Pow(4, 0.5, _ret);    Konsol:Print("Pow(4,0.5)   = ${_ret}");

// Exp
Math:Exp(0, _ret);   Konsol:Print("Exp(0)       = ${_ret}");
Math:Exp(1, _ret);   Konsol:Print("Exp(1)       = ${_ret}");
Math:Exp(2, _ret);   Konsol:Print("Exp(2)       = ${_ret}");
Math:Exp(-1, _ret);  Konsol:Print("Exp(-1)      = ${_ret}");

// Min / Max over a List
Var:List:Number nums = [3, 1, 4, 1, 5, 9, 2, 6];
Math:Min(nums, _ret); Konsol:Print("Min([3,1,4,1,5,9,2,6]) = ${_ret}");
Math:Max(nums, _ret); Konsol:Print("Max([3,1,4,1,5,9,2,6]) = ${_ret}");

// single-element list
Var:List:Number one = [7];
Math:Min(one, _ret); Konsol:Print("Min([7])     = ${_ret}");
Math:Max(one, _ret); Konsol:Print("Max([7])     = ${_ret}");

// negative values
Var:List:Number negs = [-5, -2, -8, -1];
Math:Min(negs, _ret); Konsol:Print("Min(negs)    = ${_ret}");
Math:Max(negs, _ret); Konsol:Print("Max(negs)    = ${_ret}");

// Clamp
Math:Clamp(5, 1, 10, _ret);   Konsol:Print("Clamp(5,1,10)  = ${_ret}");
Math:Clamp(-3, 0, 100, _ret); Konsol:Print("Clamp(-3,0,100)= ${_ret}");
Math:Clamp(200, 0, 100, _ret);Konsol:Print("Clamp(200,0,100)= ${_ret}");
Math:Clamp(0, 0, 1, _ret);    Konsol:Print("Clamp(0,0,1)   = ${_ret}");
Math:Clamp(1, 0, 1, _ret);    Konsol:Print("Clamp(1,0,1)   = ${_ret}");

// Lerp
Math:Lerp(0, 10, 0, _ret);    Konsol:Print("Lerp(0,10,0)   = ${_ret}");
Math:Lerp(0, 10, 1, _ret);    Konsol:Print("Lerp(0,10,1)   = ${_ret}");
Math:Lerp(0, 10, 0.5, _ret);  Konsol:Print("Lerp(0,10,0.5) = ${_ret}");
Math:Lerp(2, 8, 0.25, _ret);  Konsol:Print("Lerp(2,8,0.25) = ${_ret}");
Math:Lerp(-10, 10, 0.5, _ret);Konsol:Print("Lerp(-10,10,0.5)= ${_ret}");