Math Utilities

Overview

Math helpers in BattleScript live in the std::math standard library. Like all std::libraries it is available automatically — no import is required to call it.

The full, canonical reference (with descriptions and examples for every function) is on the Math (std::math) standard library page. The list below summarises everything the library currently provides.

// No import needed
int roll = std::math.randomInt(1, 6);
float clamped = std::math.clamp(120, 0, 100);   // 100
std::console.log("Roll:", roll, "Clamped:", clamped);

Available functions

These are the only functions provided by std::math. There is no general random/randomFloat, no trigonometric functions (sin/cos/tan), no angle conversions, and no seeding.

native int   std::math.randomInt (int a, int b);
native float std::math.abs   (float x);
native float std::math.clamp (float val, float min, float max);
native float std::math.min   (float a, float b);
native float std::math.max   (float a, float b);
native float std::math.round (float x);
native float std::math.floor (float x);
native float std::math.ceil  (float x);
native float std::math.sqrt  (float x);
native float std::math.pow   (float base, float exponent);
native float std::math.PI    ();
  • randomInt(int a, int b) — random integer between a and b (inclusive).
  • abs(float x) — absolute value of x.
  • clamp(float val, float min, float max) — constrains val between min and max.
  • min(float a, float b) — the smaller of two values.
  • max(float a, float b) — the larger of two values.
  • round(float x) — rounds to the nearest integer value.
  • floor(float x) — rounds down.
  • ceil(float x) — rounds up.
  • sqrt(float x) — square root of x.
  • pow(float base, float exponent)base raised to exponent.
  • PI() — returns the value of pi.

Related Topics