Overview

Math Functions

The core math library provides standard mathematical functions, constants, and utilities for common calculations.

Comprehensive Function List

Function Parameters Description
Abs v Absolute value (supports Int, Float, Variant).
Sqr v Square (v * v).
Sqrt v Square root.
Exp v Exponential (e^v).
Ln v Natural logarithm.
Log2 v Base-2 logarithm.
Log10 v Base-10 logarithm.
LogN n, x Logarithm of x to base n.
Power base, exp Exponentiation (float exponent).
IntPower base, exp Exponentiation (integer exponent).
Hypot x, y Hypotenuse (sqrt(xx + yy)).
Haversine lat1, lon1, lat2, lon2, r Great-circle distance between two points.
Factorial v Returns v!.
GCD a, b Greatest Common Divisor.
LCM a, b Least Common Multiple.
IsPrime n Primality test.
LeastFactor n Smallest prime factor.
Floor v Largest integer less than or equal to v.
Ceil v Smallest integer greater than or equal to v.
Round v Rounds to nearest integer.
Trunc v Truncates fractional part.
Frac v Returns fractional part.
Sign v Returns -1, 0, or 1.
Clamp v, min, max Clamps float to range.
ClampInt v, min, max Clamps integer to range.
Min / Max v1, v2 Returns smaller or larger of two values.
MinInt / MaxInt v1, v2 Returns smaller or larger of two integers.
MaxInt Returns the largest possible 64-bit integer.
Odd i True if integer is odd.
PopCount i Number of set bits (Population Count).
TestBit i, bit True if specific bit is set.
CompareNum a, b Returns -1, 0, or 1 (method .Compare).
DivMod a, b, var res, var rem Performs division and modulo.
Unsigned32 v Casts to unsigned 32-bit integer.
IsNaN v True if value is Not-a-Number.
IsInfinite v True if value is Infinity.
IsFinite v True if value is a finite number.
Pi Returns the mathematical constant ΓΏ.
Infinity Returns positive infinity.
NaN Returns "Not-a-Number".
Sin / Cos / Tan a Sine, Cosine, Tangent (radians).
ArcSin / ArcCos / ArcTan v Inverse trigonometric functions.
DegToRad / RadToDeg a Angle conversions.
Random Random float in [0..1).
RandomInt range Random integer in [0..range-1].
Randomize Seeds the random generator with time.
SetRandSeed seed Manually sets the random seed.
RandG mean, stdDev Gaussian (Normal) random distribution.

Trigonometry

Standard trigonometric functions work with radians. Converters are available.

var rad := DegToRad(90);
PrintLn('Sin(90): ' + Sin(rad).ToString);
Result
Sin(90): 1

Randomization

DWScript provides a pseudo-random number generator.

var f := Random; // 0.0 <= f < 1.0
var i := RandomInt(10); // 0, 1, ..., 9

Advanced Math

Related Reference

  • 3D Math - Vectors, Quaternions and Matrices.
On this page