# Math

> Numbers, angles, vectors, rotations, random numbers and interpolation.

LSL has [integers](/types/integer/) and [floats](/types/float/), plus [vector](/types/vector/) and [rotation](/types/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](/features/movement/) and [Physics](/features/physics/).

## Concepts

- Basic math: [absolute values](/functions/llAbs/), [rounding](/functions/llRound/), [powers](/functions/llPow/), [roots](/functions/llSqrt/) and [logarithms](/functions/llLog/)
  - Integer and float operators follow the usual rules; dividing integers truncates
  - [Statistics over a list](/functions/llListStatistics/) of numbers in one call
- Angles are in radians
  - [`DEG_TO_RAD`](/constants/DEG_TO_RAD/) and [`RAD_TO_DEG`](/constants/RAD_TO_DEG/) convert
  - [`PI`](/constants/PI/), [`TWO_PI`](/constants/TWO_PI/) and [`PI_BY_TWO`](/constants/PI_BY_TWO/) are constants
- Vectors hold positions, velocities, scales and colors
  - [Length](/functions/llVecMag/), [normalisation](/functions/llVecNorm/) and [distance](/functions/llVecDist/) functions, plus the dot (`*`) and cross (`%`) operators, cover most vector math
- Rotations (quaternions)
  - Multiply rotations to combine them, and multiply a vector by a rotation to rotate it
  - Convert to and from [Euler angles](/functions/llEuler2Rot/), [axes](/functions/llAxes2Rot/) and [angle-axis](/functions/llAxisAngle2Rot/), and find the [rotation between two directions](/functions/llRotBetween/)
- Random numbers
  - [`llFrand`](/functions/llFrand/) returns a random float up to a limit
  - [Shuffle lists](/functions/llListRandomize/), and make [unique identifiers](/functions/llGenerateKey/)
- Interpolation and color
  - Linear interpolation is `a + (b - a) * t` for floats, vectors and colors
  - Convert colors between [sRGB](/functions/llLinear2sRGB/) and [linear](/functions/llsRGB2Linear/) space before blending them

## Functions and events

- `integer` [`llAbs`](/functions/llAbs/) `(integer val)`
- `float` [`llFabs`](/functions/llFabs/) `(float val)`
- `integer` [`llRound`](/functions/llRound/) `(float val)`
- `float` [`llPow`](/functions/llPow/) `(float base, float exponent)`
- `float` [`llSqrt`](/functions/llSqrt/) `(float val)`
- `float` [`llSin`](/functions/llSin/) `(float theta)`
- `float` [`llAtan2`](/functions/llAtan2/) `(float y, float x)`
- `float` [`llVecMag`](/functions/llVecMag/) `(vector vec)`
- `vector` [`llVecNorm`](/functions/llVecNorm/) `(vector vec)`
- `float` [`llVecDist`](/functions/llVecDist/) `(vector vec_a, vector vec_b)`
- `rotation` [`llEuler2Rot`](/functions/llEuler2Rot/) `(vector vec)`
- `vector` [`llRot2Euler`](/functions/llRot2Euler/) `(rotation q)`
- `rotation` [`llRotBetween`](/functions/llRotBetween/) `(vector start_vec, vector end_vec)`
- `float` [`llFrand`](/functions/llFrand/) `(float mag)`

## [Rotations](#rotations)

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

More on the [Second Life Wiki](https://wiki.secondlife.com/wiki/Category:LSL_Rotation).

## [Math on the wiki](https://wiki.secondlife.com/wiki/Category:LSL_Math)

The wiki's category of math functions.

## Scripting Details

### Basic math

- `integer` [`llAbs`](/functions/llAbs/) `(integer val)`: Returns the absolute (positive) integer value of val.
- `float` [`llFabs`](/functions/llFabs/) `(float val)`: Returns the absolute (positive) value of val.
- `integer` [`llRound`](/functions/llRound/) `(float val)`: Returns val rounded to the nearest integer. Halfway values are rounded toward infinity.
- `integer` [`llFloor`](/functions/llFloor/) `(float val)`: Returns val rounded toward negative infinity. In other words, returns the largest integer less than or equal to val.
- `integer` [`llCeil`](/functions/llCeil/) `(float val)`: Returns val rounded toward positive infinity. In other words, returns the smallest integer greater than or equal to val.
- `float` [`llPow`](/functions/llPow/) `(float base, float exponent)`: Returns base raised to the power exponent. If result is imaginary, returns NaN.
- `float` [`llSqrt`](/functions/llSqrt/) `(float val)`: Returns the square root of val. If negative, return NaN.
- `float` [`llLog`](/functions/llLog/) `(float val)`: Returns natural (base e) logarithm of val. If negative, return 0.0.
- `float` [`llLog10`](/functions/llLog10/) `(float val)`: Returns base-10 (common) logarithm of val. If negative, return 0.0.
- `integer` [`llModPow`](/functions/llModPow/) `(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.
- `float` [`llListStatistics`](/functions/llListStatistics/) `(integer operation, list src)`: Returns the numeric result of the statistical aggregate function operation (a LIST_STAT_* constant) on the numeric list src.

### Angles and trigonometry

- `float` [`llSin`](/functions/llSin/) `(float theta)`: Returns the sine of theta. Theta is in radians.
- `float` [`llCos`](/functions/llCos/) `(float theta)`: Returns the cosine of theta. Theta is in radians.
- `float` [`llTan`](/functions/llTan/) `(float theta)`: Returns the tangent of theta. Theta is in radians.
- `float` [`llAsin`](/functions/llAsin/) `(float val)`: Returns the arcsine of val in radians.
- `float` [`llAcos`](/functions/llAcos/) `(float val)`: Returns the arccosine of val in radians.
- `float` [`llAtan2`](/functions/llAtan2/) `(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.
- `float` [`PI`](/constants/PI/) `= 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.
- `float` [`TWO_PI`](/constants/TWO_PI/) `= 6.28318530`: Mathematical constant pi*2, representing the number of radians in a full circle.
- `float` [`PI_BY_TWO`](/constants/PI_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.
- `float` [`DEG_TO_RAD`](/constants/DEG_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.
- `float` [`RAD_TO_DEG`](/constants/RAD_TO_DEG/) `= 57.2957795`: 180/PI. Multiply a value in radians by this number to convert it to degrees.

### Vectors

- `float` [`llVecMag`](/functions/llVecMag/) `(vector vec)`: Returns the magnitude (geometric length) of vec.
- `vector` [`llVecNorm`](/functions/llVecNorm/) `(vector vec)`: Returns the normalized unit vector pointing the same direction as vec. If <0, 0, 0>, return <0, 0, 0>.
- `float` [`llVecDist`](/functions/llVecDist/) `(vector vec_a, vector vec_b)`: Returns a float representing the undirected, non-negative distance between vectors vec_a and vec_b.
- `vector` [`ZERO_VECTOR`](/constants/ZERO_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.

### Rotations

- `rotation` [`llEuler2Rot`](/functions/llEuler2Rot/) `(vector vec)`: Returns the quaternion representation of the Euler angles (in radians) within vec.
- `vector` [`llRot2Euler`](/functions/llRot2Euler/) `(rotation q)`: Returns a vector of Euler angles (roll, pitch, yaw) of q. The angles will be in radians.
- `rotation` [`llAxisAngle2Rot`](/functions/llAxisAngle2Rot/) `(vector axis, float angle)`: Returns the rotation that rotates angle radians around the axis vector.
- `float` [`llRot2Angle`](/functions/llRot2Angle/) `(rotation q)`: Returns the angle, in radians, that q rotates.
- `vector` [`llRot2Axis`](/functions/llRot2Axis/) `(rotation q)`: Returns the unit vector axis that q rotates around.
- `rotation` [`llAxes2Rot`](/functions/llAxes2Rot/) `(vector fwd, vector left, vector up)`: Returns the rotation defined by the coordinate axes fwd, left, and up.
- `vector` [`llRot2Fwd`](/functions/llRot2Fwd/) `(rotation q)`: Returns the unit vector pointing toward positive X (forward) in the coordinate space of rotation q. Equivalent to <1, 0, 0> * q.
- `vector` [`llRot2Left`](/functions/llRot2Left/) `(rotation q)`: Returns the unit vector pointing toward positive Y (left) in the coordinate space of rotation q. Equivalent to <0, 1, 0> * q.
- `vector` [`llRot2Up`](/functions/llRot2Up/) `(rotation q)`: Returns the unit vector pointing toward positive Z (up) in the coordinate space of rotation q. Equivalent to <0, 0, 1> * q.
- `rotation` [`llRotBetween`](/functions/llRotBetween/) `(vector start_vec, vector end_vec)`: Returns the shortest-path quaternion that rotates start_vec onto end_vec.
- `float` [`llAngleBetween`](/functions/llAngleBetween/) `(rotation start_rot, rotation end_rot)`: Returns the angle, in radians, between rotations start_rot and and end_rot.
- `rotation` [`ZERO_ROTATION`](/constants/ZERO_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.

### Random numbers

- `float` [`llFrand`](/functions/llFrand/) `(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.
- `list` [`llListRandomize`](/functions/llListRandomize/) `(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.
- `key` [`llGenerateKey`](/functions/llGenerateKey/) `()`: 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.

### Color

- `vector` [`llLinear2sRGB`](/functions/llLinear2sRGB/) `(vector color)`: Returns a sRGB colorspace vector converted from the linear RGB colorspace argument.
- `vector` [`llsRGB2Linear`](/functions/llsRGB2Linear/) `(vector color)`: Returns a linear RGB colorspace vector converted from the sRGB colorspace argument.

## Related

See also: [integer type](/types/integer/), [float type](/types/float/), [Math](/categories/math/)

See also: [Math Trig](/categories/math_trig/)
   (planned: Recipe: simple inverse kinematics with trigonometry)

See also: [vector type](/types/vector/), [Vector](/categories/vector/), [Math 3D](/categories/math_3d/)
   (planned: Recipe: projectile trajectories)

See also: [rotation type](/types/rotation/), [Quaternion](/categories/quaternion/), [Guide: constraining rotations](/guides/constraining-rotations/), [Guide: interpolation and easing](/guides/interpolation/), [LSL Rotation (Second Life Wiki)](https://wiki.secondlife.com/wiki/Category:LSL_Rotation)

See also:
   (planned: Recipe: Perlin noise maps)

See also: [LSL Math (Second Life Wiki)](https://wiki.secondlife.com/wiki/Category:LSL_Math), [Physics](/features/physics/), [Movement](/features/movement/), [Guide: interpolation and easing](/guides/interpolation/)
   (planned: Recipe: numeric integration for custom physics)

## Related features

- [Movement](/features/movement/)
- [Physics](/features/physics/)

## Categories

- [Math](/categories/math/)
- [Math 3D](/categories/math_3d/)
- [Math Trig](/categories/math_trig/)
- [Quaternion](/categories/quaternion/)
- [Vector](/categories/vector/)

---

From lsl.dev: https://lsl.dev/features/math/
