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.SINtable (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)— likesinbut 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.