Skip to content
lsl.devlsl.devLSL Dev

Math

LSL has integers and floats, plus vector and rotation types for 3D work. Rotations are quaternions; you rarely need their components, because conversion functions translate them to and from Euler angles, axes and angle-axis form.

These building blocks cover projectile trajectories, look-at rotations, simple inverse kinematics with trigonometry, noise and procedural patterns, and numeric integration for custom physics. Applying the results to objects is covered by Movement and Physics.

Concepts

Functions and events

integerllAbs(integer val)
floatllFabs(float val)
integerllRound(float val)
floatllPow(float base, float exponent)
floatllSqrt(float val)
floatllSin(float theta)
floatllAtan2(float y, float x)
floatllVecMag(vector vec)
vectorllVecNorm(vector vec)
floatllVecDist(vector vec_a, vector vec_b)
rotationllEuler2Rot(vector vec)
vectorllRot2Euler(rotation q)
rotationllRotBetween(vector start_vec, vector end_vec)
floatllFrand(float mag)

Rotations

Quaternions combine by multiplication; convert them with the Euler, axis and angle-axis functions.

More on the Second Life Wiki.

Math on the wiki

The wiki’s category of math functions.

Scripting Details

integerllAbs(integer val)
Returns the absolute (positive) integer value of val.
floatllFabs(float val)
Returns the absolute (positive) value of val.
integerllRound(float val)
Returns val rounded to the nearest integer. Halfway values are rounded toward infinity.
integerllFloor(float val)
Returns val rounded toward negative infinity. In other words, returns the largest integer less than or equal to val.
integerllCeil(float val)
Returns val rounded toward positive infinity. In other words, returns the smallest integer greater than or equal to val.
floatllPow(float base, float exponent)
Returns base raised to the power exponent. If result is imaginary, returns NaN.
floatllSqrt(float val)
Returns the square root of val. If negative, return NaN.
floatllLog(float val)
Returns natural (base e) logarithm of val. If negative, return 0.0.
floatllLog10(float val)
Returns base-10 (common) logarithm of val. If negative, return 0.0.
integerllModPow(integer base, integer exponent, integer modulus)
Returns base raised to the power exponent, modulo modulus (i.e., (b^e)%m). All inputs are wrapped to unsigned 32-bit integer range [0..4294967295]. Output is wrapped to signed 32-bit integer range [-2147483648..2147483647]. Will never overflow, unlike (b^e), which can overflow to inf.
floatllListStatistics(integer operation, list src)
Returns the numeric result of the statistical aggregate function operation (a LIST_STAT_* constant) on the numeric list src.
floatllSin(float theta)
Returns the sine of theta. Theta is in radians.
floatllCos(float theta)
Returns the cosine of theta. Theta is in radians.
floatllTan(float theta)
Returns the tangent of theta. Theta is in radians.
floatllAsin(float val)
Returns the arcsine of val in radians.
floatllAcos(float val)
Returns the arccosine of val in radians.
floatllAtan2(float y, float x)
Returns the arctangent of y/x in radians, using the signs to determine the quadrant. Note the argument order: Y is the first parameter, X is the second parameter.
floatPI = 3.14159265
Mathematical constant pi, representing the number of radians in a half circle (semi-circle). When used in sensor functions, it specifies a full sphere scan.
floatTWO_PI = 6.28318530
Mathematical constant pi*2, representing the number of radians in a full circle.
floatPI_BY_TWO = 1.57079633
Mathematical constant pi/2, representing the number of radians in a quarter circle. When used in sensor functions, it specifies a hemisphere scan.
floatDEG_TO_RAD = 0.017453293
Constant 0.017453293 (precise value is PI/180). Multiply a value in degrees by this number to convert it to radians.
floatRAD_TO_DEG = 57.2957795
180/PI. Multiply a value in radians by this number to convert it to degrees.
floatllVecMag(vector vec)
Returns the magnitude (geometric length) of vec.
vectorllVecNorm(vector vec)
Returns the normalized unit vector pointing the same direction as vec. If <0, 0, 0>, return <0, 0, 0>.
floatllVecDist(vector vec_a, vector vec_b)
Returns a float representing the undirected, non-negative distance between vectors vec_a and vec_b.
vectorZERO_VECTOR = <0.0, 0.0, 0.0>
A vector constant representing <0.0, 0.0, 0.0>. This is the default value for vector variables.
rotationllEuler2Rot(vector vec)
Returns the quaternion representation of the Euler angles (in radians) within vec.
vectorllRot2Euler(rotation q)
Returns a vector of Euler angles (roll, pitch, yaw) of q. The angles will be in radians.
rotationllAxisAngle2Rot(vector axis, float angle)
Returns the rotation that rotates angle radians around the axis vector.
floatllRot2Angle(rotation q)
Returns the angle, in radians, that q rotates.
vectorllRot2Axis(rotation q)
Returns the unit vector axis that q rotates around.
rotationllAxes2Rot(vector fwd, vector left, vector up)
Returns the rotation defined by the coordinate axes fwd, left, and up.
vectorllRot2Fwd(rotation q)
Returns the unit vector pointing toward positive X (forward) in the coordinate space of rotation q. Equivalent to <1, 0, 0> * q.
vectorllRot2Left(rotation q)
Returns the unit vector pointing toward positive Y (left) in the coordinate space of rotation q. Equivalent to <0, 1, 0> * q.
vectorllRot2Up(rotation q)
Returns the unit vector pointing toward positive Z (up) in the coordinate space of rotation q. Equivalent to <0, 0, 1> * q.
rotationllRotBetween(vector start_vec, vector end_vec)
Returns the shortest-path quaternion that rotates start_vec onto end_vec.
floatllAngleBetween(rotation start_rot, rotation end_rot)
Returns the angle, in radians, between rotations start_rot and and end_rot.
rotationZERO_ROTATION = <0.0, 0.0, 0.0, 1.0>
A rotation constant representing an identity rotation (causes no change). This is the default value for rotation variables.
floatllFrand(float mag)
Returns a pseudo-random float in the range [0.0, mag) or (mag, 0.0] depending on the sign of mag. The value is inclusive of 0.0 but exclusive of mag.
listllListRandomize(list src, integer stride)
Returns a randomized copy of the list src by blocks of size stride. If the list length is not perfectly divisible by stride, no randomization occurs.
keyllGenerateKey()
Generates and returns a unique versioned UUID key (utilizing SHA-1 hashing). Due to being versioned, it will not return NULL_KEY; however, the exact UUID version is an implementation detail that should not be relied upon.
vectorllLinear2sRGB(vector color)
Returns a sRGB colorspace vector converted from the linear RGB colorspace argument.
vectorllsRGB2Linear(vector color)
Returns a linear RGB colorspace vector converted from the sRGB colorspace argument.

See also Page integer typePage float typeCategory Math

See also Category Math TrigPlanned Recipe: simple inverse kinematics with trigonometry

See also Page vector typeCategory VectorCategory Math 3DPlanned Recipe: projectile trajectories

See also Page rotation typeCategory QuaternionPage Guide: constraining rotationsPage Guide: interpolation and easingSL Wiki LSL Rotation

See also Planned Recipe: Perlin noise maps

See also SL Wiki LSL MathFeature PhysicsFeature MovementPage Guide: interpolation and easingPlanned Recipe: numeric integration for custom physics