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.
Common scalar and vectorized families include:
Trigonometric and inverse trigonometric:
sin,cos,tan,asin,acos,atan,atan2Hyperbolic and inverse hyperbolic:
sinh,cosh,tanh,asinh,acosh,atanhExponential and logarithmic:
exp,exp2,expm1,log,log2,log10,log1p,powCore numeric helpers:
sqrt,cbrt,hypot,trunc,floor,ceil,round,fract
Remainder note for floating point values:
a % bandfmod(a, b)follow truncation-style remainder (sign follows dividenda).remainder(a, b)follows IEEE remainder rules and can differ from%/fmodfor the same inputs.
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")
print("tanh(1) = {tanh(1.0)}\n")
print("log10(1000) = {log10(1000.0)}\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
// tanh(1) = 0.7615942
// log10(1000) = 3
// 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¶
- 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
- 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
- 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¶
- max(x: double; y: double): double¶
Returns the component-wise maximum of two values, supporting scalar double, float, int, int64, uint, uint64 and vector float2, float3, float4 types.
- Arguments:
x : double
y : double
- max(x: float2; y: float2): float2
- max(x: float3; y: float3): float3
- max(x: float4; y: float4): float4
- max(x: float; y: float): float
- max(x: int2; y: int2): int2
- max(x: int3; y: int3): int3
- max(x: int4; y: int4): int4
- max(x: int64; y: int64): int64
- max(x: int; y: int): int
- max(x: uint2; y: uint2): uint2
- max(x: uint3; y: uint3): uint3
- max(x: uint4; y: uint4): uint4
- max(x: uint64; y: uint64): uint64
- max(x: uint; y: uint): uint
2.2.3.2. min¶
- min(x: double; y: double): double¶
Returns the component-wise minimum of two values, supporting scalar double, float, int, int64, uint, uint64 and vector float2, float3, float4 types.
- Arguments:
x : double
y : double
- min(x: float2; y: float2): float2
- min(x: float3; y: float3): float3
- min(x: float4; y: float4): float4
- min(x: float; y: float): float
- min(x: int2; y: int2): int2
- min(x: int3; y: int3): int3
- min(x: int4; y: int4): int4
- min(x: int64; y: int64): int64
- min(x: int; y: int): int
- min(x: uint2; y: uint2): uint2
- min(x: uint3; y: uint3): uint3
- min(x: uint4; y: uint4): uint4
- min(x: uint64; y: uint64): uint64
- min(x: uint; y: uint): uint
2.2.4. float* and double¶
2.2.4.1. abs¶
- abs(x: double): double¶
Returns the absolute value of x, component-wise for vectors and across numeric scalar overloads.
- Arguments:
x : double
- abs(x: float): float
- abs(x: float2): float2
- abs(x: float3): float3
- abs(x: float4): float4
- abs(x: int): int
- abs(x: int2): int2
- abs(x: int3): int3
- abs(x: int4): int4
- abs(x: int64): int64
- abs(x: uint): uint
- abs(x: uint2): uint2
- abs(x: uint3): uint3
- abs(x: uint4): uint4
- abs(x: uint64): uint64
2.2.4.2. acos¶
- acos(x: double): double¶
Returns the arccosine of x in radians, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- acos(x: float): float
- acos(x: float2): float2
- acos(x: float3): float3
- acos(x: float4): float4
2.2.4.3. acosh¶
- acosh(x: double): double¶
Returns the inverse hyperbolic cosine of x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- acosh(x: float): float
- acosh(x: float2): float2
- acosh(x: float3): float3
- acosh(x: float4): float4
2.2.4.4. asin¶
- asin(x: double): double¶
Returns the arc sine of x in radians, for double precision argument.
- Arguments:
x : double
- asin(x: float): float
- asin(x: float2): float2
- asin(x: float3): float3
- asin(x: float4): float4
2.2.4.5. asinh¶
- asinh(x: double): double¶
Returns the inverse hyperbolic sine of x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- asinh(x: float): float
- asinh(x: float2): float2
- asinh(x: float3): float3
- asinh(x: float4): float4
2.2.4.6. atan¶
- atan(x: double): double¶
Returns the arctangent of x in radians, with the result in the range [-pi/2, pi/2]; works with float and double.
- Arguments:
x : double
- atan(x: float): float
- atan(x: float2): float2
- atan(x: float3): float3
- atan(x: float4): float4
2.2.4.7. atan2¶
- atan2(y: double; x: double): double¶
Returns the arc tangent of y/x in radians, for double precision arguments, using the signs of both arguments to determine the quadrant.
- Arguments:
y : double
x : double
- atan2(y: float2; x: float2): float2
- atan2(y: float3; x: float3): float3
- atan2(y: float4; x: float4): float4
- atan2(y: float; x: float): float
2.2.4.8. atanh¶
- atanh(x: double): double¶
Returns the inverse hyperbolic tangent of x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- atanh(x: float): float
- atanh(x: float2): float2
- atanh(x: float3): float3
- atanh(x: float4): float4
2.2.4.9. cbrt¶
- cbrt(x: double): double¶
Returns the cube root of x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- cbrt(x: float): float
- cbrt(x: float2): float2
- cbrt(x: float3): float3
- cbrt(x: float4): float4
2.2.4.10. ceil¶
- ceil(x: double): double¶
Returns the smallest integer value not less than x, component-wise for vectors, and for float/double scalars.
- Arguments:
x : double
- ceil(x: float): float
- ceil(x: float2): float2
- ceil(x: float3): float3
- ceil(x: float4): float4
2.2.4.11. cos¶
- cos(x: double): double¶
Returns the cosine of x, where x is specified in radians; works with float and double.
- Arguments:
x : double
- cos(x: float): float
- cos(x: float2): float2
- cos(x: float3): float3
- cos(x: float4): float4
2.2.4.12. cosh¶
- cosh(x: double): double¶
Returns the hyperbolic cosine of x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- cosh(x: float): float
- cosh(x: float2): float2
- cosh(x: float3): float3
- cosh(x: float4): float4
2.2.4.13. exp¶
- exp(x: double): double¶
Returns e raised to x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- exp(x: float): float
- exp(x: float2): float2
- exp(x: float3): float3
- exp(x: float4): float4
2.2.4.14. exp2¶
- exp2(x: double): double¶
Returns 2 raised to x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- exp2(x: float): float
- exp2(x: float2): float2
- exp2(x: float3): float3
- exp2(x: float4): float4
2.2.4.15. expm1¶
- expm1(x: double): double¶
Returns exp(x)-1 with improved accuracy near zero, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- expm1(x: float): float
- expm1(x: float2): float2
- expm1(x: float3): float3
- expm1(x: float4): float4
2.2.4.16. floor¶
- floor(x: double): double¶
Returns the largest integer value not greater than x, component-wise for vectors, and for float/double scalars.
- Arguments:
x : double
- floor(x: float): float
- floor(x: float2): float2
- floor(x: float3): float3
- floor(x: float4): float4
2.2.4.17. fmod¶
- fmod(x: double; y: double): double¶
Returns the floating-point remainder of x/y using truncation-style quotient (same sign behavior as %), component-wise for vectors and for float/double scalars.
- Arguments:
x : double
y : double
- fmod(x: float2; y: float2): float2
- fmod(x: float3; y: float3): float3
- fmod(x: float4; y: float4): float4
- fmod(x: float; y: float): float
2.2.4.18. hypot¶
- hypot(x: double; y: double): double¶
Returns sqrt(x*x + y*y), component-wise for float2/float3/float4 vectors, and for float/double scalar pairs.
- Arguments:
x : double
y : double
- hypot(x: float2; y: float2): float2
- hypot(x: float3; y: float3): float3
- hypot(x: float4; y: float4): float4
- hypot(x: float; y: float): float
2.2.4.19. is_finite¶
- is_finite(x: double): bool¶
Returns true if the double value x is finite (not NaN and not infinity).
- Arguments:
x : double
- is_finite(x: float): bool
2.2.4.20. is_nan¶
- is_nan(x: double): 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 : double
- is_nan(x: float): bool
2.2.4.21. log¶
- log(x: double): double¶
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 : double
- log(x: float): float
- log(x: float2): float2
- log(x: float3): float3
- log(x: float4): float4
2.2.4.22. log10¶
- log10(x: double): double¶
Returns the base-10 logarithm of x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- log10(x: float): float
- log10(x: float2): float2
- log10(x: float3): float3
- log10(x: float4): float4
2.2.4.23. log1p¶
- log1p(x: double): double¶
Returns ln(1+x) with improved accuracy near zero, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- log1p(x: float): float
- log1p(x: float2): float2
- log1p(x: float3): float3
- log1p(x: float4): float4
2.2.4.24. log2¶
- 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
- log2(x: float): float
- log2(x: float2): float2
- log2(x: float3): float3
- log2(x: float4): float4
2.2.4.25. pow¶
- 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
- pow(x: float2; y: float2): float2
- pow(x: float3; y: float3): float3
- pow(x: float4; y: float4): float4
- pow(x: float; y: float): float
2.2.4.26. rcp¶
- 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
- rcp(x: float): float
- rcp(x: float2): float2
- rcp(x: float3): float3
- rcp(x: float4): float4
2.2.4.27. remainder¶
- remainder(x: double; y: double): double¶
Returns the IEEE 754 remainder of x/y (round-to-nearest quotient), component-wise for vectors and for float/double scalars.
- Arguments:
x : double
y : double
- remainder(x: float2; y: float2): float2
- remainder(x: float3; y: float3): float3
- remainder(x: float4; y: float4): float4
- remainder(x: float; y: float): float
2.2.4.28. safe_acos¶
- 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
- safe_acos(x: float): float
- safe_acos(x: float2): float2
- safe_acos(x: float3): float3
- safe_acos(x: float4): float4
2.2.4.29. safe_asin¶
- safe_asin(x: double): double¶
Returns the arc sine of x clamped to [-1,1], for double precision argument, avoiding NaN for out-of-range inputs.
- Arguments:
x : double
- safe_asin(x: float): float
- safe_asin(x: float2): float2
- safe_asin(x: float3): float3
- safe_asin(x: float4): float4
2.2.4.30. saturate¶
- saturate(x: double): double¶
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 : double
- saturate(x: float): float
- saturate(x: float2): float2
- saturate(x: float3): float3
- saturate(x: float4): float4
2.2.4.31. sign¶
- sign(x: double): double¶
Returns the sign of x component-wise: -1 for negative, 0 for zero, and 1 for positive.
- Arguments:
x : double
- sign(x: float): float
- sign(x: float2): float2
- sign(x: float3): float3
- sign(x: float4): float4
- sign(x: int): int
- sign(x: int2): int2
- sign(x: int3): int3
- sign(x: int4): int4
- sign(x: int64): int64
- sign(x: uint): uint
- sign(x: uint2): uint2
- sign(x: uint3): uint3
- sign(x: uint4): uint4
- sign(x: uint64): uint64
2.2.4.32. sin¶
- 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
- sin(x: float): float
- sin(x: float2): float2
- sin(x: float3): float3
- sin(x: float4): float4
2.2.4.33. sincos¶
- sincos(x: double; s: double&; c: double&)¶
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 : double
s : double& implicit
c : double& implicit
- sincos(x: float; s: float&; c: float&)
2.2.4.34. sinh¶
- sinh(x: double): double¶
Returns the hyperbolic sine of x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- sinh(x: float): float
- sinh(x: float2): float2
- sinh(x: float3): float3
- sinh(x: float4): float4
2.2.4.35. sqrt¶
- 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
- sqrt(x: float): float
- sqrt(x: float2): float2
- sqrt(x: float3): float3
- sqrt(x: float4): float4
2.2.4.36. tan¶
- 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
- tan(x: float): float
- tan(x: float2): float2
- tan(x: float3): float3
- tan(x: float4): float4
2.2.4.37. tanh¶
- tanh(x: double): double¶
Returns the hyperbolic tangent of x, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- tanh(x: float): float
- tanh(x: float2): float2
- tanh(x: float3): float3
- tanh(x: float4): float4
2.2.4.38. trunc¶
- trunc(x: double): double¶
Rounds x toward zero, component-wise for float2/float3/float4 vectors, and for float/double scalars.
- Arguments:
x : double
- trunc(x: float): float
- trunc(x: float2): float2
- trunc(x: float3): float3
- trunc(x: float4): float4
2.2.5. float* only¶
2.2.5.1. atan2_est¶
- atan2_est(y: float2; x: float2): float2¶
Returns an estimated arc tangent of y/x for each component of the float2 vectors.
- Arguments:
y : float2
x : float2
- atan2_est(y: float3; x: float3): float3
- atan2_est(y: float4; x: float4): float4
- atan2_est(y: float; x: float): float
2.2.5.2. atan_est¶
- atan_est(x: float): float¶
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 : float
- atan_est(x: float2): float2
- atan_est(x: float3): float3
- atan_est(x: float4): float4
2.2.5.3. ceili¶
- ceili(x: double): int¶
Returns the smallest integer not less than x (rounds toward positive infinity), converting the double argument to an int result.
- Arguments:
x : double
- ceili(x: float): int
- ceili(x: float2): int2
- ceili(x: float3): int3
- ceili(x: float4): int4
- 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
- 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
- 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¶
- floori(x: double): int¶
Returns the largest integer not greater than x, converting the double argument to an int result.
- Arguments:
x : double
- floori(x: float): int
- floori(x: float2): int2
- floori(x: float3): int3
- floori(x: float4): int4
2.2.5.5. fract¶
- fract(x: double): double¶
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 : double
- fract(x: float): float
- fract(x: float2): float2
- fract(x: float3): float3
- fract(x: float4): float4
2.2.5.6. rcp_est¶
- rcp_est(x: float): float¶
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 : float
- rcp_est(x: float2): float2
- rcp_est(x: float3): float3
- rcp_est(x: float4): float4
2.2.5.7. round¶
- round(x: double): double¶
Returns the rounded value of x to the nearest integer; works with float and double scalars.
- Arguments:
x : double
- round(x: float): float
- round(x: float2): float2
- round(x: float3): float3
- round(x: float4): float4
2.2.5.8. roundi¶
- roundi(x: double): int¶
Rounds the double x to the nearest integer value and returns the result as an int.
- Arguments:
x : double
- roundi(x: float): int
- roundi(x: float2): int2
- roundi(x: float3): int3
- roundi(x: float4): int4
2.2.5.9. rsqrt¶
- rsqrt(x: float): float¶
Returns the reciprocal square root (1/sqrt(x)) of a scalar float or each component of a float2, float3, or float4 vector.
- Arguments:
x : float
- rsqrt(x: float2): float2
- rsqrt(x: float3): float3
- rsqrt(x: float4): float4
2.2.5.10. rsqrt_est¶
- rsqrt_est(x: float): float¶
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 : float
- rsqrt_est(x: float2): float2
- rsqrt_est(x: float3): float3
- rsqrt_est(x: float4): float4
2.2.5.11. trunci¶
- trunci(x: double): int¶
Truncates the float x toward zero to the nearest integer and returns the result as an int.
- Arguments:
x : double
- trunci(x: float): int
- trunci(x: float2): int2
- trunci(x: float3): int3
- trunci(x: float4): int4
2.2.6. float3 only¶
- 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¶
- 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
- distance(x: float3; y: float3): float
- distance(x: float4; y: float4): float
2.2.6.2. distance_sq¶
- distance_sq(x: float2; y: float2): 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 : float2
y : float2
- distance_sq(x: float3; y: float3): float
- distance_sq(x: float4; y: float4): float
2.2.6.3. inv_distance¶
- inv_distance(x: float2; y: float2): 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 : float2
y : float2
- inv_distance(x: float3; y: float3): float
- inv_distance(x: float4; y: float4): float
2.2.6.4. inv_distance_sq¶
- inv_distance_sq(x: float2; y: float2): 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 : float2
y : float2
- inv_distance_sq(x: float3; y: float3): float
- inv_distance_sq(x: float4; y: float4): float
2.2.6.5. reflect¶
- reflect(v: float2; n: float2): float2¶
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 : float2
n : float2
- reflect(v: float3; n: float3): float3
2.2.6.6. refract¶
- 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
- refract(v: float3; n: float3; nint: float): float3
2.2.7. float2, float3, float4¶
2.2.7.1. dot¶
- dot(x: float2; y: float2): float¶
Returns the dot product (scalar product) of two vectors as a float; works with float2, float3, and float4 vector types.
- Arguments:
x : float2
y : float2
- dot(x: float3; y: float3): float
- dot(x: float4; y: float4): float
2.2.7.2. fast_normalize¶
- 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
- fast_normalize(x: float3): float3
- fast_normalize(x: float4): float4
2.2.7.3. inv_length¶
- inv_length(x: float2): 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 : float2
- inv_length(x: float3): float
- inv_length(x: float4): float
2.2.7.4. inv_length_sq¶
- inv_length_sq(x: float2): 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 : float2
- inv_length_sq(x: float3): float
- inv_length_sq(x: float4): float
2.2.7.5. length¶
- length(x: float2): float¶
Returns the Euclidean length (magnitude) of the vector as a float; works with float2, float3, and float4 vector types.
- Arguments:
x : float2
- length(x: float3): float
- length(x: float4): float
2.2.7.6. length_sq¶
- length_sq(x: float2): 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 : float2
- length_sq(x: float3): float
- length_sq(x: float4): float
2.2.7.7. normalize¶
- 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
- normalize(x: float3): float3
- normalize(x: float4): float4
2.2.8. Noise functions¶
- 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
- 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
- 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
- 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¶
- clamp(t: double; a: double; b: double): double¶
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 : double
a : double
b : double
- clamp(t: float2; a: float2; b: float2): float2
- clamp(t: float3; a: float3; b: float3): float3
- clamp(t: float4; a: float4; b: float4): float4
- clamp(t: float; a: float; b: float): float
- clamp(t: int2; a: int2; b: int2): int2
- clamp(t: int3; a: int3; b: int3): int3
- clamp(t: int4; a: int4; b: int4): int4
- clamp(t: int64; a: int64; b: int64): int64
- clamp(t: int; a: int; b: int): int
- clamp(t: uint2; a: uint2; b: uint2): uint2
- clamp(t: uint3; a: uint3; b: uint3): uint3
- clamp(t: uint4; a: uint4; b: uint4): uint4
- clamp(t: uint64; a: uint64; b: uint64): uint64
- clamp(t: uint; a: uint; b: uint): uint
2.2.9.2. lerp¶
- 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
- lerp(a: float2; b: float2; t: float): float2
- lerp(a: float2; b: float2; t: float2): float2
- lerp(a: float3; b: float3; t: float): float3
- lerp(a: float3; b: float3; t: float3): float3
- lerp(a: float4; b: float4; t: float): float4
- lerp(a: float4; b: float4; t: float4): float4
- lerp(a: float; b: float; t: float): float
2.2.9.3. mad¶
- mad(a: double; b: double; c: double): double¶
Computes the multiply-add operation a * b + c.
- Arguments:
a : double
b : double
c : double
- mad(a: float2; b: float2; c: float2): float2
- mad(a: float2; b: float; c: float2): float2
- mad(a: float3; b: float3; c: float3): float3
- mad(a: float3; b: float; c: float3): float3
- mad(a: float4; b: float4; c: float4): float4
- mad(a: float4; b: float; c: float4): float4
- mad(a: float; b: float; c: float): float
- mad(a: int2; b: int2; c: int2): int2
- mad(a: int2; b: int; c: int2): int2
- mad(a: int3; b: int3; c: int3): int3
- mad(a: int3; b: int; c: int3): int3
- mad(a: int4; b: int4; c: int4): int4
- mad(a: int4; b: int; c: int4): int4
- mad(a: int; b: int; c: int): int
- mad(a: uint2; b: uint2; c: uint2): uint2
- mad(a: uint2; b: uint; c: uint2): uint2
- mad(a: uint3; b: uint3; c: uint3): uint3
- mad(a: uint3; b: uint; c: uint3): uint3
- mad(a: uint4; b: uint4; c: uint4): uint4
- mad(a: uint4; b: uint; c: uint4): uint4
- mad(a: uint; b: uint; c: uint): uint
2.2.10. Matrix operations¶
- float3x3!=(x: float3x3; y: float3x3): bool¶
Returns true if two float3x3 matrices are not equal, comparing all elements component-wise.
2.2.10.1. float3x3*¶
- float3x3*(x: float3x3; y: float3): float3¶
Transforms a float3 vector by a 3x3 matrix.
- Arguments:
x : float3x3 implicit
y : float3
- float3x3*(x: float3x3; y: float3x3): float3x3
- float3x3==(x: float3x3; y: float3x3): bool¶
Returns true if two float3x3 matrices are exactly equal, comparing all elements component-wise.
- float3x4!=(x: float3x4; y: float3x4): bool¶
Returns true if two float3x4 matrices are not equal, comparing all elements component-wise.
2.2.10.2. float3x4*¶
- float3x4*(x: float3x4; y: float3): float3¶
Transforms a float3 vector by a 3x4 matrix.
- Arguments:
x : float3x4 implicit
y : float3
- float3x4*(x: float3x4; y: float3x4): float3x4
- float3x4==(x: float3x4; y: float3x4): bool¶
Returns true if two float3x4 matrices are exactly equal, comparing all elements component-wise.
- float4x4!=(x: float4x4; y: float4x4): bool¶
Returns true if two float4x4 matrices are not equal, comparing all elements component-wise.
2.2.10.3. float4x4*¶
- float4x4*(x: float4x4; y: float4): float4¶
Transforms a float4 vector by a 4x4 matrix.
- Arguments:
x : float4x4 implicit
y : float4
- float4x4*(x: float4x4; y: float4x4): float4x4
- float4x4==(x: float4x4; y: float4x4): bool¶
Returns true if two float4x4 matrices are exactly equal, comparing all elements component-wise.
2.2.11. Matrix initializers¶
2.2.11.1. float3x3¶
- float3x3(): float3x3
Extracts the upper-left 3x3 rotation part from a float3x4 transformation matrix, returning it as a float3x3.
- float3x3(arg0: float3x4): float3x3
- float3x3(arg0: float4x4): float3x3
2.2.11.2. float3x4¶
- float3x4(): float3x4
Constructs a float3x4 transformation matrix from a float3x3 rotation matrix, with the translation component set to zero.
- float3x4(arg0: float4x4): float3x4
2.2.11.3. float4x4¶
- float4x4(): float4x4
Converts a float3x4 transformation matrix to a float4x4 matrix, filling the fourth row with [0, 0, 0, 1].
- float4x4(arg0: float3x4): float4x4
- identity3x3(): float3x3¶
Returns a float3x3 identity matrix with ones on the diagonal and zeros elsewhere.
- identity3x4(): float3x4¶
Returns a float3x4 identity transformation matrix with the rotation part set to identity and the translation set to zero.
- identity4x4(): float4x4¶
Returns a float4x4 identity matrix with ones on the diagonal and zeros elsewhere.
2.2.12. Matrix manipulation¶
- 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
- 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.12.1. determinant¶
- determinant(x: float3x3): float¶
Returns the determinant of a float3x3 matrix as a float scalar; a zero determinant indicates the matrix is singular and non-invertible.
- Arguments:
x : float3x3 implicit
- determinant(x: float3x4): float
- determinant(x: float4x4): float
2.2.12.2. identity¶
- identity(x: float3x3)¶
Sets the given float3x3 matrix to the identity transformation and returns it.
- Arguments:
x : float3x3 implicit
- identity(x: float3x4)
- identity(x: float4x4)
2.2.12.3. inverse¶
- inverse(m: float4x4): float4x4¶
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:
m : float4x4 implicit
- inverse(x: float3x4): float3x4
- 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.12.4. orthonormal_inverse¶
- 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
- orthonormal_inverse(m: float3x4): float3x4
- 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
- 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
- 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
- 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
- transpose(x: float4x4): float4x4¶
Returns the transpose of a float3x3 or float4x4 matrix, swapping rows and columns.
- Arguments:
x : float4x4 implicit
2.2.13. Quaternion operations¶
- 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.13.1. quat¶
- quat(m: float3x3): float4¶
Extracts the rotation part of a float3x4 matrix and returns it as a float4 quaternion in (x, y, z, w) format.
- Arguments:
m : float3x3 implicit
- quat(m: float3x4): float4
- quat(m: float4x4): float4
- 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.13.2. quat_from_euler¶
- quat_from_euler(angles: float3): float4¶
Creates a float4 quaternion from a float3 of Euler angles (pitch, yaw, roll) given in radians.
- Arguments:
angles : float3
- quat_from_euler(x: float; y: float; z: float): float4
- 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
- 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
- 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
- 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
- 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.14. Packing and unpacking¶
- 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
- 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