2.2. Math library
The MATH module contains floating point math functions and constants
(trigonometry, exponentials, clamping, interpolation, noise, and vector/matrix operations).
Floating point math in general is not bit-precise: the compiler may optimize
permutations, replace divisions with multiplications, and some functions are
not bit-exact. Use double precision types when exact results are required.
All functions and symbols are in “math” module, use require to get access to it.
require math
Example:
require math
[export]
def main() {
print("sin(PI/2) = {sin(PI / 2.0)}\n")
print("cos(0) = {cos(0.0)}\n")
print("sqrt(16) = {sqrt(16.0)}\n")
print("abs(-5) = {abs(-5)}\n")
print("clamp(15, 0, 10) = {clamp(15, 0, 10)}\n")
print("min(3, 7) = {min(3, 7)}\n")
print("max(3, 7) = {max(3, 7)}\n")
let v = float3(1, 0, 0)
print("length = {length(v)}\n")
}
// output:
// sin(PI/2) = 1
// cos(0) = 1
// sqrt(16) = 4
// abs(-5) = 5
// clamp(15, 0, 10) = 10
// min(3, 7) = 3
// max(3, 7) = 7
// length = 1
2.2.1. Constants
- PI = 3.1415927f
The single-precision float constant pi (3.14159265…), representing the ratio of a circle’s circumference to its diameter.
- DBL_PI = 3.141592653589793lf
The double-precision constant pi (3.141592653589793…), representing the ratio of a circle’s circumference to its diameter.
- FLT_EPSILON = 1.1920929e-07f
The smallest single-precision float value epsilon such that 1.0f + epsilon != 1.0f, approximately 1.1920929e-7.
- DBL_EPSILON = 2.220446049250313e-16lf
The smallest double-precision value epsilon such that 1.0 + epsilon != 1.0, approximately 2.2204460492503131e-16.
2.2.2. Handled structures
- math::float4x4
floating point matrix with 4 rows and 4 columns
- Fields
x : float4 - 0th row
y : float4 - 1st row
z : float4 - 2nd row
w : float4 - 3rd row
- math::float3x4
floating point matrix with 4 rows and 3 columns
- Fields
x : float3 - 0th row
y : float3 - 1st row
z : float3 - 2nd row
w : float3 - 3rd row
- math::float3x3
floating point matrix with 3 rows and 3 columns
- Fields
x : float3 - 0th row
y : float3 - 1st row
z : float3 - 2nd row
2.2.3. all numerics (uint*, int*, float*, double)
2.2.3.1. max
- math::max(x: int; y: int) : int()
Returns the component-wise maximum of two values, supporting scalar double, float, int, int64, uint, uint64 and vector float2, float3, float4 types.
- Arguments
x : int
y : int
- math::max(x: uint64; y: uint64) : uint64()
- math::max(x: int2; y: int2) : int2()
- math::max(x: double; y: double) : double()
- math::max(x: int4; y: int4) : int4()
- math::max(x: int3; y: int3) : int3()
- math::max(x: float3; y: float3) : float3()
- math::max(x: int64; y: int64) : int64()
- math::max(x: uint2; y: uint2) : uint2()
- math::max(x: uint; y: uint) : uint()
- math::max(x: uint3; y: uint3) : uint3()
- math::max(x: float4; y: float4) : float4()
- math::max(x: uint4; y: uint4) : uint4()
- math::max(x: float2; y: float2) : float2()
- math::max(x: float; y: float) : float()
2.2.3.2. min
- math::min(x: uint3; y: uint3) : uint3()
Returns the component-wise minimum of two values, supporting scalar double, float, int, int64, uint, uint64 and vector float2, float3, float4 types.
- Arguments
x : uint3
y : uint3
- math::min(x: uint2; y: uint2) : uint2()
- math::min(x: int4; y: int4) : int4()
- math::min(x: int; y: int) : int()
- math::min(x: float3; y: float3) : float3()
- math::min(x: float4; y: float4) : float4()
- math::min(x: float2; y: float2) : float2()
- math::min(x: float; y: float) : float()
- math::min(x: int2; y: int2) : int2()
- math::min(x: int3; y: int3) : int3()
- math::min(x: uint; y: uint) : uint()
- math::min(x: uint64; y: uint64) : uint64()
- math::min(x: uint4; y: uint4) : uint4()
- math::min(x: double; y: double) : double()
- math::min(x: int64; y: int64) : int64()
2.2.4. float* and double
2.2.4.1. abs
- math::abs(x: float3) : float3()
Returns the absolute value of the argument, computed component-wise for float2, float3, float4, int2, int3, and int4 vector types, and per-element for scalar float, double, int, and int64 types.
- Arguments
x : float3
- math::abs(x: uint) : uint()
- math::abs(x: uint64) : uint64()
- math::abs(x: int2) : int2()
- math::abs(x: int4) : int4()
- math::abs(x: int3) : int3()
- math::abs(x: int64) : int64()
- math::abs(x: uint2) : uint2()
- math::abs(x: int) : int()
- math::abs(x: uint4) : uint4()
- math::abs(x: uint3) : uint3()
- math::abs(x: double) : double()
- math::abs(x: float) : float()
- math::abs(x: float4) : float4()
- math::abs(x: float2) : float2()
2.2.4.2. acos
- math::acos(x: float4) : float4()
Returns the arccosine of x in radians; the input must be in the range [-1, 1] and the result is in the range [0, pi]; works with float and double.
- Arguments
x : float4
- math::acos(x: float2) : float2()
- math::acos(x: float) : float()
- math::acos(x: float3) : float3()
- math::acos(x: double) : double()
2.2.4.3. asin
- math::asin(x: float) : float()
Returns the arcsine of x in radians; the input must be in the range [-1, 1] and the result is in the range [-pi/2, pi/2]; works with float and double.
- Arguments
x : float
- math::asin(x: double) : double()
- math::asin(x: float3) : float3()
- math::asin(x: float4) : float4()
- math::asin(x: float2) : float2()
2.2.4.4. atan
- math::atan(x: float2) : float2()
Returns the arctangent of x in radians, with the result in the range [-pi/2, pi/2]; works with float and double.
- Arguments
x : float2
- math::atan(x: float4) : float4()
- math::atan(x: double) : double()
- math::atan(x: float3) : float3()
- math::atan(x: float) : float()
2.2.4.5. atan2
- math::atan2(y: float3; x: float3) : float3()
Returns the arctangent of y/x in radians, using the signs of both arguments to determine the correct quadrant; the result is in the range [-pi, pi]; works with float and double.
- Arguments
y : float3
x : float3
- math::atan2(y: float2; x: float2) : float2()
- math::atan2(y: float; x: float) : float()
- math::atan2(y: float4; x: float4) : float4()
- math::atan2(y: double; x: double) : double()
2.2.4.6. ceil
- math::ceil(x: float4) : float4()
Returns the smallest integral value not less than x (rounds toward positive infinity), computed component-wise for float2, float3, and float4 vector types; works with float and double scalars.
- Arguments
x : float4
- math::ceil(x: float3) : float3()
- math::ceil(x: float2) : float2()
- math::ceil(x: float) : float()
2.2.4.7. cos
- math::cos(x: double) : double()
Returns the cosine of x, where x is specified in radians; works with float and double.
- Arguments
x : double
- math::cos(x: float4) : float4()
- math::cos(x: float3) : float3()
- math::cos(x: float) : float()
- math::cos(x: float2) : float2()
2.2.4.8. exp
- math::exp(x: float4) : float4()
Returns e raised to the power of x (the base-e exponential), computed component-wise for float2, float3, and float4 vector types; works with float and double scalars.
- Arguments
x : float4
- math::exp(x: double) : double()
- math::exp(x: float2) : float2()
- math::exp(x: float) : float()
- math::exp(x: float3) : float3()
2.2.4.9. exp2
- math::exp2(x: float4) : float4()
Returns 2 raised to the power of x, computed component-wise for float2, float3, and float4 vector types; works with float and double scalars.
- Arguments
x : float4
- math::exp2(x: float3) : float3()
- math::exp2(x: float2) : float2()
- math::exp2(x: double) : double()
- math::exp2(x: float) : float()
2.2.4.10. floor
- math::floor(x: float) : float()
Returns the largest integral value not greater than x (rounds toward negative infinity), computed component-wise for float2, float3, and float4 vector types; works with float and double scalars.
- Arguments
x : float
- math::floor(x: float2) : float2()
- math::floor(x: float4) : float4()
- math::floor(x: float3) : float3()
2.2.4.11. is_finite
- math::is_finite(x: float) : bool()
Returns true if x is a finite value (not infinity and not NaN), checked component-wise for float2, float3, and float4 vector types; works with float and double scalars.
- Arguments
x : float
- math::is_finite(x: double) : bool()
2.2.4.12. is_nan
- math::is_nan(x: float) : bool()
Returns true if x is NaN (Not a Number), checked component-wise for float2, float3, and float4 vector types; works with float and double scalars.
- Arguments
x : float
- math::is_nan(x: double) : bool()
2.2.4.13. log
- math::log(x: float4) : float4()
Returns the natural (base-e) logarithm of x; the input must be positive; computed component-wise for float2, float3, and float4 vector types; works with float and double scalars.
- Arguments
x : float4
- math::log(x: float3) : float3()
- math::log(x: float2) : float2()
- math::log(x: double) : double()
- math::log(x: float) : float()
2.2.4.14. log2
- math::log2(x: double) : double()
Returns the base-2 logarithm of x; the input must be positive; computed component-wise for float2, float3, and float4 vector types; works with float and double scalars.
- Arguments
x : double
- math::log2(x: float4) : float4()
- math::log2(x: float3) : float3()
- math::log2(x: float2) : float2()
- math::log2(x: float) : float()
2.2.4.15. pow
- math::pow(x: double; y: double) : double()
Returns x raised to the power of y for scalar double, float, or vector float2, float3, float4 types; domain requires x >= 0 for non-integer y values.
- Arguments
x : double
y : double
- math::pow(x: float4; y: float4) : float4()
- math::pow(x: float3; y: float3) : float3()
- math::pow(x: float; y: float) : float()
- math::pow(x: float2; y: float2) : float2()
2.2.4.16. rcp
- math::rcp(x: double) : double()
Returns the reciprocal (1/x) of a scalar float or each component of a float2, float3, or float4 vector.
- Arguments
x : double
- math::rcp(x: float4) : float4()
- math::rcp(x: float3) : float3()
- math::rcp(x: float2) : float2()
- math::rcp(x: float) : float()
2.2.4.17. safe_acos
- math::safe_acos(x: double) : double()
Returns the arccosine of x in radians, clamping the input to the valid domain [-1, 1] to prevent NaN results from out-of-range values.
- Arguments
x : double
- math::safe_acos(x: float2) : float2()
- math::safe_acos(x: float3) : float3()
- math::safe_acos(x: float) : float()
- math::safe_acos(x: float4) : float4()
2.2.4.18. safe_asin
- math::safe_asin(x: float3) : float3()
Returns the arcsine of x in radians, clamping the input to the valid domain [-1, 1] to prevent NaN results from out-of-range values.
- Arguments
x : float3
- math::safe_asin(x: float4) : float4()
- math::safe_asin(x: double) : double()
- math::safe_asin(x: float2) : float2()
- math::safe_asin(x: float) : float()
2.2.4.19. saturate
- math::saturate(x: float4) : float4()
Clamps the scalar double, float, or each component of a float2, float3, float4 vector to the [0, 1] range, returning 0 for values below 0 and 1 for values above 1.
- Arguments
x : float4
- math::saturate(x: float3) : float3()
- math::saturate(x: float2) : float2()
- math::saturate(x: float) : float()
2.2.4.20. sign
- math::sign(x: uint) : uint()
Returns the sign of x component-wise: -1 for negative, 0 for zero, or 1 for positive. For unsigned types, the result is 0 or 1.
- Arguments
x : uint
- math::sign(x: uint2) : uint2()
- math::sign(x: int4) : int4()
- math::sign(x: float4) : float4()
- math::sign(x: int64) : int64()
- math::sign(x: double) : double()
- math::sign(x: float3) : float3()
- math::sign(x: uint4) : uint4()
- math::sign(x: float) : float()
- math::sign(x: float2) : float2()
- math::sign(x: uint64) : uint64()
- math::sign(x: int3) : int3()
- math::sign(x: int) : int()
- math::sign(x: int2) : int2()
- math::sign(x: uint3) : uint3()
2.2.4.21. sin
- math::sin(x: double) : double()
Returns the sine of the angle x given in radians for double or float, with output in the range [-1, 1].
- Arguments
x : double
- math::sin(x: float4) : float4()
- math::sin(x: float3) : float3()
- math::sin(x: float2) : float2()
- math::sin(x: float) : float()
2.2.4.22. sincos
- math::sincos(x: float; s: float&; c: float&)
Computes both the sine and cosine of the angle x in radians simultaneously, writing the results to output parameters s and c, for float or double types.
- Arguments
x : float
s : float& implicit
c : float& implicit
- math::sincos(x: double; s: double&; c: double&)
2.2.4.23. sqrt
- math::sqrt(x: double) : double()
Returns the square root of a scalar double, float, or each component of a float2, float3, or float4 vector; input must be non-negative.
- Arguments
x : double
- math::sqrt(x: float4) : float4()
- math::sqrt(x: float3) : float3()
- math::sqrt(x: float2) : float2()
- math::sqrt(x: float) : float()
2.2.4.24. tan
- math::tan(x: double) : double()
Returns the tangent of the angle x given in radians for double or float; undefined at odd multiples of pi/2.
- Arguments
x : double
- math::tan(x: float4) : float4()
- math::tan(x: float2) : float2()
- math::tan(x: float) : float()
- math::tan(x: float3) : float3()
2.2.5. float* only
2.2.5.1. atan2_est
- math::atan2_est(y: float; x: float) : float()
Returns a fast estimated arctangent of y/x in radians, using the signs of both arguments to determine the correct quadrant; trades some precision for speed.
- Arguments
y : float
x : float
- math::atan2_est(y: float4; x: float4) : float4()
- math::atan2_est(y: float3; x: float3) : float3()
- math::atan2_est(y: float2; x: float2) : float2()
2.2.5.2. atan_est
- math::atan_est(x: float2) : float2()
Returns a fast estimated arctangent of x in radians, trading some precision for speed; the result approximates the range [-pi/2, pi/2].
- Arguments
x : float2
- math::atan_est(x: float4) : float4()
- math::atan_est(x: float) : float()
- math::atan_est(x: float3) : float3()
2.2.5.3. ceili
- math::ceili(x: float4) : int4()
Returns the smallest integer not less than x (rounds toward positive infinity), converting the float argument to an int result.
- Arguments
x : float4
- math::ceili(x: float) : int()
- math::ceili(x: double) : int()
- math::ceili(x: float3) : int3()
- math::ceili(x: float2) : int2()
- math::float3x3-(x: float3x3) : float3x3()
Returns the component-wise arithmetic negation of a matrix, flipping the sign of every element; works with float3x3, float3x4, and float4x4 matrix types.
- Arguments
x : float3x3 implicit
- math::float3x4-(x: float3x4) : float3x4()
Returns the component-wise arithmetic negation of a matrix, flipping the sign of every element; works with float3x3, float3x4, and float4x4 matrix types.
- Arguments
x : float3x4 implicit
- math::float4x4-(x: float4x4) : float4x4()
Returns the component-wise arithmetic negation of a matrix, flipping the sign of every element; works with float3x3, float3x4, and float4x4 matrix types.
- Arguments
x : float4x4 implicit
2.2.5.4. floori
- math::floori(x: float) : int()
Returns the largest integer not greater than x (rounds toward negative infinity), converting the float argument to an int result.
- Arguments
x : float
- math::floori(x: float2) : int2()
- math::floori(x: float4) : int4()
- math::floori(x: double) : int()
- math::floori(x: float3) : int3()
2.2.5.5. fract
- math::fract(x: float3) : float3()
Returns the fractional part of x (equivalent to x - floor(x)), computed component-wise for float2, float3, and float4 vector types; works with float and double scalars.
- Arguments
x : float3
- math::fract(x: float4) : float4()
- math::fract(x: float2) : float2()
- math::fract(x: float) : float()
2.2.5.6. rcp_est
- math::rcp_est(x: float4) : float4()
Returns a fast hardware estimate of the reciprocal (1/x) of a scalar float or each component of a float2, float3, or float4 vector, trading precision for speed.
- Arguments
x : float4
- math::rcp_est(x: float2) : float2()
- math::rcp_est(x: float) : float()
- math::rcp_est(x: float3) : float3()
2.2.5.7. round
- math::round(x: float4) : float4()
Rounds each component of the scalar double, float, or vector float2, float3, float4 value x to the nearest integer, with halfway cases rounded to the nearest even value.
- Arguments
x : float4
- math::round(x: float3) : float3()
- math::round(x: float2) : float2()
- math::round(x: float) : float()
2.2.5.8. roundi
- math::roundi(x: float4) : int4()
Rounds the float x to the nearest integer value and returns the result as an int.
- Arguments
x : float4
- math::roundi(x: float2) : int2()
- math::roundi(x: float3) : int3()
- math::roundi(x: double) : int()
- math::roundi(x: float) : int()
2.2.5.9. rsqrt
- math::rsqrt(x: float4) : float4()
Returns the reciprocal square root (1/sqrt(x)) of a scalar float or each component of a float2, float3, or float4 vector.
- Arguments
x : float4
- math::rsqrt(x: float2) : float2()
- math::rsqrt(x: float) : float()
- math::rsqrt(x: float3) : float3()
2.2.5.10. rsqrt_est
- math::rsqrt_est(x: float3) : float3()
Returns a fast hardware estimate of the reciprocal square root (1/sqrt(x)) of a scalar float or each component of a float2, float3, or float4 vector, trading precision for speed.
- Arguments
x : float3
- math::rsqrt_est(x: float4) : float4()
- math::rsqrt_est(x: float2) : float2()
- math::rsqrt_est(x: float) : float()
2.2.6. float3 only
- math::cross(x: float3; y: float3) : float3()
Returns the cross product of two float3 vectors, producing a float3 vector perpendicular to both inputs with magnitude equal to the area of the parallelogram they span.
- Arguments
x : float3
y : float3
2.2.6.1. distance
- math::distance(x: float2; y: float2) : float()
Returns the Euclidean distance between two vectors as a float scalar; works with float2, float3, and float4 vector types.
- Arguments
x : float2
y : float2
- math::distance(x: float4; y: float4) : float()
- math::distance(x: float3; y: float3) : float()
2.2.6.2. distance_sq
- math::distance_sq(x: float3; y: float3) : float()
Returns the squared Euclidean distance between two vectors as a float scalar, avoiding the square root for faster distance comparisons; works with float2, float3, and float4 vector types.
- Arguments
x : float3
y : float3
- math::distance_sq(x: float2; y: float2) : float()
- math::distance_sq(x: float4; y: float4) : float()
2.2.6.3. inv_distance
- math::inv_distance(x: float3; y: float3) : float()
Returns the reciprocal of the Euclidean distance between two vectors (1 / distance(x, y)) as a float; works with float2, float3, and float4 vector types.
- Arguments
x : float3
y : float3
- math::inv_distance(x: float2; y: float2) : float()
- math::inv_distance(x: float4; y: float4) : float()
2.2.6.4. inv_distance_sq
- math::inv_distance_sq(x: float4; y: float4) : float()
Returns the reciprocal of the squared Euclidean distance between two vectors (1 / distance_sq(x, y)) as a float; works with float2, float3, and float4 vector types.
- Arguments
x : float4
y : float4
- math::inv_distance_sq(x: float2; y: float2) : float()
- math::inv_distance_sq(x: float3; y: float3) : float()
2.2.6.5. reflect
- math::reflect(v: float3; n: float3) : float3()
Computes the reflection of float2 or float3 vector v off a surface with unit normal n, returning the reflected vector as v - 2*dot(v,n)*n.
- Arguments
v : float3
n : float3
- math::reflect(v: float2; n: float2) : float2()
2.2.6.6. refract
- math::refract(v: float2; n: float2; nint: float) : float2()
Computes the refraction direction of vector v through a surface with unit normal n using Snell’s law with index of refraction ratio nint. Returns a zero vector if total internal reflection occurs.
- Arguments
v : float2
n : float2
nint : float
- math::refract(v: float3; n: float3; nint: float) : float3()
2.2.7. float2, float3, float4
2.2.7.1. dot
- math::dot(x: float3; y: float3) : float()
Returns the dot product (scalar product) of two vectors as a float; works with float2, float3, and float4 vector types.
- Arguments
x : float3
y : float3
- math::dot(x: float2; y: float2) : float()
- math::dot(x: float4; y: float4) : float()
2.2.7.2. fast_normalize
- math::fast_normalize(x: float2) : float2()
Returns a unit-length vector in the same direction as x using a fast approximation; does not check for zero-length input; works with float2, float3, and float4 vector types.
- Arguments
x : float2
- math::fast_normalize(x: float4) : float4()
- math::fast_normalize(x: float3) : float3()
2.2.7.3. inv_length
- math::inv_length(x: float4) : float()
Returns the reciprocal of the length of the vector (1 / length(x)) as a float; works with float2, float3, and float4 vector types.
- Arguments
x : float4
- math::inv_length(x: float3) : float()
- math::inv_length(x: float2) : float()
2.2.7.4. inv_length_sq
- math::inv_length_sq(x: float4) : float()
Returns the reciprocal of the squared length of the vector (1 / length_sq(x)) as a float; works with float2, float3, and float4 vector types.
- Arguments
x : float4
- math::inv_length_sq(x: float2) : float()
- math::inv_length_sq(x: float3) : float()
2.2.7.5. length
- math::length(x: float3) : float()
Returns the Euclidean length (magnitude) of the vector as a float; works with float2, float3, and float4 vector types.
- Arguments
x : float3
- math::length(x: float2) : float()
- math::length(x: float4) : float()
2.2.7.6. length_sq
- math::length_sq(x: float3) : float()
Returns the squared Euclidean length of the vector as a float, equivalent to dot(x, x) and avoiding the square root for faster magnitude comparisons; works with float2, float3, and float4 vector types.
- Arguments
x : float3
- math::length_sq(x: float2) : float()
- math::length_sq(x: float4) : float()
2.2.7.7. normalize
- math::normalize(x: float2) : float2()
Returns a unit-length vector with the same direction as the input float2, float3, or float4 vector; behavior is undefined if the input vector has zero length.
- Arguments
x : float2
- math::normalize(x: float3) : float3()
- math::normalize(x: float4) : float4()
2.2.8. Noise functions
- math::uint32_hash(seed: uint) : uint()
Returns a well-distributed uint hash of the input uint seed using an improved integer hash function suitable for hash tables and procedural generation.
- Arguments
seed : uint
- math::uint_noise_1D(position: int; seed: uint) : uint()
Generates a deterministic uint hash value from a 1D integer position and a uint seed, suitable for repeatable procedural noise.
- Arguments
position : int
seed : uint
- math::uint_noise_2D(position: int2; seed: uint) : uint()
Generates a deterministic uint hash value from 2D integer coordinates (x, y) and a uint seed, suitable for repeatable procedural noise.
- Arguments
position : int2
seed : uint
- math::uint_noise_3D(position: int3; seed: uint) : uint()
Generates a deterministic uint hash value from 3D integer coordinates (x, y, z) and a uint seed, suitable for repeatable procedural noise.
- Arguments
position : int3
seed : uint
2.2.9. lerp/mad/clamp
2.2.9.1. clamp
- math::clamp(t: float; a: float; b: float) : float()
Returns the value t clamped to the inclusive range [a, b], equivalent to min(max(t, a), b); works with float, double, float2, float3, float4, int, int64, uint, and uint64 types.
- Arguments
t : float
a : float
b : float
- math::clamp(t: int64; a: int64; b: int64) : int64()
- math::clamp(t: int3; a: int3; b: int3) : int3()
- math::clamp(t: uint3; a: uint3; b: uint3) : uint3()
- math::clamp(t: int; a: int; b: int) : int()
- math::clamp(t: uint64; a: uint64; b: uint64) : uint64()
- math::clamp(t: int4; a: int4; b: int4) : int4()
- math::clamp(t: int2; a: int2; b: int2) : int2()
- math::clamp(t: float4; a: float4; b: float4) : float4()
- math::clamp(t: float2; a: float2; b: float2) : float2()
- math::clamp(t: uint4; a: uint4; b: uint4) : uint4()
- math::clamp(t: float3; a: float3; b: float3) : float3()
- math::clamp(t: double; a: double; b: double) : double()
- math::clamp(t: uint; a: uint; b: uint) : uint()
- math::clamp(t: uint2; a: uint2; b: uint2) : uint2()
2.2.9.2. lerp
- math::lerp(a: double; b: double; t: double) : double()
Performs linear interpolation between a and b using the factor t, returning a + (b - a) * t; when t is 0 the result is a, when t is 1 the result is b; works component-wise with float, double, float2, float3, and float4 types.
- Arguments
a : double
b : double
t : double
- math::lerp(a: float4; b: float4; t: float4) : float4()
- math::lerp(a: float3; b: float3; t: float3) : float3()
- math::lerp(a: float2; b: float2; t: float2) : float2()
- math::lerp(a: float; b: float; t: float) : float()
- math::lerp(a: float3; b: float3; t: float) : float3()
- math::lerp(a: float4; b: float4; t: float) : float4()
- math::lerp(a: float2; b: float2; t: float) : float2()
2.2.9.3. mad
- math::mad(a: uint4; b: uint4; c: uint4) : uint4()
Computes the fused multiply-add operation a * b + c.
- Arguments
a : uint4
b : uint4
c : uint4
- math::mad(a: uint3; b: uint3; c: uint3) : uint3()
- math::mad(a: uint2; b: uint; c: uint2) : uint2()
- math::mad(a: uint; b: uint; c: uint) : uint()
- math::mad(a: int3; b: int; c: int3) : int3()
- math::mad(a: int2; b: int; c: int2) : int2()
- math::mad(a: int4; b: int; c: int4) : int4()
- math::mad(a: uint2; b: uint2; c: uint2) : uint2()
- math::mad(a: uint3; b: uint; c: uint3) : uint3()
- math::mad(a: int2; b: int2; c: int2) : int2()
- math::mad(a: float4; b: float; c: float4) : float4()
- math::mad(a: float3; b: float; c: float3) : float3()
- math::mad(a: int; b: int; c: int) : int()
- math::mad(a: int3; b: int3; c: int3) : int3()
- math::mad(a: float4; b: float4; c: float4) : float4()
- math::mad(a: double; b: double; c: double) : double()
- math::mad(a: float; b: float; c: float) : float()
- math::mad(a: float2; b: float2; c: float2) : float2()
- math::mad(a: float3; b: float3; c: float3) : float3()
- math::mad(a: float2; b: float; c: float2) : float2()
- math::mad(a: int4; b: int4; c: int4) : int4()
- math::mad(a: uint4; b: uint; c: uint4) : uint4()
2.2.10. Matrix element access
float3x3 const implicit ==const.[] (m: float3x3 const implicit ==const; i: int) : float3
float3x3 const implicit ==const.[] (m: float3x3 const implicit ==const; i: uint) : float3
float3x3 implicit ==const.[] (m: float3x3 implicit ==const; i: int) : float3&
float3x3 implicit ==const.[] (m: float3x3 implicit ==const; i: uint) : float3&
float3x4 const implicit ==const.[] (m: float3x4 const implicit ==const; i: uint) : float3
float3x4 const implicit ==const.[] (m: float3x4 const implicit ==const; i: int) : float3
float3x4 implicit ==const.[] (m: float3x4 implicit ==const; i: uint) : float3&
float3x4 implicit ==const.[] (m: float3x4 implicit ==const; i: int) : float3&
float4x4 const implicit ==const.[] (m: float4x4 const implicit ==const; i: uint) : float4
float4x4 const implicit ==const.[] (m: float4x4 const implicit ==const; i: int) : float4
float4x4 implicit ==const.[] (m: float4x4 implicit ==const; i: int) : float4&
float4x4 implicit ==const.[] (m: float4x4 implicit ==const; i: uint) : float4&
2.2.10.1. float3x3 const implicit ==const.[]
- float3x3 const implicit ==const.[](m: float3x3 const implicit ==const; i: int) : float3()
Returns a copy of the row vector at index i from a constant float3x3 matrix.
- Arguments
m : float3x3 implicit!
i : int
- float3x3 const implicit ==const.[](m: float3x3 const implicit ==const; i: uint) : float3()
2.2.10.2. float3x3 implicit ==const.[]
- float3x3 implicit ==const.[](m: float3x3 implicit ==const; i: int) : float3&()
Returns a reference to the row vector at index i from a float3x3 matrix.
- Arguments
m : float3x3 implicit!
i : int
- float3x3 implicit ==const.[](m: float3x3 implicit ==const; i: uint) : float3&()
2.2.10.3. float3x4 const implicit ==const.[]
- float3x4 const implicit ==const.[](m: float3x4 const implicit ==const; i: uint) : float3()
Returns a copy of the row vector at index i from a constant float3x4 matrix.
- Arguments
m : float3x4 implicit!
i : uint
- float3x4 const implicit ==const.[](m: float3x4 const implicit ==const; i: int) : float3()
2.2.10.4. float3x4 implicit ==const.[]
- float3x4 implicit ==const.[](m: float3x4 implicit ==const; i: uint) : float3&()
Returns a reference to the row vector at index i from a float3x4 matrix.
- Arguments
m : float3x4 implicit!
i : uint
- float3x4 implicit ==const.[](m: float3x4 implicit ==const; i: int) : float3&()
2.2.10.5. float4x4 const implicit ==const.[]
- float4x4 const implicit ==const.[](m: float4x4 const implicit ==const; i: uint) : float4()
Returns a copy of the row vector at index i from a constant float4x4 matrix.
- Arguments
m : float4x4 implicit!
i : uint
- float4x4 const implicit ==const.[](m: float4x4 const implicit ==const; i: int) : float4()
2.2.10.6. float4x4 implicit ==const.[]
- float4x4 implicit ==const.[](m: float4x4 implicit ==const; i: int) : float4&()
Returns a reference to the row vector at index i from a float4x4 matrix.
- Arguments
m : float4x4 implicit!
i : int
- float4x4 implicit ==const.[](m: float4x4 implicit ==const; i: uint) : float4&()
2.2.11. Matrix operations
- math::float3x3!=(x: float3x3; y: float3x3) : bool()
Returns true if two float3x3 matrices are not equal, comparing all elements component-wise.
2.2.11.1. float3x3*
- math::float3x3*(x: float3x3; y: float3x3) : float3x3()
Transforms a float3 vector by a 3x3 matrix.
- math::float3x3*(x: float3x3; y: float3) : float3()
- math::float3x3==(x: float3x3; y: float3x3) : bool()
Returns true if two float3x3 matrices are exactly equal, comparing all elements component-wise.
- math::float3x4!=(x: float3x4; y: float3x4) : bool()
Returns true if two float3x4 matrices are not equal, comparing all elements component-wise.
2.2.11.2. float3x4*
- math::float3x4*(x: float3x4; y: float3x4) : float3x4()
Transforms a float3 vector by a 3x3 matrix.
- math::float3x4*(x: float3x4; y: float3) : float3()
- math::float3x4==(x: float3x4; y: float3x4) : bool()
Returns true if two float3x4 matrices are exactly equal, comparing all elements component-wise.
- math::float4x4!=(x: float4x4; y: float4x4) : bool()
Returns true if two float4x4 matrices are not equal, comparing all elements component-wise.
2.2.11.3. float4x4*
- math::float4x4*(x: float4x4; y: float4) : float4()
Transforms a float4 vector by a 4x4 matrix.
- Arguments
x : float4x4 implicit
y : float4
- math::float4x4*(x: float4x4; y: float4x4) : float4x4()
- math::float4x4==(x: float4x4; y: float4x4) : bool()
Returns true if two float4x4 matrices are exactly equal, comparing all elements component-wise.
2.2.12. Matrix initializers
2.2.12.1. float3x3
- math::float3x3(arg0: float3x4) : float3x3()
Extracts the upper-left 3x3 rotation part from a float3x4 transformation matrix, returning it as a float3x3.
- Arguments
arg0 : float3x4 implicit
- math::float3x3() : float3x3()
- math::float3x3(arg0: float4x4) : float3x3()
2.2.12.2. float3x4
- math::float3x4(arg0: float4x4) : float3x4()
Constructs a float3x4 transformation matrix from a float3x3 rotation matrix, with the translation component set to zero.
- Arguments
arg0 : float4x4 implicit
- math::float3x4() : float3x4()
2.2.12.3. float4x4
- math::float4x4(arg0: float3x4) : float4x4()
Converts a float3x4 transformation matrix to a float4x4 matrix, filling the fourth row with [0, 0, 0, 1].
- Arguments
arg0 : float3x4 implicit
- math::float4x4() : float4x4()
- math::identity3x3() : float3x3()
Returns a float3x3 identity matrix with ones on the diagonal and zeros elsewhere.
- math::identity3x4() : float3x4()
Returns a float3x4 identity transformation matrix with the rotation part set to identity and the translation set to zero.
- math::identity4x4() : float4x4()
Returns a float4x4 identity matrix with ones on the diagonal and zeros elsewhere.
2.2.13. Matrix manipulation
- math::compose(pos: float3; rot: float4; scale: float3) : float4x4()
Constructs a float4x4 transformation matrix from a float3 translation position, a float4 quaternion rotation, and a float3 scale.
- Arguments
pos : float3
rot : float4
scale : float3
- math::decompose(mat: float4x4; pos: float3&; rot: float4&; scale: float3&)
Decomposes a float4x4 transformation matrix into its float3 translation position, float4 quaternion rotation, and float3 scale components., writing the results into the output arguments rot and pos.
- Arguments
mat : float4x4 implicit
pos : float3& implicit
rot : float4& implicit
scale : float3& implicit
2.2.13.1. determinant
- math::determinant(x: float3x4) : float()
Returns the determinant of a float3x4 matrix as a float scalar; a zero determinant indicates the matrix is singular and non-invertible.
- Arguments
x : float3x4 implicit
- math::determinant(x: float4x4) : float()
- math::determinant(x: float3x3) : float()
2.2.13.2. identity
- math::identity(x: float3x4)
Sets the given float3x4 matrix to the identity transformation (rotation part is the identity matrix, translation is zero) and returns it.
- Arguments
x : float3x4 implicit
- math::identity(x: float4x4)
- math::identity(x: float3x3)
2.2.13.3. inverse
- math::inverse(x: float3x4) : float3x4()
Returns the inverse of a matrix, such that multiplying the original by its inverse yields the identity; works with float3x4 and float4x4 matrix types.
- Arguments
x : float3x4 implicit
- math::inverse(m: float4x4) : float4x4()
- math::look_at(eye: float3; at: float3; up: float3) : float4x4()
Constructs a float4x4 look-at view transformation matrix from eye position, target position, and up vector. from an eye position, a target point to look at, and an up direction vector.
- Arguments
eye : float3
at : float3
up : float3
2.2.13.4. orthonormal_inverse
- math::orthonormal_inverse(m: float3x3) : float3x3()
Returns the inverse of a float3x3 orthonormal matrix (each axis is unit length and mutually perpendicular), computed more efficiently than a general matrix inverse.
- Arguments
m : float3x3 implicit
- math::orthonormal_inverse(m: float3x4) : float3x4()
- math::persp_forward(wk: float; hk: float; zn: float; zf: float) : float4x4()
Returns a forward (standard) perspective projection float4x4 matrix constructed from horizontal scale wk, vertical scale hk, near plane zn, and far plane zf.
- Arguments
wk : float
hk : float
zn : float
zf : float
- math::persp_reverse(wk: float; hk: float; zn: float; zf: float) : float4x4()
Returns a reverse-depth perspective projection float4x4 matrix constructed from horizontal scale wk, vertical scale hk, near plane zn, and far plane zf, mapping the far plane to 0 and the near plane to 1.
- Arguments
wk : float
hk : float
zn : float
zf : float
- math::rotate(x: float3x4; y: float3) : float3()
Rotates a float3 vector v by the 3x3 rotation part of the float3x4 matrix m, ignoring the translation component.
- Arguments
x : float3x4 implicit
y : float3
- math::translation(xyz: float3) : float4x4()
Constructs a float4x4 matrix representing a pure translation by the given float3 offset. by the float3 offset xyz, with the rotation part set to identity.
- Arguments
xyz : float3
- math::transpose(x: float4x4) : float4x4()
Returns the transpose of a float3x3 or float4x4 matrix, swapping rows and columns.
- Arguments
x : float4x4 implicit
2.2.14. Quaternion operations
- math::euler_from_quat(angles: float4) : float3()
Converts a float4 quaternion to Euler angles, returning a float3 representing rotation around the x, y, and z axes in radians.
- Arguments
angles : float4
2.2.14.1. quat
- math::quat(m: float4x4) : float4()
Extracts the rotation part of a float3x4 matrix and returns it as a float4 quaternion in (x, y, z, w) format.
- Arguments
m : float4x4 implicit
- math::quat(m: float3x3) : float4()
- math::quat(m: float3x4) : float4()
- math::quat_conjugate(q: float4) : float4()
Returns the conjugate of the float4 quaternion q by negating its xyz components, which for unit quaternions equals the inverse rotation.
- Arguments
q : float4
2.2.14.2. quat_from_euler
- math::quat_from_euler(x: float; y: float; z: float) : float4()
Creates a float4 quaternion from a float3 of Euler angles (pitch, yaw, roll) given in radians.
- Arguments
x : float
y : float
z : float
- math::quat_from_euler(angles: float3) : float4()
- math::quat_from_unit_arc(v0: float3; v1: float3) : float4()
Creates a float4 quaternion representing the shortest rotation arc from unit-length float3 vector v0 to unit-length float3 vector v1; both input vectors must be normalized.
- Arguments
v0 : float3
v1 : float3
- math::quat_from_unit_vec_ang(v: float3; ang: float) : float4()
Creates a float4 quaternion representing a rotation of ang radians around the unit-length float3 axis vector v.
- Arguments
v : float3
ang : float
- math::quat_mul(q1: float4; q2: float4) : float4()
Returns the float4 quaternion product of q1 and q2, representing the combined rotation of q2 followed by q1.
- Arguments
q1 : float4
q2 : float4
- math::quat_mul_vec(q: float4; v: float3) : float3()
Rotates a float3 vector v by the float4 quaternion q and returns the resulting float3 vector.
- Arguments
q : float4
v : float3
- math::quat_slerp(t: float; a: float4; b: float4) : float4()
Performs spherical linear interpolation between float4 quaternions a and b by factor t in the range [0,1], returning a smoothly interpolated float4 quaternion.
- Arguments
t : float
a : float4
b : float4
2.2.15. Packing and unpacking
- math::pack_float_to_byte(x: float4) : uint()
Packs a float4 vector into a single uint by converting each component to an 8-bit byte value, mapping the XYZW components to the four bytes of the result.
- Arguments
x : float4
- math::unpack_byte_to_float(x: uint) : float4()
Unpacks the four bytes of a uint into a float4 vector, converting each 8-bit byte value to its corresponding floating-point component.
- Arguments
x : uint