coloX Math Functions
coloX provides math and bitwise functions, along with a random number generator. Some functions differ from standard Lua's math library (for example, trigonometric functions use screen coordinates).
Contents
1. Basic Math
max(x, y) → number
Returns the greater of x and y.
min(x, y) → number
Returns the lesser of x and y.
mid(x, y, z) → number
Returns the middle value of x, y, and z after sorting. Often used to clamp a value to a range:
-- Clamp x to [0, 100]
local clamped = mid(0, x, 100)
ceil(x) → number
Rounds upward, returning the smallest integer ≥ x.
flr(x) → number
Rounds downward, returning the largest integer ≤ x.
flr(3.7) -- → 3
flr(-1.2) -- → -2
sqrt(x) → number
Returns the square root of x. Returns 0 for x < 0.
abs(x) → number
Returns the absolute value of x.
sgn(x) → number
Returns the sign of x: -1 for x < 0, 1 for x ≥ 0.
2. Trigonometric Functions
sin(x) → number
Returns the sine of x.
cos(x) → number
Returns the cosine of x.
Important: Unlike standard
math.sin/math.cos, coloX trigonometric functions use screen coordinates, with the Y-axis direction reversed.
Parameter x: An angle where [0, 1] represents one full turn (0°~360°), counterclockwise on screen:
| x value | Screen direction | Conventional angle |
|---|---|---|
| 0 | Right (→) | 0° |
| 0.25 | Up (↑) | 90° |
| 0.5 | Left (←) | 180° |
| 0.75 | Down (↓) | 270° |
-- Distribute 8 points evenly on a circle of radius 50
for i = 0, 7 do
local angle = i / 8
local px = 120 + cos(angle) * 50
local py = 120 + sin(angle) * 50
pset(px, py, white)
end
atan2(dx, dy) → number
Computes an angle from dx and dy, returning a value in [0, 1). The angle direction matches sin/cos (counterclockwise on screen).
atan2(0, -1) -- → 0.25 (up)
atan2(1, 0) -- → 0 (right)
atan2(0, 1) -- → 0.75 (down)
Usage: Determine the direction from one point to another.
-- Calculate the angle from (px,py) to (tx,ty)
local angle = atan2(tx - px, ty - py)
3. Bitwise Operations
Bitwise functions have the same functionality as Lua's bitwise operators (&, |, ~, <<, >>), but do not throw exceptions for invalid arguments.
| Function | Equivalent operation | Description |
|---|---|---|
band(x, y) | x & y | Bitwise AND |
bor(x, y) | x \| y | Bitwise OR |
bxor(x, y) | x ~ y | Bitwise XOR |
bnot(x) | ~x | Bitwise NOT |
shl(x, n) | x << n | Left shift, filling with 0 on the right |
shr(x, n) | x >> n | Arithmetic right shift, extending the sign bit on the left |
lshr(x, n) | None | Logical right shift, filling with 0 on the left |
rotl(x, n) | None | Rotate left |
rotr(x, n) | None | Rotate right |
Functions vs operators:
-- Function form: silently return on invalid arguments
band(-1, 0xff) -- Does not raise an error
-- Operator form: raise an exception on invalid arguments
-1 & 0xff -- May raise an error
4. Random Numbers
rnd([x]) → number | value
Returns a random number n, 0 ≤ n < x. Omitting x is equivalent to rnd(1); returns 0 for x ≤ 0.
Note: The return value is not an integer. Use flr(rnd(x)) for integers.
If x is an array (table), returns a randomly selected element from table[1] through table[#table].
-- Random integer 0..9
local i = flr(rnd(10))
-- Choose a random array element
local colors = {red, green, blue, yellow}
local c = rnd(colors) -- Choose a random color
srand(x)
Sets the pseudorandom seed. Identical seeds produce identical random sequences.
srand(42) -- Fixed seed
print(rnd(100)) -- Same result on each run
5. Conversion and String Utilities
The following global functions belong to the same family as the math functions (global functions coexisting with the standard math.* and string.* libraries).
tostr(v [, flags]) → string
Converts a value to a string. Strings are returned unchanged; booleans become "true"/"false"; other types become "[type name]".
For numeric v with truthy flags, outputs fixed-point format: nonzero flags&2 gives the decimal integer x*0x10000; flags&3==3 gives 32-bit hexadecimal; otherwise gives fixed-point hexadecimal in "0xHigh16.Low16" form.
tostr(3.7) -- "3.7"
tostr(0.5, true) -- "0x0000.8000" (fixed-point hex)
tostr(nil) -- "[nil]"
tonum(v) → number
Converts a string to a number. On parse failure, returns no value (nil), without an error.
tonum("3.14") -- 3.14
tonum("abc") -- nil
chr(c) → string
Converts a byte value to a single-byte string (0..255).
ord(s [, i]) → number
Gets the value of the byte at position i (1-based, default 1). Returns no value if i is out of range.
split(str [, sep [, convert]]) → table
Splits a string into an array. For string sep, its first character is the delimiter (default ","); for numeric sep, splits into fixed-length byte chunks (≤0 treated as 1). convert defaults to true, attempting numeric conversion for each element (unconvertible elements remain strings).
split("1,2,3") -- {1, 2, 3}
split("a,b,c", ",") -- {"a", "b", "c"}
split("AABBCC", 2) -- {"AA", "BB", "CC"}
split("1,2,x", ",", true) -- {1, 2, "x"}
API Index
| Function | Description |
|---|---|
max(x, y) | Greater value |
min(x, y) | Lesser value |
mid(x, y, z) | Middle value |
ceil(x) | Round upward |
flr(x) | Round downward (floor) |
sqrt(x) | Square root |
abs(x) | Absolute value |
sgn(x) | Sign |
sin(x) | Sine (screen coordinates, x=[0,1] for one turn) |
cos(x) | Cosine (screen coordinates, x=[0,1] for one turn) |
atan2(dx, dy) | Arctangent → [0, 1) |
band(x, y) | Bitwise AND |
bor(x, y) | Bitwise OR |
bxor(x, y) | Bitwise XOR |
bnot(x) | Bitwise NOT |
shl(x, n) | Left shift |
shr(x, n) | Arithmetic right shift |
lshr(x, n) | Logical right shift |
rotl(x, n) | Rotate left |
rotr(x, n) | Rotate right |
rnd(x) | Random number / random array element |
srand(x) | Set the random seed |
tostr(v [, flags]) | Value to string (flags: fixed-point hex format) |
tonum(v) | String to number (nil on failure) |
chr(c) | Byte value to single-byte string |
ord(s [, i]) | Get the i-th byte's value |
split(str [, sep [, convert]]) | Split a string into an array |