Math Class
Math has extra MacroForge methods on top of the standard ones.
Math.eval(expression)
Evaluate a math expression string.
let result = Math.eval('(12 + 8) / 2 + 5');
console.log(result);Math.clamp(value, min, max)
Clamp a number into a range.
let health = 145;
let result = Math.clamp(health, 0, 100);
console.log(result);Math.lerp(start, end, t)
Linear interpolation.
let startX = 120;
let endX = 540;
let result = Math.lerp(startX, endX, 0.25);
console.log(result);Math.random()
Random decimal from 0.0 to 1.0.
let result = Math.random();
console.log(result);Math.random(min, max)
Random decimal in a range.
let result = Math.random(0.8, 1.2);
console.log(result);Math.randomInt(min, max)
Random integer in a range.
let retries = Math.randomInt(2, 5);
console.log(retries);Math.floor(...)
Number helpers.
let result = Math.floor(12.6);
console.log(result);Math.ceil(...)
Number helpers.
let result = Math.ceil(12.1);
console.log(result);Math.abs(...)
Number helpers.
let result = Math.abs(-42);
console.log(result);Math.min(...)
Minimum and maximum.
let result = Math.min(320, 280, 410);
console.log(result);Math.max(...)
Minimum and maximum.
let result = Math.max(320, 280, 410);
console.log(result);Math.pow(...)
Power and square root.
let result = Math.pow(2, 4);
console.log(result);Math.sqrt(...)
Power and square root.
let result = Math.sqrt(144);
console.log(result);Math.sin(...)
Trigonometry.
let radians = Math.toRadians(30);
let result = Math.sin(radians);
console.log(result);Math.cos(...)
Trigonometry.
let radians = Math.toRadians(60);
let result = Math.cos(radians);
console.log(result);Math.tan(...)
Trigonometry.
let radians = Math.toRadians(45);
let result = Math.tan(radians);
console.log(result);Math.toRadians(...)
Angle conversion.
let result = Math.toRadians(180);
console.log(result);Math.toDegrees(...)
Angle conversion.
let result = Math.toDegrees(Math.PI() / 2);
console.log(result);Math.distance(x1, y1, x2, y2)
Distance between two points.
let result = Math.distance(120, 240, 360, 620);
console.log(result);Math.PI
The standard constants. Write Math.PI, not Math.PI().
let result = Math.PI;
console.log(result);Math.E
The standard constants. Write Math.PI, not Math.PI().
let result = Math.E;
console.log(result);Math.round(value, decimals?)
Rounds to decimals places when given; with one argument it rounds to a whole number.
let result = Math.round(1, 2);
console.log(result);