forked from genail/pulpcore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoreMath.java
More file actions
1200 lines (1030 loc) · 33.9 KB
/
CoreMath.java
File metadata and controls
1200 lines (1030 loc) · 33.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (c) 2008, Interactive Pulp, LLC
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Interactive Pulp, LLC nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
package pulpcore.math;
/**
The CoreMath class contains fixed-point arithmetic functions and other
useful math functions.
<p>
Fixed-point numbers can be used in place of floating-point numbers.
Regarding fixed-point numbers:
<p>
Addition and subtraction of two fixed-point numbers is done normally, using
+ and - operators.
<p>
Multiplying a fixed-point number by an integer is done normally,
using the * operator. The result is a fixed-point number.
<p>
Dividing a fixed-point number by an integer (the fixed-point number is the
numerator, and the integer is the denominator) is done normally, using the /
operator. The result is a fixed-point number.
*/
public class CoreMath {
public static final int FRACTION_BITS = 16;
public static final int FRACTION_MASK = (1 << FRACTION_BITS) - 1;
public static final int ONE = (1 << FRACTION_BITS);
public static final int ONE_HALF = ONE >> 1;
public static final int PI = (int)Math.round(Math.PI * ONE);
public static final int TWO_PI = (int)Math.round(2 * Math.PI * ONE);
public static final int ONE_HALF_PI = (int)Math.round(.5 * Math.PI * ONE);
public static final int E = (int)Math.round(Math.E * ONE);
public static final int MAX_VALUE = (1 << 31) - 1;
public static final int MIN_VALUE = -(1 << 31);
/** The maximum integer value that a 32-bit fixed-point value can represent. */
public static final int MAX_INT_VALUE = (1 << (31 - FRACTION_BITS)) - 1;
/** The minumum integer value that a 32-bit fixed-point value can represent. */
public static final int MIN_INT_VALUE = -(1 << (31 - FRACTION_BITS));
/** The maximum 32-bit floating-point value that a 32-bit fixed-point value can represent. */
// Math.round(MAX_FLOAT_VALUE * ONE) = MAX_VALUE
public static final float MAX_FLOAT_VALUE = 32768f;
/** The minumum 32-bit floating-point value that a 32-bit fixed-point value can represent. */
public static final float MIN_FLOAT_VALUE = -32768f;
/** The maximum 64-bit floating-point value that a 32-bit fixed-point value can represent. */
// Found this by trial and error
// Math.round(MAX_DOUBLE_VALUE * ONE) = MAX_VALUE
public static final double MAX_DOUBLE_VALUE = 32767.999992370602;
/** The minumum 64-bit floating-point value that a 32-bit fixed-point value can represent. */
public static final double MIN_DOUBLE_VALUE = -32768.0;
// For more accurate results for sine/cosine. This number was found by trial and error.
private static final int TWO_PI_ERROR_FACTOR = 6;
//private static final int TWO_PI_ERROR = -11;
private static final int TWO_PI_ERROR =
(int)Math.round((2*Math.PI*(1 << TWO_PI_ERROR_FACTOR) -
(double)(TWO_PI << TWO_PI_ERROR_FACTOR) / ONE) * ONE);
/** Number of fractional bits used for some internal calculations */
private static final int INTERNAL_BITS = 24;
// Prevent instantiation
private CoreMath() { }
/**
Converts an integer to a fixed-point value.
*/
public static final int toFixed(int n) {
if (n > MAX_INT_VALUE) {
return MAX_VALUE;
}
else if (n < MIN_INT_VALUE) {
return MIN_VALUE;
}
else {
return n << FRACTION_BITS;
}
}
/**
Converts a float to a fixed-point value.
*/
public static final int toFixed(float n) {
if (n > MAX_FLOAT_VALUE) {
return MAX_VALUE;
}
else if (n < MIN_FLOAT_VALUE) {
return MIN_VALUE;
}
else {
return Math.round(n * ONE);
}
}
/**
Converts a double-percsion float to a fixed-point value.
*/
public static final int toFixed(double n) {
if (n > MAX_DOUBLE_VALUE) {
return MAX_VALUE;
}
else if (n < MIN_DOUBLE_VALUE) {
return MIN_VALUE;
}
else {
return (int)Math.round(n * ONE);
}
}
/**
Converts a String representing a double-percsion float into a fixed-point value.
@throws NumberFormatException if the string does not contain a parsable number.
*/
public static final int toFixed(String n) throws NumberFormatException {
return toFixed(Double.valueOf(n).doubleValue());
}
/**
Converts a fixed-point value to a float.
*/
public static final float toFloat(int f) {
return (float)f / ONE;
}
/**
Converts a fixed-point value to a double.
*/
public static final double toDouble(int f) {
return (double)f / ONE;
}
/**
Converts a fixed-point value to an integer. Same behavior as casting
a float to an int.
*/
public static final int toInt(int f) {
if (f < 0) {
return toIntCeil(f);
}
else {
return toIntFloor(f);
}
}
/**
Converts a fixed-point value to an integer.
*/
public static final int toIntFloor(int f) {
return f >> FRACTION_BITS;
}
/**
Converts a fixed-point value to an integer.
*/
public static final int toIntRound(int f) {
return toIntFloor(f + ONE_HALF);
}
/**
Converts a fixed-point value to an integer.
*/
public static final int toIntCeil(int f) {
return -toIntFloor(-f);
}
/**
Returns the fractional part of a fixed-point value (removes the
integer part).
*/
public static final int fracPart(int f) {
return abs(f) & FRACTION_MASK;
}
/**
Returns the floor of a fixed-point value (removes the
fractional part).
*/
public static final int floor(int f) {
return f & ~FRACTION_MASK;
}
/**
Returns the ceil of a fixed-point value.
*/
public static final int ceil(int f) {
return -floor(-f);
}
/**
Returns the fixed-point value rounded to the nearest integer location.
*/
public static final int round(int f) {
return floor(f + ONE_HALF);
}
/**
Converts a fixed-point number to a base-10 string representation.
*/
public static final String toString(int f) {
return formatNumber(
abs(toInt(f)),
fracPart(f) << (32 - FRACTION_BITS), (f < 0),
1, 7, false);
}
/**
Converts a fixed-point number to a base-10 string representation using
the specified number of fractional digits.
*/
public static final String toString(int f, int numFractionalDigits) {
return formatNumber(
abs(toInt(f)),
fracPart(f) << (32 - FRACTION_BITS), (f < 0),
numFractionalDigits, numFractionalDigits, false);
}
/**
Converts a fixed-point number to a base-10 string representation.
@param f the fixed-point number
@param minFracDigits the minimum number of digits to show after
the decimal point.
@param maxFracDigits the maximum number of digits to show after
the decimal point.
@param grouping if (true, uses the grouping character (',')
to seperate groups in the integer portion of the number.
*/
public static String toString(int f,
int minFracDigits, int maxFracDigits, boolean grouping)
{
return formatNumber(
abs(toInt(f)),
fracPart(f) << (32 - FRACTION_BITS), (f < 0),
minFracDigits, maxFracDigits, grouping);
}
/**
Converts an integer to a base-10 string representation.
*/
public static final String intToString(int n) {
return formatNumber(abs(n), 0, (n < 0), 0, 0, false);
}
/**
Converts a integer to a base-10 string representation using
the specified number of fractional digits.
*/
public static final String intToString(int n, int numFractionalDigits) {
return formatNumber(abs(n), 0, (n < 0),
numFractionalDigits, numFractionalDigits, false);
}
/**
Converts an integer to a base-10 string representation.
@param n the integer
@param minFracDigits the minimum number of digits to show after
the decimal point.
@param maxFracDigits the maximum number of digits to show after
the decimal point.
@param grouping if (true, uses the grouping character (',')
to seperate groups in the integer portion of the number.
*/
public static String intToString(int n,
int minFracDigits, int maxFracDigits, boolean grouping)
{
return formatNumber(abs(n), 0, (n < 0),
minFracDigits, maxFracDigits, grouping);
}
/**
Converts a number to a base-10 string representation.
@param intPart the integer part of the number
@param fracPart the fractional part, a 32-bit fixed point value.
@param minFracDigits the minimum number of digits to show after
the decimal point.
@param maxFracDigits the maximum number of digits to show after
the decimal point.
@param intPartGrouping if (true, uses the groupong character (',')
to seperate groups in the integer portion of the number.
*/
private static String formatNumber(int intPart, int fracPart, boolean negative,
int minFracDigits, int maxFracDigits, boolean intPartGrouping)
{
StringBuffer buffer = new StringBuffer();
long one = 1L << 32;
long mask = one - 1;
long frac = ((long)fracPart) & mask;
// Round up if needed
if (maxFracDigits < 10) {
long place = 1;
for (int i = 0; i < maxFracDigits; i++) {
place *= 10;
}
frac += (1L << 31) / place;
if (frac >= one) {
intPart++;
}
}
// Convert integer part
if (!intPartGrouping || intPart == 0) {
if (negative) {
buffer.append('-');
}
buffer.append(intPart);
}
else {
int i = 0;
while (intPart > 0) {
if (i == 3) {
buffer.insert(0, ',');
i = 0;
}
char ch = (char)((intPart % 10) + '0');
buffer.insert(0, ch);
intPart /= 10;
i++;
}
if (negative) {
buffer.insert(0, '-');
}
}
if (maxFracDigits == 0 || (fracPart == 0 && minFracDigits == 0)) {
return buffer.toString();
}
buffer.append('.');
// Convert fractional part
int numFracDigits = 0;
while (true) {
frac = (frac & mask) * 10L;
if (frac == 0) {
buffer.append('0');
}
else {
buffer.append((char)('0' + ((frac >>> 32) % 10)));
}
numFracDigits++;
if (numFracDigits == maxFracDigits || (frac == 0 && numFracDigits >= minFracDigits)) {
break;
}
}
// Remove uneeded trailing zeros
if (numFracDigits > minFracDigits) {
int len = numFracDigits - minFracDigits;
for (int i = 0; i < len; i++) {
if (buffer.charAt(buffer.length() - 1) == '0') {
buffer.setLength(buffer.length() - 1);
}
else {
break;
}
}
}
return buffer.toString();
}
//
// Bit manipulation
//
/**
Returns true if the number (greater than 1) is a power of two.
*/
public static final boolean isPowerOfTwo(int n) {
return (n & (n - 1)) == 0;
}
/**
Counts the number of "on" bits in an integer.
*/
public static final int countBits(int n) {
/*
int count = 0;
while (n > 0) {
count += (n & 1);
n >>= 1;
}
return count;
*/
int count = n;
count = ((count >> 1) & 0x55555555) + (count & 0x55555555);
count = ((count >> 2) & 0x33333333) + (count & 0x33333333);
count = ((count >> 4) & 0x0F0F0F0F) + (count & 0x0F0F0F0F);
count = ((count >> 8) & 0x00FF00FF) + (count & 0x00FF00FF);
count = ((count >> 16) & 0x0000FFFF) + (count & 0x0000FFFF);
return count;
}
/**
Returns the log base 2 of an integer greater than 0. The returned value
is equal to {@code Math.floor(Math.log(n) / Math.log(2))}.
*/
public static final int log2(int n) {
//if (n <= 1) {
// throw new ArithmeticException("NaN");
//}
int count = 0;
while (true) {
n >>= 1;
if (n == 0) {
return count;
}
count++;
}
/*
int count = 0;
if ((n & 0xFFFF0000) != 0) {
n >>= 16;
count = 16;
}
if ((n & 0xFF00) != 0) {
n >>= 8;
count |= 8;
}
if ((n & 0xF0) != 0) {
n >>= 4;
count |= 4;
}
if ((n & 0xC) != 0) {
n >>= 2;
count |= 2;
}
if ((n & 0x2) != 0) {
//n >>= 1;
count |= 1;
}
return count;
*/
}
//
// Integer math
//
/**
Clamps a number between two values. If the number <= min returns min; if the number >= max
returns max; otherwise returns the number.
*/
public static final int clamp(int n, int min, int max) {
if (n <= min) {
return min;
}
else if (n >= max) {
return max;
}
else {
return n;
}
}
/**
Clamps a number between two values. If the number <= min returns min; if the number >= max
returns max; otherwise returns the number.
*/
public static final float clamp(float n, float min, float max) {
if (n <= min) {
return min;
}
else if (n >= max) {
return max;
}
else {
return n;
}
}
/**
Clamps a number between two values. If the number <= min returns min; if the number >= max
returns max; otherwise returns the number.
*/
public static final double clamp(double n, double min, double max) {
if (n <= min) {
return min;
}
else if (n >= max) {
return max;
}
else {
return n;
}
}
/**
Returns the sign of a number.
*/
public static final int sign(int n) {
return (n > 0)?1:((n < 0)?-1:0);
}
/**
Returns the sign of a number.
*/
public static final int sign(double n) {
return (n > 0)?1:((n < 0)?-1:0);
}
/**
Returns the absolute value of a number.
*/
public static final int abs(int n) {
return (n >= 0)?n:-n;
//return (n ^ (n >> 31)) - (n >> 31);
}
/**
Divides the number, n, by the divisor, d, rounding the result to the
nearest integer.
*/
public static final int intDivRound(int n, int d) {
if ((d > 0) ^ (n > 0)) {
return (n - (d >> 1)) / d;
}
else {
return (n + (d >> 1)) / d;
}
}
/**
Divides the number, n, by the divisor, d, returning the nearest integer
less than or equal to the result.
*/
public static final int intDivFloor(int n, int d) {
if (d > 0) {
if (n < 0) {
return (n - d + 1) / d;
}
else {
return n / d;
}
}
else if (d < 0) {
if (n > 0) {
return (n - d - 1) / d;
}
else {
return n / d;
}
}
else {
// d == 0 throws ArithmeticException
return n / d;
}
}
/**
Divides the number, n, by the divisor, d, returning the nearest integer
greater than or equal to the result.
*/
public static final int intDivCeil(int n, int d) {
return -intDivFloor(-n, d);
}
//
// Fixed-point math
//
/**
Multiplies two fixed-point numbers together.
*/
public static final int mul(int f1, int f2) {
return (int)(((long)f1 * f2) >> FRACTION_BITS);
}
/**
Multiplies two fixed-point numbers together.
*/
public static final long mul(long f1, long f2) {
return (f1 * f2) >> FRACTION_BITS;
}
/**
Divides the first fixed-point number by the second fixed-point number.
*/
public static final int div(int f1, int f2) {
return (int)(((long)f1 << FRACTION_BITS) / f2);
}
/**
Divides the first fixed-point number by the second fixed-point number.
*/
public static final long div(long f1, long f2) {
return (f1 << FRACTION_BITS) / f2;
}
/**
Multiplies the first two fixed-point numbers together, then divides by
the third fixed-point number.
*/
public static final int mulDiv(int f1, int f2, int f3) {
return (int)((long)f1 * f2 / f3);
}
/**
Multiplies the first two fixed-point numbers together, then divides by
the third fixed-point number.
*/
public static final long mulDiv(long f1, long f2, long f3) {
return f1 * f2 / f3;
}
//
// Logs and powers
//
public static final int sqrt(int fx) {
if (fx < 0) {
throw new ArithmeticException("NaN");
}
if (fx == 0 || fx == ONE) {
return fx;
}
// invert numbers less than one (if they aren't too small)
boolean invert = false;
if (fx < ONE && fx > 6) {
invert = true;
fx = div(ONE, fx);
}
int iterations = 16;
if (fx > ONE) {
// number of iterations == (number of bits in number) / 2
int s = fx;
iterations = 0;
while (s > 0) {
s >>=2;
iterations++;
}
}
// Newton's iteration
int l = (fx >> 1) + 1;
for (int i=1; i<iterations; i++) {
l = (l + div(fx, l)) >> 1;
}
// undo the inversion
if (invert) {
return div(ONE, l);
}
return l;
}
public static final long sqrt(long fx) {
if (fx < 0) {
throw new ArithmeticException("NaN");
}
if (fx == 0 || fx == ONE) {
return fx;
}
// Invert numbers less than one (if they aren't too small)
boolean invert = false;
if (fx < ONE && fx > 6) {
invert = true;
fx = div(ONE, fx);
}
int iterations = 16;
if (fx > ONE) {
// Number of iterations == (number of bits in number) / 2
long s = fx;
iterations = 0;
while (s > 0) {
s >>= 2;
iterations++;
}
}
// Newton's iteration
long l = (fx >> 1) + 1;
for (int i=1; i<iterations; i++) {
l = (l + div(fx, l)) >> 1;
}
// Undo the inversion
if (invert) {
return div(ONE, l);
}
return l;
}
public static long dist(int x1, int y1, int x2, int y2) {
long dx = x1 - x2;
long dy = y1 - y2;
return sqrt(mul(dx, dx) + mul(dy, dy));
}
//
// Fixed-point Trigonometry
//
/**
Returns the sine of the specified fixed-point radian value.
*/
public static final int sin(int fx) {
if (fx == 0) {
return 0;
}
// reduce range to -2*pi and 2*pi
int s = fx / TWO_PI;
if (abs(s) >= (1<<TWO_PI_ERROR_FACTOR)) {
// fix any error for large values of fx
fx -= s*TWO_PI + (s >> TWO_PI_ERROR_FACTOR) * TWO_PI_ERROR;
}
else {
fx -= s*TWO_PI;
}
// reduce range to -pi/2 and pi/2
// this allows us to limit the number of iterations in the maclaurin series
if (fx > PI) {
fx = fx - TWO_PI;
}
else if (fx < -PI) {
fx = fx + TWO_PI;
}
if (fx > ONE_HALF_PI) {
fx = PI - fx;
}
else if (fx < -ONE_HALF_PI) {
fx = -PI - fx;
}
// Helps with rotation appearance near 90, 180, 270, 360, etc.
if (abs(fx) < 32) {
return 0;
}
else if (abs(fx - ONE_HALF_PI) < 32) {
return ONE;
}
else if (abs(fx + ONE_HALF_PI) < 32) {
return -ONE;
}
// Maclaurin power series
int fxSquared = mul(fx, fx);
int d = mul((1 << INTERNAL_BITS) / (2*3*4*5*6*7*8*9), fxSquared);
int c = mul(d - (1 << INTERNAL_BITS) / (2*3*4*5*6*7), fxSquared);
int b = mul(c + (1 << INTERNAL_BITS) / (2*3*4*5), fxSquared);
int a = mul(b - (1 << INTERNAL_BITS) / (2*3), fxSquared);
int sine = mul(a + (1 << INTERNAL_BITS), fx);
return sine >> (INTERNAL_BITS - FRACTION_BITS);
}
/**
Returns the cosine of the specified fixed-point radian value.
*/
public static final int cos(int fx) {
if (fx == 0) {
return CoreMath.ONE;
}
else if (fx < 0) {
// make up for potential overflow
return sin(ONE_HALF_PI - TWO_PI - fx);
}
else {
return sin(ONE_HALF_PI - fx);
}
}
/**
Returns the tangent of the specified fixed-point radian value.
*/
public static final int tan(int fx) {
int cos = cos(fx);
if (cos == 0) {
return Integer.MAX_VALUE;
}
else {
return div(sin(fx), cos);
}
}
/**
Returns the cotangent of the specified fixed-point radian value.
*/
public static final int cot(int fx) {
int sin = sin(fx);
if (sin == 0) {
return Integer.MAX_VALUE;
}
else {
return div(cos(fx), sin);
}
}
/**
Returns the arcsine of the specified fixed-point value.
*/
public static final int asin(int fx) {
if (abs(fx) > ONE) {
throw new ArithmeticException("NaN");
}
else if (fx == ONE) {
return ONE_HALF_PI;
}
else if (fx == -ONE) {
return -ONE_HALF_PI;
}
else {
return atan(div(fx, sqrt(ONE - mul(fx, fx))));
}
}
/**
Returns the arccosine of the specified fixed-point value.
*/
public static final int acos(int fx) {
return ONE_HALF_PI - asin(fx);
}
/**
Returns the arctangent of the specified fixed-point value.
*/
public static final int atan(int fx) {
boolean negative = false;
boolean invert = false;
if (fx == 0) {
return 0;
}
if (fx < 0) {
negative = true;
fx = -fx;
}
// Avoid overflow
if (fx > ONE) {
invert = true;
fx = div(ONE, fx);
}
// Approximation from Ranko at http://www.lightsoft.co.uk/PD/stu/stuchat37.html
// r(x) = (x + 0.43157974*x^3)/(1 + 0.76443945*x^2 + 0.05831938*x^4)
int fxPow2 = mul(fx, fx);
int fxPow3 = mul(fxPow2, fx);
int fxPow4 = mul(fxPow3, fx);
int numer = fx + mul(28284, fxPow3);
int denom = ONE + mul(50098, fxPow2) + mul(3822, fxPow4);
int answer = div(numer, denom);
if (invert) {
answer = ONE_HALF_PI - answer;
}
if (negative) {
answer = -answer;
}
return answer;
}
/**
Returns in the range from -pi to pi.
*/
public static final int atan2(int fy, int fx) {
if (fy == 0) {
if (fx < 0) {
return PI;
}
else {
return 0;
}
}
else if (fx == 0) {
if (fy < 0) {
return -ONE_HALF_PI;
}
else {
return ONE_HALF_PI;
}
}
else {
int answer = atan(abs(div(fy, fx)));
if (fy > 0 && fx < 0) {
return PI - answer;
}
else if (fy < 0 && fx < 0) {
return answer - PI;
}
else if (fy < 0 && fx > 0) {
return -answer;
}
else {
return answer;
}
}
}
//
// Random number generation and Noise functions
//
/**
Returns a random integer from 0 to max, inclusive
*/
public static final int rand(int max) {
return rand(0, max);
}
/**
Returns a random integer from min to max, inclusive
*/
public static final int rand(int min, int max) {
// Prevent overflow
long range = (long)max - (long)min + 1;
int value = (int)(min + (long)(Math.random() * range));
// The Java 1.1 doc is unclear - Math.random() could return 1.0.
// In later versions of the doc, Math.random() is stated to return
// 0 <= x < 1.0. For the sake of compatibility, assume the
// return value is 0 <= x <= 1.0
// (In any case, Math.random() returning 1.0 will be a very rare case.)
if (value > max) {
return max;
}
else {
return value;
}
}
/**
Returns a random double from 0 to max, inclusive
*/
public static final double rand(double max) {
return rand(0, max);
}
/**
Returns a random double from min to max, inclusive
*/
public static final double rand(double min, double max) {
return min + (Math.random()*(max-min));
}
/**
Returns a random boolean.
*/
public static final boolean rand() {
return (rand(0, 1) == 0);
}