LuaJIT maths library with support for floating point determinism. By Tachytaenius.
- Operations create and return new types, only changing the fields of a type (like
v.x = 3
) doesn't. - The deterministic functions will be slower and less accurate than whatever your system provides.
Deterministic maths module.
Supplies Lua implementations of functions that may not give the same results on different machines if using the Lua math
library.
tau
: Library-supplied (so guaranteed to be the same) constant for the ratio of a circle's circumference to its radius.pi
: Library-supplied constant for the ratio of a circle's circumference to its diameter.e
: Library supplied constant for Euler's number.
getRoundingMode()
: Detects and returns the rounding mode in use as a string. Should be specified and checked against for a game's input-based replay files.exp(x)
: Returnse
raised to the power ofx
.pow(x, y)
: Returnsx
to the power ofy
. Currently has terrible error magnification.intPow(x, n)
: Returnsx
raised to the power ofn
wheren
is an integer. This function exists because the IEEE 754 standard does not define exponentiation to an integer power as deterministic.log(x)
: Returns the natural logarithm ofx
.sin(x)
: Returns the sine ofx
, wherex
is in radians.cos(x)
: Return the cosine ofx
, wherex
is in radians.tan(x)
: Returns the tangent ofx
, wherex
is in radians.asin(x)
: Returns the arcsine ofx
in radians.acos(x)
: Returns the arccosine ofx
in radians.atan(x)
: Returns the arctangent ofx
in radians.atan2(y, x)
: Returns the "atan2" of the coordinatesx, y
in radians. Returns 0 whenx
andy
both equate to 0, regardless as to the sign of the floats. Outputs in the range -tau/2 to tau/2.sinh(x)
: Returns the hyperbolic sine ofx
.cosh(x)
: Returns the hyperbolic cosine ofx
.tanh(x)
: Returns the hyperbolic tangent ofx
.
Supplies functionality for 2-dimensional vectors.
Has deterministic versions of its non-deterministic functions.
Calling the module is equivalent to calling its new
function.
vec2
: A 2-dimensional vector type. Supports addition, subtraction, negation, multiplication, division, modulo, equality testing, length, andtostring
. Has fieldsx
andy
.
new(x, y
): Returns a newvec2
with componentsx
andy
.length(a)
: Returns the length ofvec2
a
.length2(a)
: Returns the square of the length ofvec2
a
, faster thanlength(a)
.distance(a, b)
: Returns the distance betweenvec2
sa
andb
.distance2(a, b)
: Returns the square of the distance betweena
andb
, faster thandistance(a, b)
.dot(a, b)
: Returns the dot product ofvec2
sa
andb
.normalise(a)
: Returnsa
, normalised.normalize(a)
: Alias fornormalise(a)
.reflect(incident, normal)
: Reflect avec2
.refract(incident, normal, eta)
: Refract avec2
.rotate(v, a)
: Rotatevec2
v
by anglea
in radians.detRotate(v, a)
: Deterministic version ofrotate(v, a)
.fromAngle(a)
: Create a newvec
from anglea
in radians.detFromAngle(a)
: Deterministic version offromAngle(a)
.toAngle(v)
: Returnsatan2(v.y, v.x)
, in the range 0 to tau.detToAngle(v)
: Deterministic version oftoAngle(v)
.components(v)
: Returnsv.x, v.y
.clone(v)
: Creates a newvec2
identical tov
.
Supplies functionality for 3-dimensional vectors.
Has deterministic versions of its non-deterministic functions.
Calling the module is equivalent to calling its new
function.
vec3
: A 3-dimensional vector type. Supports addition, subtraction, negation, multiplication, division, modulo, equality testing, length, andtostring
. Has fieldsx
,y
, andz
.
new(x, y, z)
: Returns a newvec3
with componentsx
,y
, andz
.length(a)
: Returns the length ofvec3
a
.length2(a)
: Returns the square of the length ofvec3
a
, faster thanlength(a)
.distance(a, b)
: Returns the distance betweenvec3
sa
andb
.distance2(a, b)
: Returns the square of the distance betweena
andb
, faster thandistance(a, b)
.dot(a, b)
: Returns the dot product ofvec3
sa
andb
.cross(a, b)
: Returns the cross product ofvec3
sa
andb
.normalise(a)
: Returnsa
, normalised.normalize(a)
: Alias fornormalise(a)
.reflect(incident, normal)
: Reflect avec3
.refract(incident, normal, eta)
: Refract avec3
.rotate(v, q)
: Rotatevec3
v
withquat
q
.fromAngles(theta, phi)
: Create a newvec3
from horizontal angletheta
and vertical anglephi
.detFromAngles(theta, phi)
: Deterministic version offromAngles(theta, phi)
.components(v)
: Returnsv.x, v.y, v.z
.clone(v)
: Returns a newvec3
identical tov
.
Supplies functionality for quaternions.
Has deterministic versions of its non-deterministic functions.
Calling the module is equivalent to calling its new
function.
quat
: A quaternion type. Supports negation, multiplication, addition, equality testing, length, andtostring
. Has fieldsx
,y
,z
, andw
.
new(x, y, z, w)
: Returns a newquat
with componentsx
,y
,z
, andw
.length(q)
: Returns the length of aquat
q
.normalise(q)
: Returnsq
, normalised.normalize(q)
: Alias fornormalise(q)
.inverse(q)
: Returns the inverse ofquat
q
.dot(a, b)
: Returns the dot product ofquat
sa
andb
.slerp(a, b, i)
: Returns the spherical linear interpolation betweenquat
sa
andb
with interpolation factori
.detSlerp(a, b, i)
: Deterministic version ofslerp(a, b, i)
.fromAxisAngle(v)
: Returns a newquat
from axis-anglevec3
v
.detFromAxisAngle(v)
: Deterministic version offromAxisAngle(v)
.components(q)
: Returnsq.x, q.y, q.z, q.w
.clone(q)
: Returns a newquat
identical toq
.
Supplies functionality for 4x4 matrices.
Has deterministic versions of its non-deterministic functions.
Calling the module iss equivalent to calling its new
function.
mat4
: A 4x4 matrix type. Supports multiplication, equality testing, andtostring
. Has fields_00
,_01
,_02
,_03
,_10
,_11
,_12
,_13
,_20
,_21
,_22
,_23
,_30
,_31
,_32
, and_33
. The underscores are there because you can't have a field name that starts with a number. The second character is the x position of the component and the third character is the y position of the component.
new(...)
: Creates a newmat4
with components listed in the 16 arguments in the same order as fields.perspectiveLeftHanded(aspect, verticalFov, far, near)
: Creates a left-handed perspective projectionmat4
from aspect ratioaspect
(width / height), vertical field of viewverticalFov
(in radians), far plane distancefar
and near plane distancenear
.detPerspectiveLeftHanded(aspect, verticalFov, far, near)
: Deterministic version ofperspectiveLeftHanded(aspect, verticalFov, far, near)
. Not recommended for use in actual 3D rendering.perspectiveRightHanded(aspect, verticalFov, far, near)
: Creates a right-handed perspective projectionmat4
from aspect ratioaspect
(width / height), vertical field of viewverticalFov
(in radians), far plane distancefar
and near plane distancenear
.detPerspectiveRightHanded(aspect, verticalFov, far, near)
: Deterministic version ofperspectiveRightHanded(aspect, verticalFov, far, near)
. Not recommended for use in actual 3D rendering.translate(v)
: Creates a translationmat4
fromvec3
v
.rotate(q)
: Creates a rotationmat4
fromquat
q
.scale(v)
: Creates a scalemat4
fromvec3
v
.transform(t, r, s)
: Creates a transformationmat4
from translationvec3
t
, rotationquat
r
, and scalevec3
s
.camera(t, r, s)
: Creates a cameramat4
from translationvec3
t
, rotationquat
r
, and scalevec3
s
.components(m)
: Returns all the components of amat4
in the order described in themat4
type's description.clone(m)
: Creates a newmat4
identical tom
.inverse(m)
: Returns the inverse ofmat4
m
.transpose(m)
: Returns the transposition ofmat4
m
.