Skip to main content

Module trig

Module trig 

Source
Expand description

Port of Mojang net.minecraft.util.Mth’s float trig lookup tables.

Carvers, structures, entity physics, and a lot of other gameplay code call Mth.sin(float) / Mth.cos(float), not Math.sin(double). Matching block-level determinism with vanilla requires using the same 65536-entry float table instead of the precise FPU sine.

§Vanilla reference

private static final float[] SIN = new float[65536];
static {
    for (int i = 0; i < 65536; i++)
        SIN[i] = (float) Math.sin(i * (Math.PI * 2.0 / 65536.0));
}
public static float sin(double i)  { return SIN[(int)((long)(i * 10430.378350470453) & 65535L)]; }
public static float cos(double i)  { return SIN[(int)((long)(i * 10430.378350470453 + 16384.0) & 65535L)]; }

Note that the multiplier 10430.378350470453 is exactly 65536 / (2π) rounded to a double, and the table is indexed by the low 16 bits of the scaled angle.

Constants§

ATAN_TABLE_LEN 🔒
FRAC_BIAS 🔒
INDEX_SCALE 🔒
65536 / (2π) — Mojang’s stored constant.
ONE_SIXTH 🔒
TABLE_LEN 🔒
TABLE_MASK 🔒

Statics§

ASIN_TAB 🔒
Arcsine angles in radians for inputs from 0 to 1 in steps of 1/256.
COS_TAB 🔒
Cosines of the angles in ASIN_TAB
SIN_TABLE 🔒
The Mth.SIN table (one entry per 2π / 65536 radians, float-valued).

Functions§

atan2
Returns vanilla’s approximate angle in radians for the vector (x, y).
cos
Vanilla Mth.cos(double) — like sin but phase-shifted by +π/2 using the same table.
fast_inv_sqrt 🔒
Approximates 1 / sqrt(value) using a fast inverse square root algorithm.
sin
Vanilla Mth.sin(double) — returns the float from the 65536-entry sine table indexed by the angle’s low-16-bit bucket.