Advanced utilities
| Function | Signature | Description |
|---|---|---|
approxEqual | (a: f64, b: f64, tolerance?: f64) => boolean | Compare floats within tolerance |
lerp | (start: f64, end: f64, amount: f64) => f64 | Linear interpolation |
inverseLerp | (start: f64, end: f64, value: f64) => f64 | Inverse linear interpolation |
remap | (value: f64, inMin: f64, inMax: f64, outMin: f64, outMax: f64) => f64 | Remap value between ranges |
smoothstep | (edge0: f64, edge1: f64, x: f64) => f64 | Smooth Hermite interpolation |
gcd | (a: u32, b: u32) => u32 | Greatest common divisor |
lcm | (a: u32, b: u32) => u32 | Least common multiple |
randomRange | (min: f64, max: f64) => f64 | Random float in half-open interval |
approxEqual()
Checks if two floats are equal within an optional tolerance.
Signature
approxEqual(a: f64, b: f64, tolerance?: f64): booleantyrus
const a: f64 = 1.0;
const b: f64 = 1.0000001;
const result: boolean = approxEqual(a, b);
print(result); // truetyrus
const a: f64 = 1.0;
const b: f64 = 1.1;
const result: boolean = approxEqual(a, b, 0.05);
print(result); // falselerp()
Linearly interpolates between two values.
Signature
lerp(start: f64, end: f64, amount: f64): f64tyrus
const start: f64 = 0.0;
const end: f64 = 100.0;
const amount: f64 = 0.5;
const result: f64 = lerp(start, end, amount);
print(result); // 50.0inverseLerp()
Returns how far a value is between two endpoints as a normalized fraction.
Signature
inverseLerp(start: f64, end: f64, value: f64): f64tyrus
const start: f64 = 0.0;
const end: f64 = 100.0;
const value: f64 = 50.0;
const result: f64 = inverseLerp(start, end, value);
print(result); // 0.5remap()
Remaps a value from one range to another.
Signature
remap(value: f64, inMin: f64, inMax: f64, outMin: f64, outMax: f64): f64tyrus
const value: f64 = 50.0;
const result: f64 = remap(value, 0.0, 100.0, 0.0, 1.0);
print(result); // 0.5smoothstep()
Returns smooth Hermite interpolation between two edges.
Signature
smoothstep(edge0: f64, edge1: f64, x: f64): f64tyrus
const result: f64 = smoothstep(0.0, 1.0, 0.5);
print(result); // 0.5gcd()
Returns the greatest common divisor of two unsigned integers.
Signature
gcd(a: u32, b: u32): u32tyrus
const a: u32 = 12;
const b: u32 = 8;
const result: u32 = gcd(a, b);
print(result); // 4lcm()
Returns the least common multiple of two unsigned integers.
Signature
lcm(a: u32, b: u32): u32tyrus
const a: u32 = 4;
const b: u32 = 6;
const result: u32 = lcm(a, b);
print(result); // 12randomRange()
Returns a random floating-point number in the half-open interval [min, max).
Signature
randomRange(min: f64, max: f64): f64tyrus
const min: f64 = 1.0;
const max: f64 = 10.0;
const result: f64 = randomRange(min, max);
print(result); // A random float in [1.0, 10.0)