Mathematics

DWScript provides a rich set of mathematical tools, from basic arithmetic to advanced 3D vectors.

Basic Arithmetic

Most basic math functions are available globally. These include Abs, Sqr, Sqrt, Round, and Trunc.

var x := -10.5;
PrintLn(Abs(x).ToString);   // 10.5
PrintLn(Sqr(4).ToString);   // 16
PrintLn(Sqrt(16).ToString); // 4
PrintLn(Round(x).ToString); // -10
Result
10.5
16
4
-10

Trigonometry

Trigonometric functions like Sin, Cos, and Tan expect angles in radians. Use DegToRad and RadToDeg for conversions.

var angle := 45.0;
var rad := DegToRad(angle);

PrintLn('Sin(45): ' + Sin(rad).ToString);
Result
Sin(45): 0.707106781186547

Random Numbers

For general-purpose simulation and games, use the Random and RandomInt functions. For security-related tasks (tokens, keys), use the Crypto library instead.

// Random float between 0 and 1
var f := Random;

// Random integer between 0 and 99
var i := RandomInt(100);

Related Reference

For a complete list of all available math functions, constants, and advanced libraries, see the reference documentation:

On this page