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.

Vectorized Array Operations (array of Float)

DWScript provides high-performance SIMD-optimized operations for float arrays. These are available as both global functions and method helpers.

Global Functions

Function Parameters Description
ArrayDotProduct a1, a2 [, o1, o2, c] Returns the scalar dot product of two arrays (or segments).

Method Helpers (Mutating)

These methods modify the source array in-place and return the array instance to support fluent method chaining.

Method Parameters Description
.Offset v or other [, tO, sO, c] Element-wise addition (by scalar or array).
.Multiply v or other [, tO, sO, c] Element-wise multiplication (by scalar or array).
.Min v or other [, tO, sO, c] Element-wise x := Min(x, operand) (Cap).
.Max v or other [, tO, sO, c] Element-wise x := Max(x, operand) (Floor).
.OffsetScaled other, scale [, tO, sO, c] Element-wise x := x + other * scale.
.MultiplyAdd scale, offset In-place x := x * scale + offset.
.Reciprocal In-place x := 1 / x.
.DotProduct other Returns scalar dot product (convenience alias for global).
var data : array of Float := [1.0, 2.0, 3.0];
var noise : array of Float := [0.1, 0.2, 0.3];

// Chained in-place operations
data.Offset(noise).Multiply(2.0).Min(5.0);

// Using OffsetScaled for signal processing (signal + gain * scale)
var signal : array of Float := [10.0, 20.0, 30.0];
var gain : array of Float := [1.0, 1.0, 1.0];

signal.OffsetScaled(gain, 0.5);

for var v in signal do Print(v.ToString + ' ');
PrintLn('');
Result
10.5 20.5 30.5

Trigonometry

Standard trigonometric functions work with radians.

Function Parameters Description
Sin v Sine.
Cos v Cosine.
Tan v Tangent.
ASin v Arcsine.
ACos v Arccosine.
ATan v Arctangent.
DegToRad a Converts degrees to radians.
RadToDeg a Converts radians to degrees.
var rad := DegToRad(90);
PrintLn('Sin(90): ' + Sin(rad).ToString);
Result
Sin(90): 1

Randomization

DWScript provides a pseudo-random number generator.

Function Parameters Description
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.
var f := Random; // 0.0 <= f < 1.0
var i := RandomInt(10); // 0, 1, ..., 9

Advanced Math Utilities

Function Parameters Description
PopCount v Returns the number of set bits.
TestBit v, index Returns true if the bit at index is set.
CompareNum a, b Returns -1, 0, or 1 based on comparison.
DivMod a, b, res, rem Performs integer division and modulo in one operation.
IsNaN v Returns true if v is Not-a-Number.
IsFinite v Returns true if v is a finite number.
// Bit manipulation
var val := 0b1011; // 11
PrintLn('PopCount(11): ' + PopCount(val).ToString);
PrintLn('TestBit(11, 1): ' + TestBit(val, 1).ToString);

// Comparison and clamping
PrintLn('CompareNum(10, 20): ' + CompareNum(10, 20).ToString);
PrintLn('ClampInt(50, 0, 10): ' + ClampInt(50, 0, 10).ToString);

// Division and Modulo in one go
var res, rem : Integer;
DivMod(10, 3, res, rem);
PrintLn('10 / 3 = ' + res.ToString + ' rem ' + rem.ToString);

// Float analysis
var n := NaN;
PrintLn('IsNaN(NaN): ' + IsNaN(n).ToString);
PrintLn('IsFinite(1.0): ' + IsFinite(1.0).ToString);
Result
PopCount(11): 3
TestBit(11, 1): True
CompareNum(10, 20): -1
ClampInt(50, 0, 10): 10
10 / 3 = 3 rem 1
IsNaN(NaN): True
IsFinite(1.0): True

Related Reference

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