libstd/math
Trigonometry and square root on float, a 3-component vector, and 4×4 column-major matrices for graphics (Vulkan clip-space conventions).
#include <libstd/math>
Includes libstd/stdio.
Example
#include <libstd/stdio>
#include <libstd/math>
print(ftoa(tryc_sqrt(2.0)), "\n")
eye = vec3_new(0.0, 0.0, 3.0)
center = vec3_new(0.0, 0.0, 0.0)
up = vec3_new(0.0, 1.0, 0.0)
view = mat4_look_at(eye, center, up)
proj = mat4_perspective(PI / 4.0, 4.0 / 3.0, 0.1, 100.0)
mvp = mat4_multiply(proj, mat4_multiply(view, mat4_rotate_y(0.5)))
Constants
| Name | Value | Meaning |
|---|---|---|
PI | 3.14159265358979323846 | |
TAU | 6.28318530717958647692 |
Classes
class
Vec3
Three float components.
x float
y float
z floatFunctions
def a64_sin_cos(x: float, want_cos: int) -> float
aarch64 only.
(no description yet)def tryc_sin(x: float) -> float
Sine.
def tryc_cos(x: float) -> float
Cosine.
def tryc_sqrt(x: float) -> float
Square root (sqrtsd).
def tryc_f32_bits(x: float) -> int
The IEEE-754 single-precision bit pattern of a float (a real conversion, for buffers a GPU reads as 32-bit floats).
def vec3_new(x: float, y: float, z: float) -> Vec3
A new vector.
def vec3_sub(a: Vec3, b: Vec3) -> Vec3
a - b.def vec3_cross(a: Vec3, b: Vec3) -> Vec3
Cross product.
def vec3_dot(a: Vec3, b: Vec3) -> float
Dot product.
def vec3_length(a: Vec3) -> float
Euclidean length.
def vec3_normalize(a: Vec3) -> Vec3
The unit vector in the same direction.
def mat4_identity() -> float[16]
The identity matrix as float[16].
def mat4_multiply(a: float[16], b: float[16]) -> float[16]
a * b, column-major: applying the result equals applying b then a.def mat4_rotate_y(angle: float) -> float[16]
Rotation around the Y axis by angle radians.
def mat4_perspective(fovy: float, aspect: float, near: float, far: float) -> float[16]
Perspective projection with Vulkan's inverted clip-space Y.
def mat4_look_at(eye: Vec3, center: Vec3, up: Vec3) -> float[16]
View matrix looking from eye at center.
def mat4_mul_vec4(m: float[16], x: float, y: float, z: float, w: float) -> float[4]
Matrix times a 4-component vector, as float[4].