Math Extensions
Side: Shared (Client + Server)
Extensions to Lua's built-in math library. Available globally after loading twinded_libs.
Functions
math.lerp(start, finish, factor)
Linear interpolation between two values.
| Parameter | Type | Description |
|---|---|---|
start | number | Start value |
finish | number | End value |
factor | number | Interpolation factor (0.0 to 1.0) |
Returns: number
math.round(value, decimal_places?)
Rounds a number to the specified decimal places.
| Parameter | Type | Default | Description |
|---|---|---|---|
value | number | — | Value to round |
decimal_places | integer? | 0 | Number of decimal places |
Returns: number
math.clamp(value, lower, upper)
Clamps a value between a minimum and maximum.
| Parameter | Type | Description |
|---|---|---|
value | number | Value to clamp |
lower | number | Minimum bound |
upper | number | Maximum bound |
Returns: number
math.toHex(value, with_prefix?)
Converts an integer to a hexadecimal string.
| Parameter | Type | Default | Description |
|---|---|---|---|
value | integer | — | Value to convert |
with_prefix | boolean? | false | If true, prepends 0x |
Returns: string
math.fromHex(hex_str)
Converts a hexadecimal string to an integer.
| Parameter | Type | Description |
|---|---|---|
hex_str | string | Hex string (with or without 0x prefix) |
Returns: integer
math.toSigned(value)
Converts an unsigned 32-bit integer to a signed integer.
| Parameter | Type | Description |
|---|---|---|
value | integer | Unsigned 32-bit value |
Returns: number
Examples
lua
-- Linear interpolation
local mid = math.lerp(0, 100, 0.5) -- 50
-- Rounding
math.round(3.14159) -- 3
math.round(3.14159, 2) -- 3.14
-- Clamping
math.clamp(150, 0, 100) -- 100
math.clamp(-5, 0, 100) -- 0
math.clamp(50, 0, 100) -- 50
-- Hex conversion
math.toHex(255) -- "ff"
math.toHex(255, true) -- "0xff"
math.fromHex("ff") -- 255
math.fromHex("0xff") -- 255
-- Signed conversion (useful for native hashes)
local hash = math.toSigned(0xDEADBEEF)
