MathHelper

Fast lookup-table trig, floor/clamp/angle-wrap helpers backing movement, rotation and render math.

net/minecraft/util/MathHelper.java

Updated 2026-09-12

Packets Referenced (0)

Related Vanilla Internals

Deep Analysis

Static math utility in net.minecraft.util used by entities, rendering, and packets. Provides sin table, floor, clamp, angle wrap, and noise helpers. Matters for packets because movement yaw/pitch, positions, and chunk math all normalize through it.

Key Methods (8)

sin

Fast sine via 65536-entry lookup table.

Index=(int)(value*10430.378)&65535 where constant is 65536/2pi. Table precomputed in static block. Used for yaw, head look, and motion vectors.

cos

Fast cosine via sine table with quarter-period offset.

Index adds 16384 (pi/2) before mask. Avoids second table. Same 2pi/65536 step resolution as sin.

floor_double

Greatest integer <= double for block coords.

Casts to int then subtracts 1 if value below cast. Handles negatives correctly unlike truncation. Basis for BlockPos from entity pos.

clamp_double

Constrains value to [min,max] range.

Ternary num<min?min:num>max?max:num. Timer uses it to clamp frame delta 0-1. Also used for health, progress, and deltas.

wrapAngleTo180_float

Normalizes yaw to [-180,180) degrees.

Applies %360 then subtracts 360 if >=180, adds if <-180. Double variant identical. Critical for S08/C03 look deltas.

func_154353_e

Ceil of absolute value for packet size math.

Returns (int)(value>=0?value:-value+1). Used for entity size and bounding computations. Avoids float rounding edge cases.

bucketInt

Floor division bucket for negative chunk coords.

Formula p<0?-((-p-1)/size)-1:p/size. Ensures -1..-16 maps to chunk -1. Used for chunk lookup from block pos.

calculateLogBaseTwo

Floor log2 via De Bruijn sequence for bit packing.

Rounds up to power of two then De Bruijn multiply 125613361>>27 lookup. Subtracts 1 if not power of two. BlockPos uses it for NUM_X_BITS.

Internal State

SIN_TABLE65536 float sine samples over 0 to 2pi
SQRT_2Precomputed sqrt(2) constant
multiplyDeBruijnBitPosition32-entry log2 lookup for De Bruijn index
field_181164_e257-entry asin table for fast atan2
field_181165_f257-entry cos(asin) table for atan2

Related Vanilla Internals

Full Source

MathHelper.java564 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.util;
2
3
import java.util.Random;
4
import java.util.UUID;
5
6
public class MathHelper
7
{
8
    public static final float SQRT_2 = sqrt_float(2.0F);
9
10
    /**
11
     * A table of sin values computed from 0 (inclusive) to 2*pi (exclusive), with steps of 2*PI / 65536.
12
     */
13
    private static final float[] SIN_TABLE = new float[65536];
14
15
    /**
16
     * Though it looks like an array, this is really more like a mapping.  Key (index of this array) is the upper 5 bits
17
     * of the result of multiplying a 32-bit unsigned integer by the B(2, 5) De Bruijn sequence 0x077CB531.  Value
18
     * (value stored in the array) is the unique index (from the right) of the leftmost one-bit in a 32-bit unsigned
19
     * integer that can cause the upper 5 bits to get that value.  Used for highly optimized "find the log-base-2 of
20
     * this number" calculations.
21
     */
22
    private static final int[] multiplyDeBruijnBitPosition;
23
    private static final double field_181163_d;
24
    private static final double[] field_181164_e;
25
    private static final double[] field_181165_f;
26
27
    /**
28
     * sin looked up in a table
29
     */
30
    public static float sin(float p_76126_0_)
31
    {
32
        return SIN_TABLE[(int)(p_76126_0_ * 10430.378F) & 65535];
33
    }
34
35
    /**
36
     * cos looked up in the sin table with the appropriate offset
37
     */
38
    public static float cos(float value)
39
    {
40
        return SIN_TABLE[(int)(value * 10430.378F + 16384.0F) & 65535];
41
    }
42
43
    public static float sqrt_float(float value)
44
    {
45
        return (float)Math.sqrt((double)value);
46
    }
47
48
    public static float sqrt_double(double value)
49
    {
50
        return (float)Math.sqrt(value);
51
    }
52
53
    /**
54
     * Returns the greatest integer less than or equal to the float argument
55
     */
56
    public static int floor_float(float value)
57
    {
58
        int i = (int)value;
59
        return value < (float)i ? i - 1 : i;
60
    }
61
62
    /**
63
     * returns par0 cast as an int, and no greater than Integer.MAX_VALUE-1024
64
     */
65
    public static int truncateDoubleToInt(double value)
66
    {
67
        return (int)(value + 1024.0D) - 1024;
68
    }
69
70
    /**
71
     * Returns the greatest integer less than or equal to the double argument
72
     */
73
    public static int floor_double(double value)
74
    {
75
        int i = (int)value;
76
        return value < (double)i ? i - 1 : i;
77
    }
78
79
    /**
80
     * Long version of floor_double
81
     */
82
    public static long floor_double_long(double value)
83
    {
84
        long i = (long)value;
85
        return value < (double)i ? i - 1L : i;
86
    }
87
88
    public static int func_154353_e(double value)
89
    {
90
        return (int)(value >= 0.0D ? value : -value + 1.0D);
91
    }
92
93
    public static float abs(float value)
94
    {
95
        return value >= 0.0F ? value : -value;
96
    }
97
98
    /**
99
     * Returns the unsigned value of an int.
100
     */
101
    public static int abs_int(int value)
102
    {
103
        return value >= 0 ? value : -value;
104
    }
105
106
    public static int ceiling_float_int(float value)
107
    {
108
        int i = (int)value;
109
        return value > (float)i ? i + 1 : i;
110
    }
111
112
    public static int ceiling_double_int(double value)
113
    {
114
        int i = (int)value;
115
        return value > (double)i ? i + 1 : i;
116
    }
117
118
    /**
119
     * Returns the value of the first parameter, clamped to be within the lower and upper limits given by the second and
120
     * third parameters.
121
     */
122
    public static int clamp_int(int num, int min, int max)
123
    {
124
        return num < min ? min : (num > max ? max : num);
125
    }
126
127
    /**
128
     * Returns the value of the first parameter, clamped to be within the lower and upper limits given by the second and
129
     * third parameters
130
     */
131
    public static float clamp_float(float num, float min, float max)
132
    {
133
        return num < min ? min : (num > max ? max : num);
134
    }
135
136
    public static double clamp_double(double num, double min, double max)
137
    {
138
        return num < min ? min : (num > max ? max : num);
139
    }
140
141
    public static double denormalizeClamp(double lowerBnd, double upperBnd, double slide)
142
    {
143
        return slide < 0.0D ? lowerBnd : (slide > 1.0D ? upperBnd : lowerBnd + (upperBnd - lowerBnd) * slide);
144
    }
145
146
    /**
147
     * Maximum of the absolute value of two numbers.
148
     */
149
    public static double abs_max(double p_76132_0_, double p_76132_2_)
150
    {
151
        if (p_76132_0_ < 0.0D)
152
        {
153
            p_76132_0_ = -p_76132_0_;
154
        }
155
156
        if (p_76132_2_ < 0.0D)
157
        {
158
            p_76132_2_ = -p_76132_2_;
159
        }
160
161
        return p_76132_0_ > p_76132_2_ ? p_76132_0_ : p_76132_2_;
162
    }
163
164
    /**
165
     * Buckets an integer with specifed bucket sizes.  Args: i, bucketSize
166
     */
167
    public static int bucketInt(int p_76137_0_, int p_76137_1_)
168
    {
169
        return p_76137_0_ < 0 ? -((-p_76137_0_ - 1) / p_76137_1_) - 1 : p_76137_0_ / p_76137_1_;
170
    }
171
172
    public static int getRandomIntegerInRange(Random p_76136_0_, int p_76136_1_, int p_76136_2_)
173
    {
174
        return p_76136_1_ >= p_76136_2_ ? p_76136_1_ : p_76136_0_.nextInt(p_76136_2_ - p_76136_1_ + 1) + p_76136_1_;
175
    }
176
177
    public static float randomFloatClamp(Random p_151240_0_, float p_151240_1_, float p_151240_2_)
178
    {
179
        return p_151240_1_ >= p_151240_2_ ? p_151240_1_ : p_151240_0_.nextFloat() * (p_151240_2_ - p_151240_1_) + p_151240_1_;
180
    }
181
182
    public static double getRandomDoubleInRange(Random p_82716_0_, double p_82716_1_, double p_82716_3_)
183
    {
184
        return p_82716_1_ >= p_82716_3_ ? p_82716_1_ : p_82716_0_.nextDouble() * (p_82716_3_ - p_82716_1_) + p_82716_1_;
185
    }
186
187
    public static double average(long[] values)
188
    {
189
        long i = 0L;
190
191
        for (long j : values)
192
        {
193
            i += j;
194
        }
195
196
        return (double)i / (double)values.length;
197
    }
198
199
    public static boolean epsilonEquals(float p_180185_0_, float p_180185_1_)
200
    {
201
        return abs(p_180185_1_ - p_180185_0_) < 1.0E-5F;
202
    }
203
204
    public static int normalizeAngle(int p_180184_0_, int p_180184_1_)
205
    {
206
        return (p_180184_0_ % p_180184_1_ + p_180184_1_) % p_180184_1_;
207
    }
208
209
    /**
210
     * the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
211
     */
212
    public static float wrapAngleTo180_float(float value)
213
    {
214
        value = value % 360.0F;
215
216
        if (value >= 180.0F)
217
        {
218
            value -= 360.0F;
219
        }
220
221
        if (value < -180.0F)
222
        {
223
            value += 360.0F;
224
        }
225
226
        return value;
227
    }
228
229
    /**
230
     * the angle is reduced to an angle between -180 and +180 by mod, and a 360 check
231
     */
232
    public static double wrapAngleTo180_double(double value)
233
    {
234
        value = value % 360.0D;
235
236
        if (value >= 180.0D)
237
        {
238
            value -= 360.0D;
239
        }
240
241
        if (value < -180.0D)
242
        {
243
            value += 360.0D;
244
        }
245
246
        return value;
247
    }
248
249
    /**
250
     * parses the string as integer or returns the second parameter if it fails
251
     */
252
    public static int parseIntWithDefault(String p_82715_0_, int p_82715_1_)
253
    {
254
        try
255
        {
256
            return Integer.parseInt(p_82715_0_);
257
        }
258
        catch (Throwable var3)
259
        {
260
            return p_82715_1_;
261
        }
262
    }
263
264
    /**
265
     * parses the string as integer or returns the second parameter if it fails. this value is capped to par2
266
     */
267
    public static int parseIntWithDefaultAndMax(String p_82714_0_, int p_82714_1_, int p_82714_2_)
268
    {
269
        return Math.max(p_82714_2_, parseIntWithDefault(p_82714_0_, p_82714_1_));
270
    }
271
272
    /**
273
     * parses the string as double or returns the second parameter if it fails.
274
     */
275
    public static double parseDoubleWithDefault(String p_82712_0_, double p_82712_1_)
276
    {
277
        try
278
        {
279
            return Double.parseDouble(p_82712_0_);
280
        }
281
        catch (Throwable var4)
282
        {
283
            return p_82712_1_;
284
        }
285
    }
286
287
    public static double parseDoubleWithDefaultAndMax(String p_82713_0_, double p_82713_1_, double p_82713_3_)
288
    {
289
        return Math.max(p_82713_3_, parseDoubleWithDefault(p_82713_0_, p_82713_1_));
290
    }
291
292
    /**
293
     * Returns the input value rounded up to the next highest power of two.
294
     */
295
    public static int roundUpToPowerOfTwo(int value)
296
    {
297
        int i = value - 1;
298
        i = i | i >> 1;
299
        i = i | i >> 2;
300
        i = i | i >> 4;
301
        i = i | i >> 8;
302
        i = i | i >> 16;
303
        return i + 1;
304
    }
305
306
    /**
307
     * Is the given value a power of two?  (1, 2, 4, 8, 16, ...)
308
     */
309
    private static boolean isPowerOfTwo(int value)
310
    {
311
        return value != 0 && (value & value - 1) == 0;
312
    }
313
314
    /**
315
     * Uses a B(2, 5) De Bruijn sequence and a lookup table to efficiently calculate the log-base-two of the given
316
     * value.  Optimized for cases where the input value is a power-of-two.  If the input value is not a power-of-two,
317
     * then subtract 1 from the return value.
318
     */
319
    private static int calculateLogBaseTwoDeBruijn(int value)
320
    {
321
        value = isPowerOfTwo(value) ? value : roundUpToPowerOfTwo(value);
322
        return multiplyDeBruijnBitPosition[(int)((long)value * 125613361L >> 27) & 31];
323
    }
324
325
    /**
326
     * Efficiently calculates the floor of the base-2 log of an integer value.  This is effectively the index of the
327
     * highest bit that is set.  For example, if the number in binary is 0...100101, this will return 5.
328
     */
329
    public static int calculateLogBaseTwo(int value)
330
    {
331
        return calculateLogBaseTwoDeBruijn(value) - (isPowerOfTwo(value) ? 0 : 1);
332
    }
333
334
    public static int roundUp(int p_154354_0_, int p_154354_1_)
335
    {
336
        if (p_154354_1_ == 0)
337
        {
338
            return 0;
339
        }
340
        else if (p_154354_0_ == 0)
341
        {
342
            return p_154354_1_;
343
        }
344
        else
345
        {
346
            if (p_154354_0_ < 0)
347
            {
348
                p_154354_1_ *= -1;
349
            }
350
351
            int i = p_154354_0_ % p_154354_1_;
352
            return i == 0 ? p_154354_0_ : p_154354_0_ + p_154354_1_ - i;
353
        }
354
    }
355
356
    public static int func_180183_b(float p_180183_0_, float p_180183_1_, float p_180183_2_)
357
    {
358
        return func_180181_b(floor_float(p_180183_0_ * 255.0F), floor_float(p_180183_1_ * 255.0F), floor_float(p_180183_2_ * 255.0F));
359
    }
360
361
    public static int func_180181_b(int p_180181_0_, int p_180181_1_, int p_180181_2_)
362
    {
363
        int lvt_3_1_ = (p_180181_0_ << 8) + p_180181_1_;
364
        lvt_3_1_ = (lvt_3_1_ << 8) + p_180181_2_;
365
        return lvt_3_1_;
366
    }
367
368
    public static int func_180188_d(int p_180188_0_, int p_180188_1_)
369
    {
370
        int i = (p_180188_0_ & 16711680) >> 16;
371
        int j = (p_180188_1_ & 16711680) >> 16;
372
        int k = (p_180188_0_ & 65280) >> 8;
373
        int l = (p_180188_1_ & 65280) >> 8;
374
        int i1 = (p_180188_0_ & 255) >> 0;
375
        int j1 = (p_180188_1_ & 255) >> 0;
376
        int k1 = (int)((float)i * (float)j / 255.0F);
377
        int l1 = (int)((float)k * (float)l / 255.0F);
378
        int i2 = (int)((float)i1 * (float)j1 / 255.0F);
379
        return p_180188_0_ & -16777216 | k1 << 16 | l1 << 8 | i2;
380
    }
381
382
    public static double func_181162_h(double p_181162_0_)
383
    {
384
        return p_181162_0_ - Math.floor(p_181162_0_);
385
    }
386
387
    public static long getPositionRandom(Vec3i pos)
388
    {
389
        return getCoordinateRandom(pos.getX(), pos.getY(), pos.getZ());
390
    }
391
392
    public static long getCoordinateRandom(int x, int y, int z)
393
    {
394
        long i = (long)(x * 3129871) ^ (long)z * 116129781L ^ (long)y;
395
        i = i * i * 42317861L + i * 11L;
396
        return i;
397
    }
398
399
    public static UUID getRandomUuid(Random rand)
400
    {
401
        long i = rand.nextLong() & -61441L | 16384L;
402
        long j = rand.nextLong() & 4611686018427387903L | Long.MIN_VALUE;
403
        return new UUID(i, j);
404
    }
405
406
    public static double func_181160_c(double p_181160_0_, double p_181160_2_, double p_181160_4_)
407
    {
408
        return (p_181160_0_ - p_181160_2_) / (p_181160_4_ - p_181160_2_);
409
    }
410
411
    public static double atan2(double p_181159_0_, double p_181159_2_)
412
    {
413
        double d0 = p_181159_2_ * p_181159_2_ + p_181159_0_ * p_181159_0_;
414
415
        if (Double.isNaN(d0))
416
        {
417
            return Double.NaN;
418
        }
419
        else
420
        {
421
            boolean flag = p_181159_0_ < 0.0D;
422
423
            if (flag)
424
            {
425
                p_181159_0_ = -p_181159_0_;
426
            }
427
428
            boolean flag1 = p_181159_2_ < 0.0D;
429
430
            if (flag1)
431
            {
432
                p_181159_2_ = -p_181159_2_;
433
            }
434
435
            boolean flag2 = p_181159_0_ > p_181159_2_;
436
437
            if (flag2)
438
            {
439
                double d1 = p_181159_2_;
440
                p_181159_2_ = p_181159_0_;
441
                p_181159_0_ = d1;
442
            }
443
444
            double d9 = func_181161_i(d0);
445
            p_181159_2_ = p_181159_2_ * d9;
446
            p_181159_0_ = p_181159_0_ * d9;
447
            double d2 = field_181163_d + p_181159_0_;
448
            int i = (int)Double.doubleToRawLongBits(d2);
449
            double d3 = field_181164_e[i];
450
            double d4 = field_181165_f[i];
451
            double d5 = d2 - field_181163_d;
452
            double d6 = p_181159_0_ * d4 - p_181159_2_ * d5;
453
            double d7 = (6.0D + d6 * d6) * d6 * 0.16666666666666666D;
454
            double d8 = d3 + d7;
455
456
            if (flag2)
457
            {
458
                d8 = (Math.PI / 2D) - d8;
459
            }
460
461
            if (flag1)
462
            {
463
                d8 = Math.PI - d8;
464
            }
465
466
            if (flag)
467
            {
468
                d8 = -d8;
469
            }
470
471
            return d8;
472
        }
473
    }
474
475
    public static double func_181161_i(double p_181161_0_)
476
    {
477
        double d0 = 0.5D * p_181161_0_;
478
        long i = Double.doubleToRawLongBits(p_181161_0_);
479
        i = 6910469410427058090L - (i >> 1);
480
        p_181161_0_ = Double.longBitsToDouble(i);
481
        p_181161_0_ = p_181161_0_ * (1.5D - d0 * p_181161_0_ * p_181161_0_);
482
        return p_181161_0_;
483
    }
484
485
    public static int hsvToRGB(float p_181758_0_, float p_181758_1_, float p_181758_2_)
486
    {
487
        int i = (int)(p_181758_0_ * 6.0F) % 6;
488
        float f = p_181758_0_ * 6.0F - (float)i;
489
        float f1 = p_181758_2_ * (1.0F - p_181758_1_);
490
        float f2 = p_181758_2_ * (1.0F - f * p_181758_1_);
491
        float f3 = p_181758_2_ * (1.0F - (1.0F - f) * p_181758_1_);
492
        float f4;
493
        float f5;
494
        float f6;
495
496
        switch (i)
497
        {
498
            case 0:
499
                f4 = p_181758_2_;
500
                f5 = f3;
501
                f6 = f1;
502
                break;
503
504
            case 1:
505
                f4 = f2;
506
                f5 = p_181758_2_;
507
                f6 = f1;
508
                break;
509
510
            case 2:
511
                f4 = f1;
512
                f5 = p_181758_2_;
513
                f6 = f3;
514
                break;
515
516
            case 3:
517
                f4 = f1;
518
                f5 = f2;
519
                f6 = p_181758_2_;
520
                break;
521
522
            case 4:
523
                f4 = f3;
524
                f5 = f1;
525
                f6 = p_181758_2_;
526
                break;
527
528
            case 5:
529
                f4 = p_181758_2_;
530
                f5 = f1;
531
                f6 = f2;
532
                break;
533
534
            default:
535
                throw new RuntimeException("Something went wrong when converting from HSV to RGB. Input was " + p_181758_0_ + ", " + p_181758_1_ + ", " + p_181758_2_);
536
        }
537
538
        int j = clamp_int((int)(f4 * 255.0F), 0, 255);
539
        int k = clamp_int((int)(f5 * 255.0F), 0, 255);
540
        int l = clamp_int((int)(f6 * 255.0F), 0, 255);
541
        return j << 16 | k << 8 | l;
542
    }
543
544
    static
545
    {
546
        for (int i = 0; i < 65536; ++i)
547
        {
548
            SIN_TABLE[i] = (float)Math.sin((double)i * Math.PI * 2.0D / 65536.0D);
549
        }
550
551
        multiplyDeBruijnBitPosition = new int[] {0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9};
552
        field_181163_d = Double.longBitsToDouble(4805340802404319232L);
553
        field_181164_e = new double[257];
554
        field_181165_f = new double[257];
555
556
        for (int j = 0; j < 257; ++j)
557
        {
558
            double d0 = (double)j / 256.0D;
559
            double d1 = Math.asin(d0);
560
            field_181165_f[j] = Math.cos(d1);
561
            field_181164_e[j] = d1;
562
        }
563
    }
564
}

Deobfuscated MCP 1.8.9 source of MathHelper from Marcelektro/MavenMCP-1.8.9 (net/minecraft/util/MathHelper.java), stored verbatim.